Showing posts with label Operating System. Show all posts
Showing posts with label Operating System. Show all posts

Thursday, April 22, 2010

PAGE REPLACEMENT ALGORiTHM(FIFO & LRU)

/*PROGRAM TO IMPLEMENT PAGE REPLACEMENT ALGORiTHM(FIFO & LRU)*/
// Add the header files stdlib.h      stdio.h      conio.h

#include
#include
#include

void fifo(int a[],int n)
{
    int i,m=n,x[3],v=0,y=0,next,j=0;
while(m!=0)
{
next=a[v];
j=0;
while(x[j]!=next&&j<3)
j++;

if(j>2)
{
printf("\nReplace page %d\n",x[y]);
x[y]=a[v];
v++;
y++;
m--;
}
else
{
printf("\n\nPage hit\n\n");
m--;
v++;
}
for(i=0;i<3;i++)
    printf(" %d",x[i]);

if(y==3)
y=0;
}
}
void lru (int b[],int n)
{
int i,m=n,x[3],v=0,y=0,next,j=0,min[3],min1=0,w,min2[3],min3;
for(i=0;i<3;i++)
{
x[i]=0;
min[i]=0;
}
for(i=0;i<3;i++)
{
j=0;
while(x[j]!=b[v]&&j<3)
j++;
if(j<3)
{
printf("\n\nPage hit\n\n");
m--;
v++;
}
else
{
printf("\nReplace page %d",x[y]);
x[y]=b[v];
v++;
m--;
y++;
for(j=0;j<3;j++)
printf(" %d",x[j]);
}
}

while(m!=0)
{
next=b[v];
j=0;
while(x[j]!=next&&j<3)
j++;
if(j<3)
{
printf("\n\nPage hit\n\n");
m--;
v++;
}
else
{
for(i=0;i<3;i++)
{
w=v;
while(b[w]!=x[i]&&w>0)
w--;
if(w>0)
{
min[i]=w;
min2[i]=b[w];
}
else
min[i]=w;
}
}
min3=min[0];
min1=0;
for(i=1;i<3;i++)
{
if(min[i]<=min3)
{
min3=min[i];
min1=i;
}
}
}
printf("\nReplace pager %d\n",x[min1]);
x[min1]=b[v];
v++;
m--;
for(i=0;i<3;i++)
printf(" %d", x[i]);
}

void main()
{
int n,i,a[25],b[25],c[25],ch;
clrscr();
printf("Enter how many pages:\n");
scanf("%d",&n);
printf("Enter sequence of pages\n");
for(i=0;i
{
scanf("%d",&a[i]);
b[i]=a[i];
c[i]=a[i];
}
do
{
printf("\nMENU\n");
printf("1.FIFO\n");
printf("2. LRU\n");
printf("3. EXIT\n");
printf("Enter choice\n");
scanf("%d",&ch);

switch(ch)
{
case 1: fifo(a,n);
break;
case 2: lru(c,n);
break;
case 3: break;
}
}
while(ch!=3);
getch();
}
/*Output:
Enter how many pages:
3                                                                              
Enter sequence of pages                                                        
1                                                                              
2                                                                              
3
                                                                               
MENU                                                                           
1.FIFO                                                                         
2. LRU                                                                         
3. EXIT                                                                        
Enter choice                                                                   
2                                                                              
                                                                               
Replace page 0 1 0 0                                                           
Replace page 0 1 2 0                                                           
Replace page 0 1 2 3                                                           
Replace pager 1                                                                
 0 2 3                                                                         
MENU
1.FIFO                                                                         
2. LRU                                                                         
3. EXIT                                                                        
Enter choice
1                                                                              
                                                                               
Replace page 8889                                                              
 1 10289 644                                                                   
Replace page 10289                                                             
 1 2 644                                                                       
Replace page 644                                                               
 1 2 3                                                                         
MENU                                                                           
1.FIFO                                                                         
2. LRU                                                                         
3. EXIT                                                                        
Enter choice
3
*/
                                                                       

Tuesday, April 20, 2010

Shell Script

Shell script to simulate a simple calculation:

a=$1
op="$2"
b=$3

if[$ # -It 3]
then
echo "$0 num1 opr num2"
echo "opr can be +,-,/,x"
exit 1
fi
case "$op" in
+)echo$(($a+$b));;
-)echo$(($a-$b));;
/)echo$(($a/$b));;
x)echo$(($a*$b));;
*)echo "error";;
esac

Shell script to find largest among 3 integers:

a=$1
op="$2"
b=$3

if[$ # -It 3]
then
echo"$0 n1 n2 n3"
exit 1
fi
if[$a -gt $b -a $a -gt $c]
then
echo "$a is largest"
elif[$a -gt $b -a $b -gt $c]
then
echo "$b is largest"
elif[$a -gt $b -a $c -gt $c]
echo "$c is largest"
else
echo"sorry cannot guess number"
fi


Shell script to sort numbers in file in ascending order:

file=" "
echo -n "enter file name:"
read file
if[! -f $file]
then
echo "$file not a file"
exit 1
fi
sort -n $file


Copy File:

echo"enter sorce and target file names"
read source target
if CP $ source $ target
then
echo
echo "file copied successfully"
else "error in file copy"
fi

0-10 Number-Guess No.:
echo "enter a number from 0 to 10"
read num
if test $num -eq 5
then
echo"guess successfully"
else
echo"try again"
fi

Display 1-10 Numbers:
i=1
until[$i -gt 10]
do
echo $i
i='expr $i +1'
done
 

PRODUCER CONSUMER WITH SEMAPHORE

//add header files stdio.h & conio.h
#include
#include
#include
#define N 5;
typedef int semaphore;
semaphore mutex=1;
semaphore empty=5;
semaphore full=0;
char buffer[5];
int front=0,rear=0;
int wait (semaphore *s)
{
*s=*s-1;
if(*s<0)
{
printf("\n process is blocked");
*s=0;
return 0;
}
return 1;
}
void signal(semaphore *s)
{
*s=*s+1;
if(*s<=0)
{
printf("Process is unblocked");
}
}
void producer(void)
{
char item;
if(wait("Ø"))
{
if(wait(&mutex))
{
printf("Enter the character data:");
scanf(" %c",&item);
buffer[front]=item;
front=(front+1)%5;
signal(&mutex);
signal(&full);
}
}
}
void consumer(void)
{
char item;
if(wait(&full))
{
if(wait(&mutex))
{
item=buffer[rear];
printf("\n consumed item: %c",item);
rear=(rear+1)%5;
signal(&mutex);
signal("Ø");
}
}
}
void view(void)
{
int i;
printf("\n Buffer data:");
printf("\n+---------------------------------+\n");
for(i=0;i<5;i++)
{
printf("| %c",buffer[i]);
}
printf("\n+---------------------------------+");
}
void main()
{
int i,choice,flag=0;
clrscr();
printf("\n 1. Insert item using Producer");
printf("\n 2. Remove item using Consumer");
printf("\n 3. View buffer data");
printf("\n 4. Exit");
do
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: producer();
break;
case 2: consumer();
break;
case 3: view();
break;
case 4: flag=1;
break;
default:printf("\n Please enter correct choice!");
break;
}
}while(flag==0);
}
/****************************OUTPUT************************************
1. Insert item using Producer
2. Remove item using Consumer
3. View buffer data
4. Exit
Enter your choice:1
Enter the character data:A

Enter your choice:1
Enter the character data:B

Enter your choice:1
Enter the character data:C

Enter your choice:1
Enter the character data:D

Enter your choice:1
Enter the character data:E

Enter your choice:3

Buffer data:
+---------------------------------+
| A| B| C| D| E
+---------------------------------+
Enter your choice:2

consumed item: A
Enter your choice:1
Enter the character data:F

Enter your choice:3

Buffer data:
+---------------------------------+
| F| B| C| D| E
+---------------------------------+
Enter your choice:4
*/

Saturday, April 17, 2010

Banker's Algorithm

//add header files stdio.h and conio.h
#include
#include

int AVAILABLE[4];
int MAX[3][4];
int ALLOCATION[3][4];
int NEED[3][4];
int REQUEST[3][4];

int safestate(void)
{
int i,j,k;
int WORK[4];
int FINISH[3]={0,0,0};
int SAFE=1;

for(j=0;j<4;j++)
WORK[j]=AVAILABLE[j];

for(i=0;i<3;i++)
{
if(FINISH[i]==0)
{
for(j=0;j<4;j++)
if(NEED[i][j]>WORK[j])
break;
if(j==4)
{
FINISH[i]=1;
for(k=0;k<4;k++)
WORK[j]=WORK[j]+ALLOCATION[i][k];
}
}
}

for(i=0;i<3;i++)
{
if(FINISH[i]==0)
{
SAFE=0;
break;
}
}
return(SAFE);
}

void main(void)
{
int i,j;
char ans='n';
int state=0;
clrscr();
printf("\n System has three processes and four types of resources");

printf("\n Enter four AVAILABLE matrix values:");
for(i=0;i<4;i++)
scanf("%d",&AVAILABLE[i]);

printf("\n Enter MAXIMUM required resources for each process:");
for(i=0;i<3;i++)
{
printf("\n P%d:",i+1);
for(j=0;j<4;j++)
scanf("%d",&MAX[i][j]);
}

printf("\n Enter currently ALLOCATED resources for each process:");
for(i=0;i<3;i++)
{
printf("\n P%d:",i+1);
for(j=0;j<4;j++)
scanf("%d",&ALLOCATION[i][j]);
}

printf("\nTHE NEED MATRIX\N ");
printf("\n\t R1\tR2\tR3\tR4");
for(i=0;i<3;i++)
{
printf("\n P%d:",i+1);
for(j=0;j<4;j++)
{
NEED[i][j]=MAX[i][j]-ALLOCATION[i][j];
printf("\t%d",NEED[i][j]);
}
}
state=safestate();
if(state==1)
{
printf("\x07");
printf("\n System is in safe state");
printf("\n Do u want to process request?");
scanf(" %c",&ans);
if(ans=='y' || ans=='Y')
{
printf("\n Which process requesting resources? P");
scanf("%d",&i);
printf("\n Enter REQUEST for process P%d :",i);
for(j=0;j<4;j++)
scanf("%d",&REQUEST[i][j]);
for(j=0;j<4;j++)
{
if(NEED[i][j]
{
printf("requesting resources more than needed!!");
exit(1);
}
}
for(j=0;j<4;j++)
{
if(REQUEST[i][j]>AVAILABLE[j])
{
printf("Resources are not available at this time!!");
exit(1);
}
}

for(j=0;j<4;j++)
{
ALLOCATION[i][j]=ALLOCATION[i][j]+REQUEST[i][j];
AVAILABLE[j]=AVAILABLE[j]-REQUEST[i][j];
}
state=safestate();
if(state==1)
{
printf("\n Request is granted");
}
else
{
for(j=0;j<4;j++)
{
ALLOCATION[i][j]=ALLOCATION[i][j]-REQUEST[i][j];
AVAILABLE[j]=AVAILABLE[j]+REQUEST[i][j];
}
printf("\n Request can not be safely allocated!!");
}
}
}
else
{printf("\x07");
printf("\n System is in unsafe state"); }
getch();
}
/*
OUTPUT:
System has three processes and four types of resources
Enter four AVAILABLE matrix values:1 2 3 4

Enter MAXIMUM required resources for each process:
P1:1 0 3 2

P2:1 2 4 6

P3:2 3 4 5

Enter currently ALLOCATED resources for each process:
P1:1 0 1 1

P2:2 1 3 4

P3:1 2 0 1

THE NEED MATRIXN
R1 R2 R3 R4
P1:   0 0 2 1
P2: -1 1 1 2
P3:   1 1 4 4
System is in unsafe state
*/

Producer Consumer Problem without using semaphore

/* Program To implement Producer Consumer Problem without using semaphore*/
//add header files stdio.h & conio,h
#include
#include
#define N 5;
int count=0;
int front=0;int rear=0;
char buffer[5];
int prodsleep=0;int consleep=0;
void producer(void)
{
char item;
if (count<5)
{
printf("\n Enter charecter data:");
scanf(" %c",&item);
buffer [front]=item;
front = (front+1)%5;
count++;
if(consleep==1 && count==1)
{
printf("\n Consumer Wakeup ");
}
}
else
{
printf("\n Sorry!! Buffer if full");
prodsleep=1;
}
}
void consumer(void)
{
char item;
if (count>0)
{
item = buffer[rear];
buffer[rear]=' ';
printf("\n Consumed item: %c",item);
rear=(rear+1)%5;
count--;
if(prodsleep==1 && count==4)
{
printf("\n Producer Wakeup");
}
}
else
{
printf("\n Sorry!! Buffer is Empty");
consleep=1;
}
}
void view(void)
{
int i;
printf("\n Buffer Data : ");
printf("\n +----------------------+\n");
for(i=0;i<5;i++)
{
printf("| %c ",buffer[i]);
}
printf("| \n +-------------------------+");
}
void main()
{
int i,choice,flag=0;
clrscr();
printf("\n 1:Insert item using producer ");
printf("\n 2:Remove item using consumer ");
printf("\n 3:View buffer data ");
printf("\n 4:Exit ");

do
{
printf("\n\n Enter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:producer();
break;
case 2:consumer();
break;
case 3:view();
break;
case 4:flag=1;
break;
default:printf("\n Please enter correct choice!");
break;
}
}while(flag==0);
}
/*
Output

1:Insert item using producer
2:Remove item using consumer
3:View buffer data
4:Exit

Enter your choice :1

Enter charecter data:a


Enter your choice :1

Enter charecter data:b


Enter your choice :1

Enter charecter data:c


Enter your choice :1

Enter charecter data:d


Enter your choice :1

Enter charecter data:e


Enter your choice :3

Buffer Data :
+----------------------+
| a | b | c | d | e |
+----------------------+

Enter your choice :1

Sorry!! Buffer if full

Enter your choice :2

Consumed item: a
Producer Wakeup


Enter your choice :2

Consumed item: b

Enter your choice :2

Consumed item: c

Enter your choice :2

Consumed item: d

Enter your choice :2

Consumed item: e

Enter your choice :2

Sorry!! Buffer is Empty

Enter your choice :3

Buffer Data :
+----------------------+
|     |     |     |     |     |
+----------------------+

Enter your choice :1

Enter charecter data:a

Consumer Wakeup

Enter your choice :3

Buffer Data :
+----------------------+
| a |     |    |    |     |
+----------------------+

Enter your choice :4
*/

Tuesday, April 13, 2010

Shortest Job First (SJF)

#include
#include
#include
#include
#include
void main()
{
 int p[10],ptemp;
 int b[10];
 int w[10];
 int t[10];
 int n,i,j,tw,ttat,btemp;
 float avgw,avgtat;
 clrscr();
 printf("\n\tProgram for SJF Scheduling");
 printf("\n\nEnter the Total Number Of Processes:");
 scanf("%d",&n);
 for(i=0;i
 {
    //printf("Enter the Name Of Process%d:",(i+1));
    //scanf("%d",&p[i]);
    p[i]=i+1;
    printf("\nEnter the burst time for Process%d:",(i+1));
    scanf("%d",&b[i]);
 }
 for(j=0;j
 {
    for(i=0;i
    {
        if(b[i]>b[i+1])
        {
            btemp=b[i];
            b[i]=b[i+1];
            b[i+1]=btemp;
            ptemp=p[i];
            p[i]=p[i+1];
            p[i+1]=ptemp;
        }
    }
 }
 w[0]=0;
 tw=w[0];
 t[0]=b[0];
 ttat=t[0];
 for(i=0;i
 {
    w[i+1]=w[i]+b[i];
    tw=tw+w[i+1];
    t[i+1]=t[i]+b[i+1];
    ttat=ttat+t[i+1];
 }
 avgw=tw/n;
 avgtat=ttat/n;
 printf("\n\n\t|");
 for(i=0;i
 {
    printf("\tP%d\t|",p[i]);
 }
 printf("\n\t%d",w[0]);
 for(i=0;i
 {
 printf("\t  \t%d",t[i]);
 }
 printf("\n\nProcess id \t Burst Time \t Wait Time \t T-Around Time \n");
 for(i=0;i
 {
    printf("P%d\t\t %d\t\t %d\t\t %d\n",p[i],b[i],w[i],t[i]);
 }
 printf("\n\nAverage wait Time:%f\n",avgw);
 printf("Average Turn Arount:%f",avgtat);
 getch();
}

/* OUTPUT
Enter the Total Number Of Processes:4
Enter the burst time for Process1:8
Enter the burst time for Process2:2
Enter the burst time for Process3:5
Enter the burst time for Process4:1


    |       P4      |       P2      |       P3      |       P1      |
    0               1                3                 8              16

Process id       Burst Time      Wait Time       T-Around Time
P4                  1                           0                  1
P2                  2                           1                  3
P3                 5                            3                  8
P1                 8                            8                  16


Average wait Time:3.000000
Average Turn Arount:7.000000
*/

Round Robin

#include
#include
void main()
{
int b[10],i,j=1,n,temp,burst[10],wait[10],turn[10],p[10],a=1,q,tat[10],t1=0;
float t=0,w=0;
clrscr();
printf("enter the no of process & Quantum time");
scanf("%d%d",&n,&q);
burst[0]=0;
b[0]=0;
tat[0]=0;
p[0]=0;
printf("enter burst time");
for(i=1;i<=n;i++)
scanf("%d",&burst[i]);
for(i=1;i<=n;i++)
b[i]=burst[i];
printf("\n\n\t\t gantt chart\n");
printf("_________________________________________________________\n");
for(i=1;i<=n;i++)
{
if(b[i]>0)
{
a=1;
printf("P%d\t|",i);
if(b[i]>=q)
{
t1=t1+q;
p[j]=t1;
j++;
}
else if(b[i]
{
t1=t1+b[i];
p[j]=t1;
j++;
}
b[i]=b[i]-q;
if(b[i]<=0)
tat[i]=t1;
}
else
a++;
if(a==n+1)
break;
if(i==n)
i=0;
}
printf("\n_________________________________________________________\n\n");
for(i=0;i
printf("%d\t",p[i]);
for(i=1;i<=n;i++)
{
t=t+tat[i];
w=w+tat[i]-burst[i];
}
w=w/n;
t=t/n;
printf("\n\n the average waiting time is %0.2f",w);
printf("\n\n the average turn around turnaround time is %0.2f",t);
getch();
}
/*Output:
enter the no of process & Quantum time3 4
enter burst time18 3 3                                                         
                                                                               

                 gantt chart                                                   
____________________________________                   
P1      |P2     |P3     |P1     |P1     |P1     |P1     |                      
____________________________________                  
                                                                               
0       4       7       10        14      18      22      24                     
                                                                               
 the average waiting time is 5.67                                              
                                                                               
 the average turn around turnaround time is 13.67
 */

Priority Scheduling

//Program to implement Priority Scheduling
/*------------------------------------*/
#include
#include
void main()
{
clrscr();
int n,burst[50],arr[50],wait[50],tat[50],pid[50],i;
cout<<"Enter no of process"<
cin>>n;
cout<<"enter process_id"<
for(i=0;i
{
cin>>pid[i];
}
cout<<"enter burst time"<
for(i=0;i
{
cin>>burst[i];
}
cout<<"enter the Arrival"<
for(i=0;i
{
cin>>arr[i];
}
cout<
int flag=0;
for(int j=0;j<=n-1;j++)
{
for(i=0;i<=n-j;i++)
{
if(burst[j]==burst[i])
{
flag=1;
}
}
}
for(j=0;j<=n-1;j++)
{
for(i=0;i<=n-j;i++)
{
if(flag!=1)
{
if(burst[j]>burst[i])
{
int temp1=burst[j];
burst[j]=burst[i];
burst[i]=temp1;

int temp2=pid[j];
pid[j]=pid[i];
pid[i]=temp2;
int temp3=arr[j];
arr[j]=arr[i];
arr[i]=temp3;
}
}
else
{
if(arr[j]>arr[i])
{
int temp1=burst[j];
burst[j]=burst[i];
burst[i]=temp1;

int temp2=pid[j];
pid[j]=pid[i];
pid[i]=temp2;
int temp3=arr[j];
arr[j]=arr[i];
arr[i]=temp3;
}
}
} }


wait[0]=0;
for(i=0;i
{
wait[i+1]=burst[i]+wait[i];
}
//int tat[50];
for(i=0;i<=n;i++)
{
tat[i]=burst[i]+wait[i];
}

float avg_wt=0;
for(i=0;i
{
avg_wt=wait[i]+avg_wt;
}
cout<<"\tProcess id\tburst\tArrival\twait\tTurnaround"<
for(i=0;i
{
cout<<"\t"<<<"\t\t"<<<"\t"<<<"\t\t"<<<"\t"<
cout<
}
cout<
cout<<"[1]Gantt Chart:-"<<<"\t\t";
for(i=0;i
{
cout<<"----";
}
cout<<<"\t\t";
cout<<"|";
for(i=0;i
{

cout<<"   "<<<"\t"<<"|";
//cout<
}
cout<<<"\t\t";
for(i=0;i
{
cout<<"----";
}
cout<<<"\t\t";
for(i=0;i
{

cout<<<"\t";
}
cout<<
cout<<"[2]Avg.wait time="<<
float avg_tt=0;
for(i=0;i
{
avg_tt=tat[i]+avg_tt;
}
cout<
cout<<"[3]Avg.turnarround time="<
getch();
}


/*
Enter no of process
4
enter process_id
1 2 3 4
enter burst time
2 4 3 6
enter the Priority
2 1 4 3

    Process id      burst   Priority        wait    Turnaround
    2               4       1               0       4
    1               2       2               4       6
    4               6       3               6       12
    3               3       4               12      15
                                                                               
[1]Gantt Chart:-                                                               
                ------------------------                             
                |   2   |   1   |   4   |   3   |                              
                ------------------------                              
                0       4       6     12    15                             
                                                                               
[2]Avg.wait time=5.5
                                                                               
[3]Avg.turnarround time=9.25 */
 
ShareThis

Visitor

Website counter
Copyright 2009 Code's. Powered by Blogger
Blogger Templates created by Deluxe Templates
Wordpress by Wpthemescreator
Blogger Showcase