Os Lab
Os Lab
Os Lab
OPERATING SYSTEMS
LAB
2
CERTIFICATE
TABLE OF
CONTENTS
S.No Name of the program Page No
1. Write a program to demonstrate 4
open(),close(),read(),write() system calls
2. Write a program to demonstrate fork() system call 6
3. Write a program to demonstrate multiple fork() 7
4. Write a program to demonstrate pipe 9
5. Write a program for FCFS scheduling algorithm 11
6. Write a program for SJF-non preemptive algorithm 13
7. Write a program for SJF-preemptive algorithm 15
8. Write a program for priority scheduling-non 17
preemptive algorithm
9. Write a program for priority scheduling-preemptive 19
algorithm
10. Write a program for round robin scheduling algorithm 21
11. Write a program to demonstrate first fit, best fit, 24
worst fit
12. Write a program for FIFO page replacement algorithm 29
13. Write a program for optimal page replacement 31
algorithm
14. Write a program for LRU page replacement algorithm 34
15. Write a program to demonstrate producer-consumer 39
problem
16. Write a program to demonstrate readers-writers 43
problem
17. Write a program to demonstrate dining-philosophers 47
problem
4
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
static char message[]="Hello world";
int main()
{
int fd;
char *buffer[80];
fd=open("datafile.dat",O_RDWR|O_CREAT|O_EXCL,S_IREAD|S_IWRITE);
if(fd!=-1)
{
printf("file is opened for read/write access\n");
write(fd,message,sizeof(message));
close(fd);
fd=open("datafile.dat",O_RDWR,S_IREAD|S_IWRITE);
if(read(fd,buffer,sizeof(message))==sizeof(message))
printf("\" %s\" was written to datafile.dat\n",buffer);
else
printf("*** error reading datafile.dat ***");
close(fd);
}
else
{
printf("datafile.dat already exists\n");
fd=open("datafile.dat",O_RDWR,S_IREAD|S_IWRITE);
if(read(fd,buffer,sizeof(message))==sizeof(message))
printf("\" %s\" was written to datafile.dat\n",buffer);
else
printf("*** error reading datafile.dat ***");
close(fd);
}
5
exit(0);
}
OUTPUT:
#include<stdio.h>
#include<unistd.h>
int main(void)
{
printf("Hello World!\n");
fork();
printf("I am after forking\n");
printf("\t I am process %d \n",getpid());
}
OUTPUT:
Hello World!
I am after forking
I am process 11887
I am after forking
I am process 11886
7
#include<stdio.h>
#include<unistd.h>
int main(void)
{
printf("Here i'm just before 1st forking statement\n");
fork();
printf("Here i'm just after 1st forking statement\n");
fork();
printf("Here i'm just after 2nd forking statement\n");
fork();
printf("Here i'm just after 3rd forking statement\n");
printf("\tHello world from process %d\n",getpid());
}
8
OUTPUT:
OUTPUT:
child read hello..
(b) Child writing, Parent reading
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define READ 0
#define WRITE 1
char *phrase="Welcome to OS lab";
main()
{
int fd[2],bytesread;
char message[100];
pipe(fd);
if(fork()==0)
{
close(fd[READ]);
write(fd[WRITE],phrase,strlen(phrase)+1);
close(fd[WRITE]);
}
else
{
close(fd[WRITE]);
bytesread=read(fd[READ],message,100);
printf("Read %d bytes: %s\n",bytesread,message);
close(fd[READ]);
}
}
OUTPUT:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,w[15],a[15],t[15],p[15],i,b[15],sumw=0,sumt=0;
float avgw,avgt;
printf("enter the no. of processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter each process burst time: ");
scanf("%d",&b[i]);
printf("enter each process arrival time: ");
scanf("%d",&a[i]);
}
w[0]=0;
for(i=1;i<n;i++)
{
w[i]=w[i-1]+b[i-1];
}
for(i=0;i<n;i++)
{
t[i]=b[i]+w[i];
sumw=sumw+w[i];
sumt=sumt+t[i];
}
avgw=sumw/n;
avgt=sumt/n;
printf("the avg waiting time is: %f\n",avgw);
printf("the avg turnaround time is: %f\n",avgt);
return 0;
}
12
OUTPUT:
#include<stdio.h>
int main()
{
int p[15],bt[15],wt[15],i,wait,tat,t,n;
float awt,atat;
wait=tat=0;
int j,temp;
printf("enter no of processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
wt[i]=0;
printf("enter process %d burst time: ",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
printf("\tProcess\tburst time\twaiting time\tturnaround time\n");
for(i=0;i<n;i++)
{
t=wt[i]+bt[i];
printf("\t %d \t %d \t %d \t %d \n",p[i],bt[i],wt[i],t);
14
wt[i+1]=wt[i]+bt[i];
wait=wait+wt[i];
tat=tat+wt[i]+bt[i];
}
awt=wait/(float)n;
atat=tat/(float)n;
printf("average waiting time: %f\n",awt);
printf("average turnaround time: %f\n",atat);
}
OUTPUT:
enter no of processes:
3
enter process 1 burst time:
20
enter process 2 burst time:
30
enter process 3 burst time:
10
Process burst time waiting time turnaround time
3 10 0 10
1 20 10 30
2 30 30 60
average waiting time:
13.333333
average turnaround time:
33.333332
15
#include<stdio.h>
int main()
{
int i,j,n,time,sum_wait=0,sum_turnaround=0,smallest;
int at[10],bt[10],rt[10],remain; //rt = remaining Time
printf("Enter no of Processes : ");
scanf("%d",&n);
remain=n;
for(i=0;i<n;i++)
{
printf("Enter arrival time, burst time for process p%d :",i+1);
scanf("%d",&at[i]);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
bt[9]=9999;
printf("\n\nProcess\t| waiting time\t| turnaround time\n");
for(time=0;remain!=0;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && bt[i]<bt[smallest] && rt[i]>0)
{
smallest=i;
}
}
rt[smallest]--;
if(rt[smallest]==0)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",smallest+1,time+1-at[smallest]-bt[smallest],time+1-
at[smallest]);
sum_wait+=time+1-at[smallest]-bt[smallest];
sum_turnaround+=time+1-at[smallest];
}
}
16
OUTPUT:
Enter no of Processes :
5
Enter arrival time, burst time for process p1 :
0
10
Enter arrival time, burst time for process p2 :
1
3
Enter arrival time, burst time for process p3 :
2
2
Enter arrival time, burst time for process p4 :
3
4
Enter arrival time, burst time for process p5 :
4
5
#include<stdio.h>
int main()
{
int n,i,j,w[15],t[15],pr[15],b[15],b1[15],p[15],pn[15],sumw=0,sumt=0,temp;
float avgw,avgt;
printf("enter the no. of processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process %d burst time and priority: ",i+1);
scanf("%d %d",&b[i],&pr[i]);
p[i]=pr[i];
}
w[0]=0; j=0;
while(j<n)
{
for(i=1;i<=n;i++)
{
if(pr[j]==i)
{
b1[i-1]=b[j];
pn[i-1]=j;
p[i-1]=i;
}
}
j++;
}
printf("Process\tburst time\tpriority\twaiting time\tturnaround time\n");
for(i=0;i<n;i++)
{
w[i+1]=w[i]+b1[i];
t[i]=b1[i]+w[i];
sumw=sumw+w[i];
sumt=sumt+t[i];
printf(" %d \t %d \t %d \t %d \t %d \n",pn[i]+1,b1[i],p[i],w[i],t[i]);
18
}
avgw=sumw/(float)n;
avgt=sumt/(float)n;
printf("the avg waiting time is: %f\n",avgw);
printf("the avg turnaround time is: %f\n",avgt);
return 0;
}
OUTPUT:
#include<stdio.h>
int main()
{
int i,j,n,time,sum_wait=0,sum_turnaround=0,smallest;
int at[10],bt[10],pt[10],rt[10],remain; //rt = remaining Time
printf("Enter no of Processes : ");
scanf("%d",&n);
remain=n;
for(i=0;i<n;i++)
{
printf("Enter arrival time, burst time and priority for process p%d :",i+1);
scanf("%d",&at[i]);
scanf("%d",&bt[i]);
scanf("%d",&pt[i]);
rt[i]=bt[i];
}
pt[9]=11;
printf("\n\nProcess\t| waiting time\t| turnaround time\n");
for(time=0;remain!=0;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && pt[i]<pt[smallest] && rt[i]>0)
{
smallest=i;
}
}
rt[smallest]--;
if(rt[smallest]==0)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",smallest+1,time+1-at[smallest]-bt[smallest],time+1-
at[smallest]);
sum_wait+=time+1 - at[smallest] - bt[smallest];
sum_turnaround+=time+1 - at[smallest];
}
}
20
OUTPUT:
Enter no of Processes :
5
Enter arrival time, burst time and priority for process p1 :
0
10
3
Enter arrival time, burst time and priority for process p2 :
1
1
1
Enter arrival time, burst time and priority for process p3 :
2
2
4
Enter arrival time, burst time and priority for process p4 :
3
1
5
Enter arrival time, burst time and priority for process p5 :
4
5
2
#include<stdio.h>
int main()
{
int i,j,n,time,remain,flag=0,ts;
int sum_wait=0,sum_turnaround=0,at[10],bt[10],rt[10];
printf("Enter no of Processes : ");
scanf("%d",&n);
remain=n;
for(i=0;i<n;i++)
{
printf("Enter arrival time and burst time for Process P%d :",i+1);
scanf("%d",&at[i]);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf("Enter time slice :");
scanf("%d",&ts);
printf("\n\nProcess\t|Turnaround time|waiting time\n\n");
for(time=0,i=0;remain!=0;)
{
if(rt[i]<=ts && rt[i]>0)
{
time+=rt[i];
rt[i]=0;
flag=1;
}
else if(rt[i]>0)
22
{
rt[i]-=ts;
time+=ts;
}
if(rt[i]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
sum_wait+=time-at[i]-bt[i];
sum_turnaround+=time-at[i];
flag=0;
}
if(i==n-1)
i=0;
else if(at[i+1]<=time)
i++;
else
i=0;
}
printf("\nAvg sum_wait = %f\n",sum_wait*1.0/n);
printf("Avg sum_turnaround = %f\n",sum_turnaround*1.0/n);
return 0;
}
OUTPUT:
Enter no of Processes :
23
4
Enter arrival time and burst time for Process P1 :
0
6
Enter arrival time and burst time for Process P2 :
0
3
Enter arrival time and burst time for Process P3 :
0
1
Enter arrival time and burst time for Process P4 :
0
7
Enter time slice :
1
P[3] | 3 | 2
P[2] | 9 | 6
P[1] | 15 | 9
P[4] | 17 | 10
11. WAP TO DEMONSTRATE FIRST FIT, BEST FIT AND WORST FIT.
24
#include<stdio.h>
int main()
{
int i,j,temp,b[10],c[10],d[10],arr,n,ch,a;
printf("\t\t FIRST FIT, BEST FIT, WORST FIT\n");
printf("Enter the size of no. of blocks:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the size of %d block:",i);
scanf("%d",&b[i]);
d[i]=c[i]=b[i];
}
printf("\nEnter the size of Arriving block:");
scanf("%d",&arr);
printf("\n1.First fit\n2.Best fit\n3.Worst fit\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(d[i]>=d[j])
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
if(arr<=d[n]) {
for(i=1;i<=n;i++)
{
if(b[i]>=arr)
{
printf("Arriving block is allocated to block %d.\n",i);
break;
}
25
else
continue;
}}
else printf("the arriving block cannot be allocated.\n");
break;
case 2:
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(b[i]>=b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
if(arr<=b[n]){
for(i=1;i<=n;i++)
{
if(b[i]>=arr)
{
a=b[i];
break;
}
else
continue;
}
for(i=1;i<=n;i++)
{
if(c[i]==a)
{
printf("Arriving block is allocated to block %d.\n",i);
}
}}
else printf("the arriving block cannot be allocated.\n");
break;
case 3:
for(i=1;i<=n;i++)
26
{
for(j=i+1;j<=n;j++)
{
if(b[i]>=b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
if(arr<=b[n]){
for(i=n;i>=1;i--)
{
if(b[i]>=arr)
{
a=b[i];
break;
}
else
continue;
}
for(i=1;i<=n;i++)
{
if(c[i]==a)
{
printf("Arriving block is allocated to block %d.\n",i);
}
}}
else printf("the arriving block cannot be alocated.\n");
break;
default:
printf("Enter the valid choice:");
}
return 0;
}
OUTPUT:
1. First fit
2 .Best fit
3. Worst fit
Enter your choice:
1
Arriving block is allocated to block 1.
1. First fit
2. Best fit
3. Worst fit
28
1. First fit
2. Best fit
3. Worst fit
Enter your choice:
3
Arriving block is allocated to block 3.
#include<stdio.h>
int main()
{
int i,j,n,ref[50],frame[10],fno,k,avail,count=0;
printf("enter total no of pages in reference string: ");
scanf("%d",&n);
printf("enter the reference string:\n");
for(i=1;i<=n;i++)
scanf("%d",&ref[i]);
printf("enter no of frames: ");
scanf("%d",&fno);
for(i=0;i<fno;i++)
frame[i]=-1;
j=0;
printf("ref string | page frames\n");
printf(" | f[1] f[2] f[3]\n");
for(i=1;i<=n;i++)
{
printf(" %d ",ref[i]);
avail=0;
for(k=0;k<fno;k++)
{
if(frame[k]==ref[i])
avail=1;
}
if(avail==0)
{
frame[j]=ref[i];
j=(j+1)%fno;
count++;
for(k=0;k<fno;k++)
printf("%d ",frame[k]);
}
printf("\n");
}
printf("no of page faults: %d\n",count);
return 0;
30
OUTPUT:
no of page faults: 15
#include<stdio.h>
int main()
{
int fr[10],p[50],i,j,n;
int max,found=0,lg[10],index,k,l,flag1=0,flag2=0,pf=0,frsize;
printf("total no of pages in reference string: ");
scanf("%d",&n);
printf("enter reference string:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("no of frames: ");
scanf("%d",&frsize);
for(i=0;i<frsize;i++)
{
fr[i]=-1;
}
for(j=0;j<n;j++)
{
flag1=0;flag2=0;
for(i=0;i<frsize;i++)
{
if(fr[i]==p[j])
{
flag1=1;flag2=1;break;
}
}
if(flag1==0)
{
for(i=0;i<frsize;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
pf++;
break;
}
}
}
32
if(flag2==0)
{
for(i=0;i<frsize;i++)
{lg[i]=0;}
for(i=0;i<frsize;i++)
{
for(k=j+1;k<n;k++)
{
if(fr[i]==p[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<frsize;i++)
{
if(lg[i]==0)
{
index=i;found=1;break;
}
}
if(found==0)
{
max=lg[0];
index=0;
for(i=1;i<frsize;i++)
{
if(max<lg[i])
{
max=lg[i];index=i;
}
}
}
fr[index]=p[j];
pf++;
}
printf("\n");
for(i=0;i<frsize;i++)
33
printf("\t%d",fr[i]);
}
printf("\nno of page faults:%d\n",pf);
return 0;
}
OUTPUT:
7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 1
2 0 3
2 0 3
2 4 3
2 4 3
2 4 3
2 0 3
2 0 3
2 0 3
2 0 1
2 0 1
2 0 1
2 0 1
7 0 1
7 0 1
7 0 1
no of page faults: 9
#include<stdio.h>
int main()
{
int fr[10],p[50],i,j,n;
int max,found=0,lg[10],index,k,l,flag1=0,flag2=0,pf=0,frsize;
printf("total no of pages in reference string: ");
scanf("%d",&n);
printf("enter reference string:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("no of frames: ");
scanf("%d",&frsize);
for(i=0;i<frsize;i++)
{
fr[i]=-1;
}
for(j=0;j<n;j++)
{
flag1=0;flag2=0;
for(i=0;i<frsize;i++)
{
if(fr[i]==p[j])
{
flag1=1;flag2=1;break;
}
}
if(flag1==0)
35
{
for(i=0;i<frsize;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
pf++;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<frsize;i++)
{lg[i]=0;}
for(i=0;i<frsize;i++)
{
for(k=j-1;k>=0;k--)
{
if(fr[i]==p[k])
{
lg[i]=k-j;
break;
}
}
}
36
found=0;
for(i=0;i<frsize;i++)
{
if(lg[i]==0)
{
index=i;found=1;break;
}
}
if(found==0)
{
max=lg[0];
index=0;
for(i=1;i<frsize;i++)
{
if(max>lg[i])
{
max=lg[i];index=i;
}
}
}
fr[index]=p[j];
pf++;
}
printf("\n");
for(i=0;i<frsize;i++)
printf("\t%d",fr[i]);
}
37
OUTPUT:
1 0 2
1 0 7
1 0 7
1 0 7
no of page faults: 12
#include<stdio.h>
#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<semaphore.h>
sem_t empty,full,mutex;
char buf[10];
int i;
//printf("inside producer\n");
for(i=0;i<10;i++)
sem_wait(&empty);
sem_wait(&mutex);
buf[i]=i;
sem_post(&mutex);
sem_post(&full);
sleep(1);
pthread_exit("producer\n");
int j;
//printf("inside consumer\n");
for(j=0;j<10;j++)
sem_wait(&full);
sem_wait(&mutex);
j=buf[j];
sem_post(&mutex);
sem_post(&empty);
sleep(5);
pthread_exit("consumer\n");
int main()
pthread_t pid1,pid2;
sem_init(&empty,0,10);
sem_init(&full,0,0);
sem_init(&mutex,1,1);
void *status;
pthread_create(&pid1,NULL,producer,NULL);
pthread_create(&pid2,NULL,consumer,NULL);
41
pthread_join(pid1,&status);
pthread_join(pid2,&status);
return 0;
COMPILE:
OUTPUT:
item produced is 0
item produced is 1
item produced is 2
item produced is 3
item produced is 4
item produced is 5
item produced is 6
item produced is 7
item produced is 8
item produced is 9
42
ctrl + z
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
sem_t mutex;
sem_t S[N];
void take_fork(int);
void put_fork(int);
void test(int);
int state[N];
int phil_num[N]={0,1,2,3,4};
int main()
int i;
pthread_t thread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
44
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
printf("Philosopher %d is thinking\n",i+1);
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
while(1)
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
sem_wait(&mutex);
state[ph_num] = HUNGRY;
printf("Philosopher %d is Hungry\n",ph_num+1);
test(ph_num);
sem_post(&mutex);
45
sem_wait(&S[ph_num]);
sleep(1);
state[ph_num] = EATING;
sleep(2);
printf("Philosopher %d is Eating\n",ph_num+1);
sem_post(&S[ph_num]);
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("Philosopher %d is thinking\n",ph_num+1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
46
COMPILE:
OUTPUT:
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 5 is thinking
Philosopher 1 is Hungry
Philosopher 1 is Eating
Philosopher 2 is Hungry
Philosopher 3 is Hungry
Philosopher 3 is Eating
Philosopher 4 is Hungry
Philosopher 5 is Hungry
Philosopher 1 is thinking
ctrl + z
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
int data=0,rdcnt=0;
sem_t mutex,writeblock;
sem_wait(&mutex);
rdcnt++;
if(rdcnt==1)
sem_wait(&writeblock);
if(rdcnt==1)
sem_post(&writeblock);
sem_post(&mutex);
sem_wait(&writeblock);
data+=5;
sem_post(&writeblock);
int main()
int no,i,ir=0,iw=0,ch;
sem_init(&mutex,0,1);
sem_init(&writeblock,0,1);
scanf("%d",&no);
pthread_t r[no],w[no];
do
scanf("%d",&ch);
switch(ch)
{
49
pthread_join(r[ir++],NULL);
break;
pthread_join(w[iw++],NULL);
break;
}while(ch!=3);
sem_destroy(&mutex);
sem_destroy(&writeblock);
COMPILE:
OUTPUT :
1.Reader
2.Writer
3.terminate
Your choice : 1
Reader 0 is executing
1.Reader
2.Writer
3.terminate
Your choice : 2
Writer 0 is executing
1.Reader
2.Writer
3.terminate
Your choice : 3