Professional Documents
Culture Documents
OPERATING SYSTEMS
LABORATORY CERTIFICATE
This is to certify that Mr.
Faculty In charge
Date of submission
1|Page
2|Page
1.Develop a c program to implement the Process system calls (fork (), exec (), wait (), create
process,terminate process)
#include<stdio.h> // prin ()
#include<stdlib.h> // exit()
#include<sys/types.h> // pid_t
#include<sys/wait.h> // wait()
= fork();
if(pid==0)
prin ("%d\n",i);
exit(0);
else
exit(EXIT_FAILURE return
0; );
}}
OUTPUT:
wai ng me
3|Page
a) FCFS
#include<stdio.h> int
main()
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
wt[0]=0;
for(i=1;i<n;i++)
wt[i]=0;
for(j=0;j<i;j++) wt[i]
+=bt[j];
//calcula ng turnaround me
for(i=0;i<n;i++)
nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
avwt/=i;
avtat/=i;
return 0;
OUTPUT:
b) SJF
#include<stdio.h> int
main()
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
4|Page
"); scanf("%d",&n); prin ("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
p[i]=i+1;
for(i=0;i<n;i++)
{ pos=i;
for(j=i+1;j<n;j+
+)
{ if(bt[j]<bt[pos])
pos=j;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
wt[0]=0;
for(i=1;i<n;i++)
{ wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
ng Time\tTurnaround Time");
for(i=0;i<n;i++)
%d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
Time=%f\n",avg_tat); return 0;
OUTPUT:
5|Page
c) Round Robin
#include<stdio.h>
int main()
for(count=0;count<n;count++)
prin ("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
me+=rt[count];
rt[count]=0;
flag=1;
else if(rt[count]>0)
me_quantum;
flag=0;
if(count==n-1) count=0;
6|Page
prin ("\nAverage Wai ng Time= %f\n",wait_ me*1.0/n); prin
return 0;
OUTPUT:
d) Priority
#include<stdio.h> int
main()
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
prin ("\nP[%d]\n",i+1);
scanf("%d",&bt[i]);
prin ("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
for(i=0;i<n;i++)
pos=i;
for(j=i+1;j<n;j++)
if(pr[j]<pr[pos])
pos=j;
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
7|Page
p[i]=p[pos];
p[pos]=temp;
wt[0]=0;
for(i=1;i<n;i++)
wt[i]=0;
for(j=0;j<i;j++) wt[i]
+=bt[j]; total+=wt[i];
Time\tTurnaround Time");
for(i=0;i<n;i++)
%d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
%d\n",avg_tat);
return 0;
OUTPUT:
8|Page
3. Develop a C program to simulate producer-consumer problem using semaphores.
#include<stdio.h> void
main()
Full"); else
scanf("%d", &produce);
buffer[in] = produce; in =
(in+1)%bufsize;
else
break;
}}}
OUTPUT:
4. Develop a C program which demonstrates inter-process communica on between a reader process and a writer process. Use mkfifo, open, read, write and
close APIs in your program.
/*Writer Process*/
#include <stdio.h>
9|Page
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
char buf[1024];
/* Reader Process*/
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#define MAX_BUF
1024
int main()
{ int fd;
buf[MAX_BUF];
0; }
OUTPUT:
10 | P a g e
#include <stdio.h>
#include <stdlib.h>
int main()
: "); scanf("%d", &p); for(i = 0; i< p; i++) completed[i] = 0; prin ("\n\nEnter the no of
resources : "); scanf("%d", &r); prin ("\n\nEnter the Max Matrix for each process : ");
&Max[i][j]);
scanf("%d", &alloc[i][j]);
+) need[i][j] = Max[i][j] -
alloc[i][j]; do
prin ("\t\t");
prin ("\n");
= 0; i < p; i++)
11 | P a g e
if(completed[i] == 0)//if not completed
process = i ; for(j
= 0; j < r; j++)
process = -1;
break;
if(process != -1)
break;
if(process != -1)
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
if(count == p)
prin (">\n");
state!!");
12 | P a g e
OUTPUT:
13 | P a g e
6. Develop a C program to simulate the following con guous memory alloca on
Techniques
a) Worst fit
#include<stdio.h> int
main()
scanf("%d", &blocks[m]);
&files[m]);
if(block_arr[n] != 1)
>= 0)
file_arr[m] = n; top
= temp;
= 1; top = 0;
14 | P a g e
{
0;
OUTPUT:
b) Best Fit
#include<stdio.h> void
main()
c int barray[20],parray[20];
scanf("%d",&b[i]);
scanf("%d",&p[i]);
for(i=1;i<=np;i++)
15 | P a g e
for(j=1;j<=nb;j++)
if(barray[j]!=1)
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
parray[i]=j;
lowest=temp;
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
prin ("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
OUTPUT:
c) First Fit
#include<stdio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
16 | P a g e
prin ("Block %d:",i);
scanf("%d",&b[i]);
scanf("%d",&f[i]);
for(i=1;i<=nf;i++)
for(j=1;j<=nb;j++)
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{ ff[i]=
j;
highest=temp;
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
prin ("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", }
OUTPUT:
17 | P a g e
7.Develop a C program to simulate page replacement algorithms
a) FIFO
#include<stdio.h> int main() { int
i,j,n,a[50],frame[10],no,k,avail,count=0; prin
NUMBER :\n");
OUTPUT:
b) LRU
#include<stdio.h> int
i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,vic m=-1;
18 | P a g e
int recent[10],lrucal[50],count=0; int lruvic m();
void main()
prin ("\
n………………………………..")
;
for(i=0;i<nor;i++)
prin ("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
lrucal[i]=0; }
for(i=0;i<10;i++)
recent[i]=0; prin
("\n");
for(i=0;i<nor;i+
+)
t",ref[i]); for(j=0;j<nof;j++)
if(frm[j]==ref[i])
flag=1;
break; }
if(flag==0)
count++;
if(count<=nof)
vic m++;
else
19 | P a g e
pf++; frm[vic
m]=ref[i];
for(j=0;j<nof;j++)
prin ("%4d",frm[j]);
recent[ref[i]]=i;
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
temp1=frm[i];
lrucal[i]=recent[temp1];
temp2=lrucal[0]; for(j=1;j<nof;j+
+)
if(temp2>lrucal[j])
temp2=lrucal[j];
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i]
) return i; return 0;
OUTPUT:
20 | P a g e
8. Simulate following File Organiza on Techniques
a) Single level directory
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
prin ("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your choice
-- ");
scanf("%d",&ch);
switch(ch)
scanf("%s",dir.fname[dir.fcnt]); dir.fcnt++;
if(strcmp(f, dir.fname[i])==0)
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
scanf("%s",f); for(i=0;i<dir.fcnt;i++)
if(strcmp(f, dir.fname[i])==0)
21 | P a g e
if(dir.fcnt==0) prin ("\
for(i=0;i<dir.fcnt;i++) prin
("\t%s",dir.fname[i]);
break; default:
exit(0);
OUTPUT:
b) Two-Level Directory
#include<string.h>
#include<stdlib.h>
#include<stdio.h> struct
char dname[10],fname[10][10];
int
fcnt; }dir[10
]; void
main()
int i,ch,dcnt,k;
char f[30],
22 | P a g e
d[30]; dcnt=0;
while(1)
scanf("%d",&ch); switch(ch)
scanf("%s",d); for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
scanf("%s",dir[i].fname[dir[i].fcnt]
if(i==dcnt)
scanf("%s",d); for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
if(strcmp(f, dir[i].fname[k])==0)
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]
); goto jmp;
jmp;
23 | P a g e
prin ("Directory %s not found",d); jmp : break;
scanf("%s",d); for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
scanf("%s",f); for(k=0;k<dir[i].fcnt;k+
+)
if(strcmp(f, dir[i].fname[k])==0)
jmp1;
jmp1;
prin ("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
prin ("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++) prin
("\t%s",dir[i].fname[k]);
break; default:exit(0);
OUTPUT:
24 | P a g e
9. Develop a C program to simulate the Linked file alloca on strategies.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
scanf("%d",&a);
f[a]=1;
for(j=st;j<(st+k);j++)
if(f[j]==0)
n",j,f[j]);
else
OUTPUT:
25 | P a g e
26 | P a g e
10. Develop a C program to simulate SCAN disk scheduling algorithm.
#include <stdio.h>
int downtrack;
int down; }
kate[50]; int
(a > b) return a - b;
return b - a;
request[j + 1] = temp;
} j
= 0;
i = 0;
kate[j].down = request[i];
j++; i++;
downtrack = j; i++; j
= 0; while (i < n)
{ kate[j].up =
uptrack = j;
int i;
27 | P a g e
seekcount + dist(head, kate[i].up); head =
kate[i].up;
int main(){
int n, i;
OUTPUT:
28 | P a g e