Os Lab External New
Os Lab External New
Os Lab External New
totalT+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)totalT/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}
Output:
Enter number of process:3
Enter Burst Time:
p1:5
p2:4
p3:7
Process Burst Time Waiting Time Turnaround Time
p2 4 0 4
p1 5 4 9
p3 7 9 16
RR
#include<stdio.h>
void main()
{
int n,i,q,x=0,count=0,temp;
int bt[10],temp_bt[10],tat[10],wt[10];
float twt=0,ttat=0;
system("clear");
printf("***** RR (ROUND ROBIN) SCHEDULING ******\n");
printf("\nEnter the number of processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time of process %d : ",i);
scanf("%d",&bt[i]);
temp_bt[i]=bt[i];
}
printf("\nEnter the time quantum : ");
scanf("%d",&q);
while(1)
{
for(i=0;i<n;i++)
{
temp=q;
if(temp_bt[i]==0)
{
count++;
continue;
}
if(temp_bt[i]>q)
temp_bt[i]=temp_bt[i]-q;
else if(temp_bt[i]>=0)
{
temp=temp_bt[i];
temp_bt[i]=0;
}
x=x+temp;
tat[i]=x;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
printf("\nProcess\t|Burst Time\t|Wait Time\t|Turn-Around Time");
for(i=0;i<n;i++)
{
printf("\n%d\t|%d\t\t|%d\t\t|%d",i,bt[i],wt[i],tat[i]);
}
printf("\n\nTotal waiting time is %f",twt);
printf("\nAverage waiting time is %f",twt/n);
printf("\n\nTotal turn around time is %f",ttat);
printf("\nAverage turn around time is %f\n\n",ttat/n);
}
Output:
Enter Total Number of Processes:3
Enter Details of Process 1
Arrival Time: 2
Burst Time: 3
Enter Details of Process 2
Arrival Time: 1
Burst Time: 2
Enter Details of Process 3
Arrival Time: 3
Burst Time: 4
Enter Time Slot:5
Process ID Burst Time Turnaround Time Waiting Time
Process No 1 3 1 -2
Process No 2 2 4 2
Process No 3 4 6 2
Average Waiting Time:0.666667
Avg Turnaround Time:3.666667
3. Readers-Writers
#include<stdio.h>
#include<stdlib.h>
char arr[50];
void wait(int S)
{
while( S <= 0) ;
S--;
}
void signal(int S)
{
S++;
}
int readcount = 0;
int mutex =1;
int write=1;
int x=0;
void reader()
{
wait (mutex);
readcount ++;
if (readcount == 1)
{
wait(write);
}
signal(mutex);
wait(mutex);
readcount --;
if (readcount == 0)
{
signal (write);
}
signal(mutex);
printf("\n The reader reads: %s ",arr);
x--;
}
void writer()
{
wait(write);
signal(write);
x++;
printf("\n The writer writes: ");
scanf("%s",arr);
}
int main()
{
int n,i;
printf("\n1. Press 1 for writer"
"\n2. Press 2 for reader"
"\n3. Press 3 for exit");
printf("\nEnter your choice: ");
scanf("%d",&n);
while(n!=-1)
{
switch(n){
case 1:
writer();
break;
case 2:
reader();
break;
case 3:
exit(0);
break;
}
printf("\nEnter your choice: ");
scanf("%d",&n);
}
return 0;
}
output:
1. Press 1 for writer
2. Press 2 for reader
3. Press 3 for exit
Enter your choice: 1
The writer writes: hello
Enter your choice: 2
The reader reads: hello
Enter your choice: 3
4. Dining philosophers
#include<stdio.h>
enum{thinking,hungry,eating} state[50];
int self[50];
int signal(){ return eating; }
int wait(){ return hungry;}
void init(int n) {
int i;
for(i=0;i<n;i++)
state[i]=thinking;
}
void test(int i, int n){
if(state[(i+n-1)%n]!=eating && state[i]==hungry &&
state[(i+1)%n]!=eating){
state[i]=eating;
self[i]=signal();
}
}
void putdown(int i, int n){
state[i]=thinking;
printf("Philosopher %d went to thinking state\n",i+1);
test((i+n-1)%n,n);
test((i+1)%n,n);
}
void pickup(int i,int n) {
state[i]=hungry;
test(i,n);
if(state[i]!=eating){
self[i]=wait();
printf("Philosopher %d waiting i.e., hungry\n",i+1);
}
else
printf("Philosopher %d eating\n",i+1);
}
void showstats( int n ){
int i=0;
for(i=0;i<n;i++){
if(state[i]==thinking)
printf("Philosopher %d is Thinking\n",i+1);
else if(state[i]==hungry)
printf("Philosopher %d is Hungry\n",i+1);
else
printf("Philosopher %d is Eating\n",i+1);
}
}
void main(){
int i=0,n=0;
printf("Enter the number of Philosophers: ");
scanf("%d",&n);
init(n);
while(1)
{
printf("\n1.Pickup()\t2.Putdown()\t3.Showstats()\t4.Exit\n");
printf("Enter the choice : ");
scanf("%d",&i);
switch(i){
case 1: printf("Enter Philosopher number: ");
scanf("%d",&i);
pickup(i-1,n);
break;
case 2: printf("Enter Philosopher number: ");
scanf("%d",&i);
putdown(i-1,n);
break;
case 3: showstats(n);
break;
case 4:
return;
}
}
}
output:
Enter the number of Philosophers: 5
1.Pickup() 2.Putdown() 3.Showstats() 4.Exit
Enter the choice : 2
Enter Philosopher number: 1
Philosopher 1 went to thinking state
5.Producer-Consumer
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit (0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
Output:
1.Producer
2.Consumer
3.Exit
Enter your choice:1
Producer produces the item 1
Enter your choice:1
Producer produces the item 2
Enter your choice:2
Consumer consumes item 2
Enter your choice:2
Consumer consumes item 1
Enter your choice:3
Output:
Enter the length of the string 5
5
Enter the reference string:1 2 3 4 5
Enter no of frames:3
1
1 2
1 2 3
4 2 3
4 5 3
8. Bankers algorithm
#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
printf("Enter number of processes -- ");
scanf("%d",&n);
printf("Enter number of resources -- ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i);
printf("\nEnter allocation\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t -- \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
printf("\nEnter New Request Details -- ");
printf("\nEnter pid \t -- \t");
scanf("%d",&id);
printf("Enter Request for Resources \t -- \t");
for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i] += newr;
avail[i]=avail[i] - newr;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;
else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;
for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("("); for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
} }}
if(g==0)
{
printf("\n REQUEST NOT GRANTED -- DEADLOCK OCCURRED");
printf("\n SYSTEM IS IN UNSAFE STATE");
goto y;
}}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe Safe Sequence is -- (");
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
}
Output:
Output: Enter number of processes – 5
Enter number of resources – 3
Enter details for P0
Enter allocation -- 0 1 0
Enter Max -- 7 5 3
Enter details for P1
Enter allocation -- 2 0 0
Enter Max -- 3 2 2
Enter details for P2
Enter allocation -- 3 0 2
Enter Max -- 9 0 2
Enter details for P3
Enter allocation -- 2 1 1
Enter Max -- 2 2 2
Enter details for P4
Enter allocation -- 0 0 2
Enter Max -- 4 3 3
Enter Available Resources -- 3 3 2
Enter New Request Details –
Enter pid -- 1
Enter Request for Resources -- 1 0 2
P1 is visited( 5 3 2)
P3 is visited( 7 4 3)
P4 is visited( 7 4 5)
P0 is visited( 7 5 5)
P2 is visited( 10 5 7)
SYSTEM IS IN SAFE STATE
Output:
Enter the number of Requests
7
82
170
43
140
24
16
190
Enter initial head position
50
Total head moment is
208