0% found this document useful (0 votes)
14 views27 pages

Os Lab

Uploaded by

asmatkhanasmat34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views27 pages

Os Lab

Uploaded by

asmatkhanasmat34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Round Robin

#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;

printf("Enter number of processes");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter process %d burst time",(i+1));

scanf("%d",&burst[i]);

printf("Enter time slice");

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;

printf("\n process burst start wait finish turnaround ");

for(i=0;i<n;i++)

printf("%7d %5d %5d %5d %4d %6d \n",(i+1),burst[i],start[i],wait[i],finish[i],turn[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;

printf("\n Average waiting time %f\n",avgwait);

printf("\n Average turnaround time %f\n",avgturn);

printf("\n Average response time %f\n",avgresp);

OUTPUT:

Enter number of processes 3


Enter process 1 burst time 24
Enter process 2 burst time 3
Enter process 3 burst time 3
Enter time slice 2
Process burst start wait finish turnaround
1 24 0 6 30 30
2 3 2 6 9 9
3 3 4 7 10 10

Average waiting time 6.333333


Average turnaround time 16.333334
Average response time 2.000000

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;

printf("Enter number of Process:");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\n Enter process %d Burst time:",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];

printf("\n Burst Start Finish Wait \n");

for(i=1;i<=n;i++)

printf("%5d %5d %6d %4d\n",burst[i],start[i],finish[i],wait[i]);

for(i=1;i<=n;i++)

totalwait=totalwait+wait[i];

totalturn=totalturn+finish[i];

avgwait=totalwait/n;
avgturn=totalturn/n;

printf("Average Waiting time %f \n",avgwait);

printf("Average Turn over time %f \n",avgturn);

OUTPUT:

Enter number of Process:3


Enter process 1 Burst time:27
Enter process 2 Burst time:1
Enter process 3 Burst time:2
Burst Start Finish Wait
1 0 1 0
2 1 3 1
27 3 30 3
Average waiting time 1.333333
Average Turn over time 11.333333

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;

printf("Enter number of Process:");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("\n Enter process %d Arrival and Burst time \n",(i+1));

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);

printf("\n Arrival Burst Start Finish Wait Turn \n");

for(i=0;i<n;i++)

printf("%7d %5d %5d %6d %4d %4d \n",arrival[i],burst[i],start[i],finish[i],wait[i],turn[i]);

printf("Average waiting time %f\n",avgwait);

printf("Average turnaround time %f\n",avgturn);

OUTPUT:

Enter number of Process: 3

Enter process 1 Arrival and Burst time 0 24

Enter process 2 Arrival and Burst time 0 3

Enter process 3 Arrival and Burst time 03

Arrival Burst Start Finish Wait Turn

0 24 0 24 0 24

0 3 24 27 24 27

0 3 27 30 27 30

Average waiting time 17.000000


Average turnaround time 27.000000

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++)

printf("\n Enter Burst time and priority of process %d",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];

printf("\n Burst Priority Start Wait Finsih \n");

for(i=1;i<=n;i++)

printf("%5d %8d %5d %4d %6d ",burst[i],pri[i],start[i],wait[i],finish[i]);

for(i=1;i<=n;i++)

totalwait=totalwait+wait[i];

totalturn=totalturn+finish[i];

}
avgwait=totalwait/n;

avgturn=totalturn/n;

printf("\n Average waiting time=%f \n",avgwait);

printf("\n Average turnaround time=%f \n",avgturn);

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

MultiPrograming with a Fixed number of Tasks

#include<stdio.h>

main()

int ms,i,ps[20],n,size,p[20],s,intr=0;

printf("Enter size of memory:");

scanf("%d",&ms);

printf("Enter memory for OS:");

scanf("%d",&s);

ms-=s;

printf("Enter no.of partitions to be divided:");

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

printf("\n process%d is blocked",p[i]);

printf("\n internal fragmentation is %d",intr);

OUTPUT:

MultiPrograming with a Variable number of Tasks

#include<stdio.h>

main()

int i,m,n,tot,s[20];

printf("Enter total memory size:");

scanf("%d",&tot);

printf("Enter no. of pages:");


scanf("%d",&n);

printf("Enter memory for OS:");

scanf("%d",&m);

for(i=0;i<n;i++)

printf("Enter size of page%d:",i+1);

scanf("%d",&s[i]);

tot=tot-m;

for(i=0;i<n;i++)

if(tot>=s[i])

printf("Allocate page %d\n",i+1);

tot=tot-s[i];

else

printf("process p%d is blocked\n",i+1);

printf("External Fragmentation is=%d",tot);

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';

printf("Enter number of Pages:");

scanf("%d",&npages);

printf("Enter number of Frames:");

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++)

printf("\n Enter page value for page %d:",(i+1));

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("\n \t\t\t\t\t Pagefault %d:",faults);


for(j=0;j<=count;j++)

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);

printf("\n Enter number of Frames:");

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++)

printf("\n Enter Page value for page %d:",(i+1));

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;

printf("\n \t\t\t\t\t Pagefault %d:",faults);

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>

#define SHMSIZE 1024

int main(intargc, char* argv[])

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;

while (*shm !='*')

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>

#define SHMSIZE 1024

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;

OUTPUT: HELLO WORLD

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:

BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE

#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;

printf("Enter Instances of each Resource");

for(i=0;i<3;i++)

scanf("%d",&totalres[i]);

printf("Enter Maximum resources for each processes");

for(i=0;i<5;i++)

for(j=0;j<3;j++)

printf("\n Enter process %d Resource %d",i,(j+1));

scanf("%d",&max[i][j]);

printf("Enter number of resources allocated to each Process");

for(i=0;i<5;i++)

for(j=0;j<3;j++)

printf("\n Enter the resource of R%d allocated to process %d",(j+1),i);

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];

printf("\n Allocated Resources \n");

for(i=0;i<5;i++)

for(j=0;j<3;j++)

printf("%d",allocation[i][j]);

printf("\n");

printf("\n Maximum Resources \n");

for(i=0;i<5;i++)

for(j=0;j<3;j++)

printf("%d",max[i][j]);

printf("\n");

printf("\n Needed Reources \n");

for(i=0;i<5;i++)

for(j=0;j<3;j++)
{

printf("%d",need[i][j]);

printf("\n");

printf("\n Available Reources");

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;

printf("\n Safe States are");

for(i=0;i<5;i++)

printf("P%d",safe[i]);

OUTPUT:

Enter Instances of each Resource 10

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

BANKERS ALGORITHM FOR DEAD LOCK PREVENTION

#include<stdio.h>

void main()

int nort,nopro,avail[20],req[20][20],i,j,k,flag=0;

printf("\n enter the no of resource types:");

scanf("%d",&nort);

printf("\n enter the no of instances of each resource type:");

for(i=0;i<nort;i++)

scanf("%d",&avail[i]);

printf("\n enter the no of processes:");

scanf("%d",&nopro);

printf("\n enter the requests of each process:");


for(i=0;i<nopro;i++)

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)

printf("\n resources for process p%d cannot be allocated to prevent deadlock",i);

else

for(k=0;k<nort;k++)

avail[k]=avail[k]-req[i][k];

printf("\n%d instances of resource type R%d are allocated to process P%d",req[i][k],k,i);

printf("\n remaining resources after allocation are");

for(i=0;i<nort;i++)

printf("\n %d",avail[i]);

OUTPUT:

You might also like