Os Lab
Os Lab
#include<stdio.h>
int main()
int start[10],burst[10],need[10],execution[10],wait[10],finish[10],turn[10];
int i,ts,n,totaltime=0,totalburst=0;
float totalwait=0.0,totalturn=0.0,totalresp=0.0;
float avgwait=0.0,avgturn=0.0,avgresp=0.0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&burst[i]);
scanf("%d",&ts);
for(i=0;i<n;i++)
need[i]=burst[i];
execution[i]=0;
wait[i]=0;
finish[i]=0;
turn[i]=0;
totalburst=totalburst+burst[i];
while(totalburst>0)
for(i=0;i<n;i++)
if(execution[i]==0)
start[i]=totaltime;
}
if(need[i]>ts)
execution[i]=execution[i]+ts;
need[i]=need[i]-ts;
totaltime=totaltime+ts;
totalburst=totalburst-ts;
else
if(need[i]>0)
execution[i]=execution[i]+need[i];
totaltime=totaltime+need[i];
wait[i]=totaltime-execution[i];
finish[i]=wait[i]+burst[i];
turn[i]=wait[i]+burst[i];
totalburst=totalburst-need[i];
need[i]=0;
for(i=0;i<n;i++)
for(i=0;i<n;i++)
totalwait=totalwait+wait[i];
totalturn=totalturn+turn[i];
totalresp=totalresp+start[i];
avgwait=totalwait/n;
avgturn=totalturn/n;
avgresp=totalresp/n;
OUTPUT:
SJF
#include<stdio.h>
int main()
int i,j,burst[10],start[10],finish[10],wait[10];
int n,temp;
float totalwait=0.0,totalturn=0.0;
float avgwait,avgturn;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&burst[i]);
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(burst[i]>burst[j])
temp=burst[i];
burst[i]=burst[j];
burst[j]=temp;
for(i=1;i<=n;i++)
if(i==1)
start[i]=0;
finish[i]=burst[i];
wait[i]=0;
else
start[i]=finish[i-1];
finish[i]=start[i]+burst[i];
wait[i]=start[i];
for(i=1;i<=n;i++)
for(i=1;i<=n;i++)
totalwait=totalwait+wait[i];
totalturn=totalturn+finish[i];
avgwait=totalwait/n;
avgturn=totalturn/n;
OUTPUT:
FCFS
#include<stdio.h>
int main()
int arrival[10],burst[10],start[10],finish[10],wait[10],turn[10];
int i,j,n,sum=0;
float totalwait=0.0,totalturn=0.0;
float avgwait=0.0,avgturn=0.0;
start[0]=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d %d",&arrival[i],&burst[i]);
for(i=0;i<n;i++)
sum=0;
for(j=0;j<i;j++)
sum=sum+burst[j];
}
start[i]=sum;
for(i=0;i<n;i++)
finish[i]=burst[i]+start[i];
wait[i]=start[i]-arrival[i];
turn[i]=burst[i]+wait[i];
for(i=0;i<n;i++)
totalwait=totalwait+wait[i];
totalturn=totalturn+turn[i];
avgwait=(totalwait/n);
avgturn=(totalturn/n);
for(i=0;i<n;i++)
OUTPUT:
0 24 0 24 0 24
0 3 24 27 24 27
0 3 27 30 27 30
PRIORITY
#include<stdio.h>
int main()
int burst[10],pri[10],wait[10],start[10],finish[10];
int i,j,temp1,temp2,n,totalwait=0,totalavg=0,totalturn=0;
float avgwait=0.0,avgturn=0.0;
printf("Enter n value");
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d %d",&burst[i],&pri[i]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(pri[i]>pri[j])
temp1=pri[i];
pri[i]=pri[j];
pri[j]=temp1;
temp2=burst[i];
burst[i]=burst[j];
burst[j]=temp2;
}
}
for(i=1;i<=n;i++)
if(i==1)
start[i]=0;
finish[i]=burst[i];
wait[i]=start[i];
else
start[i]=finish[i-1];
finish[i]=start[i]+burst[i];
wait[i]=start[i];
for(i=1;i<=n;i++)
for(i=1;i<=n;i++)
totalwait=totalwait+wait[i];
totalturn=totalturn+finish[i];
}
avgwait=totalwait/n;
avgturn=totalturn/n;
OUTPUT:
Enter n value 3
Enter Burst time and priority of process 1
24 3
Enter Burst time and priority of process 2
3 22
Enter Burst time and priority of process 3
31
Burst Priority Start Wait Finnish
24 3 0 0 24
3 2 24 24 27
3 1 27 27 30
Average waiting time=17.000000
Average turnaround time=27.000000
#include<stdio.h>
main()
int ms,i,ps[20],n,size,p[20],s,intr=0;
scanf("%d",&ms);
scanf("%d",&s);
ms-=s;
scanf("%d",&n);
size=ms/n;
for(i=0;i<n;i++)
{
printf("\n Enter process and process size:");
scanf("%d%d",&p[i],&ps[i]);
if(ps[i]<=size)
intr=intr+size-ps[i];
printf("process%d is allocated\n",p[i]);
else
OUTPUT:
#include<stdio.h>
main()
int i,m,n,tot,s[20];
scanf("%d",&tot);
scanf("%d",&m);
for(i=0;i<n;i++)
scanf("%d",&s[i]);
tot=tot-m;
for(i=0;i<n;i++)
if(tot>=s[i])
tot=tot-s[i];
else
OUTPUT:
FIFO
#include<stdio.h>
void main()
{
int pages[10],frames[10][10];
int i,j,k,faults=0,pos=0,count=0,npages,nframes;
char found='f';
scanf("%d",&npages);
scanf("%d",&nframes);
for(i=0;i<nframes;i++)
for(j=0;j<npages;j++)
frames[i][j]=-1;
for(i=0;i<npages;i++)
scanf("%d",&pages[i]);
found='f';
for(j=0;j<nframes;j++)
if(i!=0)
frames[j][i]=frames[j][i-1];
if(frames[j][i]==pages[i])
found='t';
if(found=='f')
frames[pos][i]=pages[i];
faults++;
printf("%d",frames[j][i]);
if(count<nframes-1)
count++;
else
count=nframes-1;
if(pos<nframes-1)
pos++;
else
pos=0;
Output:
Enter number of Pages:Enter number of Frames:10
Enter page value for page 1:0
Pagefault 1:0
Enter page value for page 2:2
Pagefault 2:02
Enter page value for page 3:3
Pagefault 3:023
Enter page value for page 4:2
Enter page value for page 5:5
Pagefault 4:523
Enter page value for page 6:4
Pagefault 5:543
Enter page value for page 7:9
Pagefault 6:549
Enter page value for page 8:0
Pagefault 7:049
Enter page value for page 9:5
Pagefault 8:059
Enter page value for page 10:9
LRU
#include<stdio.h>
void main()
int npages,nframes,i,j,k,faults=0,count=0,big=0,value=0;
int pages[10],frames[10][10],flags[10];
char found='f';
printf("Enter number of pages:");
scanf("%d",&npages);
scanf("%d",&nframes);
for(i=0;i<nframes;i++)
flags[i]=0;
for(i=0;i<nframes;i++)
for(j=0;j<npages;j++)
frames[i][j]=0;
for(i=0;i<npages;i++)
scanf("%d",&pages[i]);
found='f';
for(j=0;j<nframes;j++)
if(i!=0)
frames[j][i]=frames[j][i-1];
if(frames[j][i]==pages[i])
found='t';
for(k=0;k<=count;k++)
if(j==k)
flags[k]=1;
}
else
flags[k]=flags[k]+1;
if(found=='f')
faults++;
if(count<nframes)
frames[count][i]=pages[i];
for(j=0;j<=count;j++)
if(j==count)
flags[j]=1;
else
flags[j]=flags[j]+1;
count++;
else
big=0;
value=0;
for(j=0;j<nframes;j++)
if(flags[j]>=big)
{
big=flags[j];
value=j;
frames[value][i]=pages[i];
for(j=0;j<nframes;j++)
if(j==value)
flags[value]=1;
else
flags[j]=flags[j]+1;
for(j=0;j<count;j++)
printf("%d",frames[j][i]);
Output:
Enter number of pages:7
Enter number of Frames:3
Enter Page value for page 1:2
Page fault 1:1
Enter Page value for page 2:3
Page fault 2:13
Enter Page value for page 3:4
Page fault 3:134
Enter Page value for page 4:5
Page fault 4:534
Enter Page value for page 5:3
Enter Page value for page 6:6
Page fault 5:536
Enter Page value for page 7:1
Page fault 6:136
LFU
#include<stdio.h>
void print(int frameno,int frame[])
{
int j;
for(j=0;j<frameno;j++)
printf("%d\t",frame[j]);
printf("\n");
}
int main()
{
int i,j,k,n,page[50],frameno,frame[10],move=0,flag,count=0,count1[10]={0},
repindex,leastcount;
float rate;
printf("Enter the number of pages\n");
scanf("%d",&n);
printf("Enter the page reference numbers\n");
for(i=0;i<n;i++)
scanf("%d",&page[i]);
printf("Enter the number of frames\n");
scanf("%d",&frameno);
for(i=0;i<frameno;i++)
frame[i]=-1;
printf("Page reference string\tFrames\n");
for(i=0;i<n;i++)
{
printf("%d\t\t\t",page[i]);
flag=0;
for(j=0;j<frameno;j++)
{
if(page[i]==frame[j])
{
flag=1;
count1[j]++;
printf("No replacement\n");
break;
}
}
if(flag==0&&count<frameno)
{
frame[move]=page[i];
count1[move]=1;
move=(move+1)%frameno;
count++;
print(frameno,frame);
}
else if(flag==0)
{
repindex=0;
leastcount=count1[0];
for(j=1;j<frameno;j++)
{
if(count1[j]<leastcount)
{
repindex=j;
leastcount=count1[j];
}
}
frame[repindex]=page[i];
count1[repindex]=1;
count++;
print(frameno,frame);
}
}
rate=(float)count/(float)n;
printf("Number of page faults is %d\n",count);
printf("Fault rate is %f\n",rate);
return 0;
}
Output:
SHARED MEMORY
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
intshmid;
key_t key;
char *shm,*s;
key=1234;
shmid=shmget(key,SHMSIZE,IPC_CREAT|0666);
if (shmid<0)
{
perror("shmget error");
exit(1);
shm=shmat(shmid,NULL,0);
if (shm==(char *) -1)
perror("\nshmat error");
exit(1);
memcpy(shm,"HELLO WORLD",11);
s=shm;
s+=11;
*s=0;
sleep(1);
return 0;
// Client reads the string from shared memory & displays it on the screen - client.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main()
intshmid;
key_t key;
char *shm,*s;
key=1234;
shmid=shmget(key,SHMSIZE,0666);
if (shmid<0)
perror("shmget error");
exit(1);
}
shm=shmat(shmid,NULL,0);
if (shm==(char *) -1)
{ perror("\nshmat error");
exit(1); }
for (s=shm;*s!=0;s++)
printf("%c",*s);
printf("\n");
*shm = '*';
return 0;
SEMAPHORES
unionsemunsem_val;
intchild_pid;
inti,rc;
structsembufsem_op;
structtimespec delay;
system("clear");
sem_set_id=semget(IPC_PRIVATE,1,0600);
if(sem_set_id==-1)
{
perror("main!segment");
exit(1);
}
printf("semaphore set created,semaphore set id %d\n",sem_set_id);
printf("\n child process is the consumer");
printf("\n parent process is producer\n");
sem_val.val=0;
rc=semctl(sem_set_id,0,SETVAL,sem_val);
child_pid=fork();
switch(child_pid){
case -1:
perror("fork");
exit(1);
case 0:
for(i=0;i<NUM_LOOPS;i++)
{
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
printf("consumer:%d\n",i);
fflush(stdout);
sleep(3);
}
break;
default :
for(i=0;i<NUM_LOOPS;i++)
{
printf("producer : %d \n",i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
sleep(2);
if(rand()>3*(RAND_MAX / 4))
{
delay.tv_sec=0;
delay.tv_nsec=10;
nanosleep(&delay,NULL);
}
}
break;
}
return 0;
}
OUTPUT:
#include<stdio.h>
int main()
int available[3],work[5],max[5][3],allocation[5][3],need[5][3],safe[5],totalres[5];
char finish[5];
int i,j,k,totalloc=0,state,value=0;
for(i=0;i<3;i++)
scanf("%d",&totalres[i]);
for(i=0;i<5;i++)
for(j=0;j<3;j++)
scanf("%d",&max[i][j]);
for(i=0;i<5;i++)
for(j=0;j<3;j++)
scanf("%d",&allocation[i][j]);
for(i=0;i<5;i++)
for(j=0;j<3;j++)
need[i][j]=max[i][j]-allocation[i][j];
for(i=0;i<5;i++)
{
finish[i]='f';
for(i=0;i<3;i++)
totalloc=0;
for(j=0;j<5;j++)
totalloc=totalloc+allocation[j][i];
available[i]=totalres[i]-totalloc;
work[i]=available[i];
for(i=0;i<5;i++)
for(j=0;j<3;j++)
printf("%d",allocation[i][j]);
printf("\n");
for(i=0;i<5;i++)
for(j=0;j<3;j++)
printf("%d",max[i][j]);
printf("\n");
for(i=0;i<5;i++)
for(j=0;j<3;j++)
{
printf("%d",need[i][j]);
printf("\n");
for(i=0;i<3;i++)
printf("%d",available[i]);
printf("\n");
for(i=0;i<5;i++)
for(j=0;j<3;j++)
if((finish[i]=='f')&&(need[i][j]<=work[j]))
state=1;
continue;
else
state=0;
break;
if(state==1)
for(j=0;j<3;j++)
work[j]=work[j]+allocation[i][j];
finish[i]='t';
safe[value]=i;
++value;
if(i==4)
if(value==5)
break;
else
i=-1;
for(i=0;i<5;i++)
printf("P%d",safe[i]);
OUTPUT:
5
7
Enter Maximum resources for each processes
Enter process 0 Resource 1: 7
Enter process 0 Resource 2: 5
Enter process 0 Resource 3: 3
Enter process 1 Resource 1: 3
Enter process 1 Resource 2: 2
Enter process 1 Resource 3: 2
Enter process 2 Resource 1: 9
Enter process 2 Resource 2: 0
Enter process 2 Resource 3: 2
Enter process 3 Resource 1: 2
Enter process 3 Resource 2: 2
Enter process 3 Resource 3: 2
Enter process 4 Resource 1: 4
Enter process 4 Resource 2: 3
Enter process 4 Resource 3: 3
Enter number of resources allocated to each Process
Enter the resource of R1 allocated to process 0:0
Enter the resource of R2 allocated to process 0:1
Enter the resource of R3 allocated to process 0:0
Enter the resource of R1 allocated to process 1:2
Enter the resource of R2 allocated to process 1:0
Enter the resource of R3 allocated to process 1:0
Enter the resource of R1 allocated to process 2:3
Enter the resource of R2 allocated to process 2:0
Enter the resource of R3 allocated to process 2:2
Enter the resource of R1 allocated to process 3:2
Enter the resource of R2 allocated to process 3:1
Enter the resource of R3 allocated to process 3:1
Enter the resource of R1 allocated to process 4:0
Enter the resource of R2 allocated to process 40
Enter the resource of R3 allocated to process 4:2
Allocated Resources
010
200
302
211
002
Maximum Resources
753
322
902
222
433
Needed Resources
743
122
600
011
431
Available Reources332
Safe States areP1P3P4P0P2
#include<stdio.h>
void main()
int nort,nopro,avail[20],req[20][20],i,j,k,flag=0;
scanf("%d",&nort);
for(i=0;i<nort;i++)
scanf("%d",&avail[i]);
scanf("%d",&nopro);
for(j=0;j<nort;j++)
scanf("%d",&req[i][j]);
for(i=0;i<nopro;i++)
flag=0;
for(j=0;j<nort;j++)
if(req[i][j]>avail[j])
flag=1;
if(flag==1)
else
for(k=0;k<nort;k++)
avail[k]=avail[k]-req[i][k];
for(i=0;i<nort;i++)
printf("\n %d",avail[i]);
OUTPUT: