0% found this document useful (0 votes)
3 views31 pages

C Programs for OS Lab Exercises

Uploaded by

aryaratna74
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)
3 views31 pages

C Programs for OS Lab Exercises

Uploaded by

aryaratna74
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

OPERATING SYSTEM LAB (BCS303)

Program1
Develop a c program to implement the Process system calls (fork (), exec(), wait(),
create process, terminate process)
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/
types.h>
int main()
{
pid_tpid=fork();
if(pid<0)
{
fprintf(stderr,"Forkfailed");
exit(-1);
}
elseif(pid==0)
{
execlp("/bin/ls","ls",NULL);
}
else
{
wait(NULL);
printf("Childprocesscompplete\n");
exit(0);
}
return0;}
Output
[thiru@home~]$vipgm1.c
[thiru@home~]
$ccpgm1.c [thiru@home
~]$ ./[Link]
[Link] Music pgm4a.c Pictures
Templates
[Link]
Child process compplete
[thiru@home~]$

Program2
Simulate the following CPU scheduling algorithms to find turnaround time and
waiting time a) FCFS b) SJF c) Round Robin d) Priority.
A)FCFS
Source code
#include<stdio.h>

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

#include<conio.h>
main()
{
Int bt[20],wt[20],tat[20],i,n;
float wtavg, tatavg;
printf("\nEnter the number of processes--");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process%d--",i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0]=tatavg=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg=wtavg+wt[i];
tatavg=tatavg+tat[i];
}
printf("\tPROCESS\tBURSTTIME\tWAITINGTIME\tTURNAROUNDTIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d\t\t%d\t\t%d\t\t%d",i,bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}

Output
Enter the number of processes -- 3
EnterBurstTimeforProcess0--24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3

PROCESSBURSTTIMEWAITINGTIMETURNAROUNDTIME

P0 24 0
24
P1 3 24 27
P2 3 27 30
Average Waiting Time -- 17.000000
AverageTurnaroundTime--27.000000

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

B)SJF

#include<stdio.h
>
#include<conio.h
> main()
{
intp[20],bt[20],wt[20],tat[20],i,k,n,temp;floatwtavg, tatavg;

printf("\nEnter the number of processes--");


scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process%d--",i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0]=wtavg= 0;
tat[0]=tatavg=bt[0];for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg=wtavg+wt[i];
tatavg=tatavg+tat[i];
}
printf("\n\tPROCESS\tBURSTTIME\tWAITINGTIME\tTURNAROUNDTIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d\t\t %d\t\t %d \t\t %d", p[i], bt[i], wt[i],tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverageTurnaroundTime--%f",tatavg/n);getch();}

Output
Enter the number ofprocesses -- 4
EnterBurstTimeforProcess0--6
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

EnterBurstTimeforProcess1--8
EnterBurstTimeforProcess2--7
EnterBurstTimeforProcess3--3
PROCESSBURST TIMEWAITING TIMETURNAROUNDTIME P3303

P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000
AverageTurnaroundTime--13.000000

C) ROUNDROBIN

#include<stdio.h>
main()
{
int
i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
printf("Enter the no of processes--");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time forp rocess%d--",i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice--");
scanf("%d",&t);
max=bu[0]; for(i=1;i<n;i
++)
if(max<bu[i])max=bu[i]
; for(j=0;j<(max/t)+1;j+
+) for(i=0;i<n;i++) if(bu[
i]!=0)
if(bu[i]<=t)
{ tat[i]=temp+bu[i];
temp=temp+bu[i]; bu[
i]=0;
}
else
{ bu[i]=bu[i]-
t;
temp=temp+
t;
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

}
for(i=0;i<n;i++)
{ wa[i]=tat[i]-
ct[i];att+=tat[i];
awt+=wa[i];}
printf("\nThe Average Turnaround time is--%f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\n\tPROCESS\tBURSTTIME\tWAITINGTIME\tTURNAROUNDTIME\n");
for(i=0;i<n;i++)
printf("\t%d\t%d \t\t %d\t\t%d\n",i+1,ct[i],wa[i],tat[i]);
}

Output
Enter the no of processes --3
EnterBurstTimeforprocess1--24
Enter Burst Time for process 2 -- 3
Enter Burst Time for process 3 -- 3
Enter the size of time slice -- 3
The Average Turnaround time is--15.000000
The Average Waiting time is -- 5.000000
PROCESS BURSTTIME WAITINGTIME TURNAROUNDTIME
1 24 30
6
2 3 3
6
3 3 6
9

D)PRIORITY

#include<stdio.h>
main()
{
intp[20],bt[20],pri[20],wt[20],tat[20],i,k,n,temp;floatwtavg,
tatavg;
printf("Enter the number of processes---");
scanf("%d",&n);
for(i=0;i<n;i++)
{ p[i] = i;
printf("Enter the Burst Time & Priority of Process%d ---
",i); scanf("%d%d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k+
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

+) if(pri[i] > pri[k])


{ temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wtavg = wt[0] = 0;
tatavg=tat[0]=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg=wtavg+wt[i];
tatavg=tatavg+tat[i];
}
printf("\nPROCESS\t\tPRIORITY\tBURSTTIME\tWAITINGTIME\tTURNAROUND
TIME");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverageTurnaround Time is --- %f",tatavg/n);}

Output

Enter the number of processes---5


EntertheBurstTime&PriorityofProcess0 ---103
Enter the Burst Time & Priority of Process 1 --- 1 1
Enter the Burst Time & Priority of Process 2 --- 2 4
Enter the Burst Time & Priority of Process 3 --- 1 5
Enter the Burst Time & Priority of Process 4 --- 5 2
PROCESS PRIORITY BURSTTIMEWAITINGTIME TURNAROUN
TIME D
1 1 0 1
1
4 2 5 1 6
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19
Average Waiting Time is --- 8.200000
Average Turnaround Time is---12.000000
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

Program3
Develop a C program to simulate producer-consumer problem using semaphores.
#include<stdio.h>
#include<stdlib.h>
void producer();
void consumer();
int wait(int);
int signal(int);
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n=1;
clrscr();
printf("\[Link]\[Link]\[Link]");
while(n!=3)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case1:if((mutex==1)&&(empty!=0))
{
producer();
}
else
printf("Bufferisfull!!");
break;
case2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Bufferisempty!!");
break;
case 3:
printf("press 3 to exit\n");
scanf("%d",&n);

}
}
return0;
}

int wait(int s)
{

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

return(--s);
}

intsignal(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
Enteryourchoice:2
Buffer is empty!!
Enter your choice:1
Producer produces the
item1 Enter your choice:1
Producer produces the
item2 Enter your choice:2
Consumer consumes item
2 Enter your choice:2
Consumer consumes item
1 Enter your choice:2
Buffer is empty!!
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

Enteryourchoice:3

Program4
Develop a C program which demonstrates interprocess communication between a
reader process and a writer process. Use mkfifo, open, read, write and close APIs in
your program.
/*Writer Process*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/
stat.h>
#include<sys/
types.h>
#include<unistd.h>
int main()
{
intfd;
charbuf[1024];
/*createtheFIFO(namedpipe)*/
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
printf("RunReaderprocesstoreadtheFIFOFile\n");
fd = open(myfifo, O_WRONLY);
write(fd,"Hi",sizeof("Hi"));
/*write"Hi"totheFIFO*/
close(fd);
unlink(myfifo);/*removetheFIFO*/
return 0;
}
/* Reader Process */
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#defineMAX_BUF1024
int main()
{
intfd;
/*AtempFIFOfileisnot createdinreader*/
char *myfifo = "/tmp/myfifo";
charbuf[MAX_BUF];
/*open,read,anddisplaythe message fromtheFIFO*/
fd = open(myfifo, O_RDONLY);

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

read(fd, buf, MAX_BUF);


printf("Writer:%s\n",buf);
close(fd);
return0;
}

Output

OR
//WRITERPROCESS
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/
stat.h>
#include<sys/
types.h> #include
<unistd.h>

intmain()
{
intfd;
char*myfifo="/tmp/myfifo";
mkfifo(myfifo, 0666);
chararr1[80],arr2[80];
while (1)
{
fd=open(myfifo,O_WRONLY);
fgets(arr2, 80, stdin);
write(fd,arr2,strlen(arr2)+1);
close(fd);

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

fd=open(myfifo,O_RDONLY);
read(fd, arr1, sizeof(arr1));
printf("User2: %s\n", arr1);
close(fd);
}
return0;}

//READERPROCESS
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/
stat.h>
#include<sys/
types.h>
#include<unistd.h>
int main()
{
intfd1;
char*myfifo="/tmp/myfifo";
mkfifo(myfifo, 0666);
charstr1[80],str2[80];
while (1)
{

fd1=open(myfifo,O_RDONLY);
read(fd1, str1, 80);
printf("User1:%s\n",str1);
close(fd1);
fd1=open(myfifo,O_WRONLY);
fgets(str2, 80, stdin);
write(fd1,str2,strlen(str2)+1);
close(fd1);
}
return0;
}

Output

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

Program5
Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.
#include<stdio.h
>
#include<conio.h
>
#include<string.h
>int main()
{
int alloc[10][10],max[10][10];
intavail[10],work[10],total[10];
int i,j,k,n,need[10][10];
intm;
intcount=0,c=0;
char finish[10];

printf("Enter the no. of processes and resources:");


scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
finish[i]='n';
printf("Enter the claim matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);
printf("Enter the allocation matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&alloc[i][j]);
printf("Resource
vector:"); for(i=0;i<m;i+
+) scanf("%d",&total[i]);
for(i=0;i<m;i++)
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

avail[i]=0;for(i=0;i<n;i++)
for(j=0;j<m;j++) avail[j]
+=alloc[i][j];
for(i=0;i<m;i++)
work[i]=avail[i];
for(j=0;j<m;j++)
work[j]=total[j]-work[j];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j]=max[i][j]-alloc[i][j]; A:
for(i=0;i<n;i++)
{ c=
0;
for(j=0;j<m;j++) if((need[i]
[j]<=work[j])&&(finish[i]=='n')) c++;
if(c==m)
{
printf("All the resources can be allocated toProcess%d",i+1);
printf("\n\nAvailable resources are:");
for(k=0;k<m;k++)
{
work[k]+=alloc[i][k];
printf("%4d",work[k]);
}
printf("\n");
finish[i]='y';
printf("\nProcess%dexecuted?:%c\n",i+1,finish[i]);
count++;
}
}
if(count!=n)
goto A;
else
printf("\n System is in safe mode");
printf("\nThe given state is safe state");
getch();
}

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

Output

Enter the no. of processes and resources:4 3


Enter the claim matrix:
322
613
314
422
Enter the allocation matrix:
100
612
211
002
Resource vector:936
All the resources can be allocated toProcess2
Available resources are: 6 2 3
Process2 executed?:y
All the resources can be allocated to Process3
Available resources are: 8 3 4
Process3 executed?:y
All the resources can be allocated to Process4
Available resources are: 8 3 6
Process4 executed?:y
All the resources can be allocated toProcess1
Available resources are: 9 3 6
Process 1 executed?:y
System is in safe mode
The given state is safe
state

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

Program6
Develop a C program to simulate the following contiguous memory allocation
Techniques:
a) Worst fit b) Best fit c) First fit.
a)Worst fit
#include<stdio.h>
#include<conio.h>#d
efine max 25
intmain()
{
int
frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme-
WorstFit"); printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block%d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files:-\n");
for(i=1;i<=nf;i++)
{
printf("File%d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{

for(j=1;j<=nb;j++)
{
if(bf[j]!=1)//if bf[j]is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
ff[i]=j;
highest=temp;
}
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:
\tFragement"); for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

Output

Memory Management Scheme-WorstFit


Enter the number of blocks:3
Enter the number of
files:2 Enter the size of the
blocks:- Block 1:5
Block2:2
Block3:7
Enter the size of the files:-
File 1:1
File2:4

File_n File_size Block_no Block_siz Fragem


o: : : e: ent
1 14 0 6
2 44 0 0

a)Bestfit

#include<stdio.h>i
nt main()
{
int
fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
printf("\n\t\t\tMemory Management Scheme-
BestFit"); printf("\nEnter the number of blocks:");
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

scanf("%d",&nb);
printf("Enter the number of processes:");
scanf("%d",&np);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Blockno.%d:",i);
scanf("%d",&b[i]);
}
printf("\nEnter the size of the processes:-\n");
for(i=1;i<=np;i++)
{
printf("Processno.%d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=np;i++)
{
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]=lowes
t;
barray[parray[i]]=1
; lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment")
; for(i=1;i<=np && parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragme
nt[i]);
}

Output
Memory Management Scheme-BestFit

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

Enter the number of blocks:3


Enterthenumberofprocesses:
2 Enter the size of the
blocks:- Block no.1:5
Blockno.2:2
Blockno.3:7
Enter the size of the processes:-
Process no.1:1
Processno.2:4
Process_noProcess_sizeBlock_noBlock_sizeFragment
1 1 2 2 1
2 4 1 5 1

c) First fit

#include<stdio.h>i
nt main()
{
intbsize[10],psize[10],bno,pno,flags[10],allocation[10],i, j;

for(i=0;i<10;i++)
{
flags[i]=0;
allocation[i]=-1;
}
printf("Enter no. of blocks:");
scanf("%d", &bno);
printf("\nEnter size of each
block:"); for(i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
printf("\nEnter no. of processes:");
scanf("%d", &pno);
printf("\nEnter size of each process:");
for(i = 0; i < pno; i++)
scanf("%d",&psize[i]);
for(i = 0; i < pno; i++)//allocationasperfirstfit for(j
= 0; j < bno; j++)
if(flags[j]==0&&bsize[j]>=psize[i])
{
allocation[j]=i;
flags[j] = 1;
break;
}
//display allocation details
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

printf("\nBlockno.\tsize\t\tprocessno.
\t\tsize"); for(i = 0; i < bno; i++)
{
printf("\n%d\t\t%d\t\t",i+1,bsize[i]);
if(flags[i] == 1)
printf("%d\t\t\t%d",allocation[i]
+1,psize[allocation[i]]); else
printf("Not allocated");
}
}

Output

Enter no. of blocks: 3


Enter size of each
block: 5
2
7
Enter no. of processes:
2 Enter size of each
process: 1
4

Blockno. size processno. si


z
e
15 1 1
22 Not allocated
37 2 4

Program7
Develop a C program to simulate page replacement algorithms:
a) FIFO b) LRU
a)FIFO

#include<stdio.h
>
#include<conio.h
>int fr[3];
int main()
{ void
display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
intflag1=0,flag2=0,pf=0,frsize=3,top=
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

0; for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++) {
flag1=0;flag2=0;for(i=0;i<12;i++)
{ if(fr[i]==page[j]) {
flag1=1;flag2=1;break;}}
if(flag1==0)
{ for(i=0;i<frsize;i++)
{ if(fr[i]==
-1)
{
fr[i]=page[j];flag2=1;break;}}}
if(flag2==0) {
fr[top]=page[j];
top++;
pf++;
if(top>=frsize)
top=0; }
display(); }
printf("Number of pagefaults:%d",pf+frsize); getch
();
}
voiddisplay()
{
int i; printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}
Output
2 -1 -
1

2 3 -
1

2 3 -
1

2 3 -
1

2 3 5

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

2 3 5

4 3 5

4 3 5

4 3 5

4 2 5

4 2 5

4 2 5 Number
ofpagefaults:5

a)LRU

#include<stdio.h
>
#include<conio.h
>int fr[3];
int main()
{
voiddisplay();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int
index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0,flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1; flag2=
1;break;
}
}
if(flag1==0)
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

{
for(i=0;i<3;i++)
{ if(fr[i]== -1)
{
fr[i]=p[j];flag2=1;
break; }}}
if(flag2==0)
{ for(i=0;i<3;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<3;i++)
{ if(fr[i]==p[k])fs[i]=1;
}}
for(i=0;i<3;i++)
{ if(fs[i]==0)
index=i; }
fr[index]=p[j];
pf++; }
display();}
printf("\nno of pagefaults:
%d",pf+frsize); getch(); }
voiddisplay()
{ int i;
printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}
Output

2 -1 -
1

2 3 -
1

2 3 -
1

2 3 1

2 5 1

2 5 1

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

2 5 4

2 5 4

3 5 4

3 5 2

3 5 2

3 5 2

noofpagefaults:7

Program8
Simulate following File Organization Techniques
a) Single level directory b) Two level directory
a)Single level directory

#include<stdio.h
>
#include<cstring
>
#include<conio.h
>#include<cstdli
b>struct
{
Chardname[10],fname[10]
[10];
int fcnt;
}
dir;
int main()
{
int i,ch;
charf[30];
[Link] = 0;
printf("\nEnter name of
directory--"); scanf("%s", [Link]);
while(1)
{
printf("\n\[Link]\[Link]\[Link]\[Link]\[Link]\n Enter
your choice-- ");
scanf("%d",&ch);
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

switch(ch)
{
case 1:printf("\nEnter the name of the file--");
scanf("%s",[Link][[Link]]);
[Link]++;break;
case 2:printf("\nEnter the name of the file--");
scanf("%s",f);
for(i=0;i<[Link];i++)
{
if(strcmp(f,[Link][i])==0)
{
printf("File%sisdeleted",f);strcpy([Link][i],[Link][[Link]-1]); break;
}
}
if(i==[Link])
printf("File%snotfound",f);
else
[Link]--;
break;
case 3:printf("\nEnter the name of the file--");
scanf("%s",f);
for(i=0;i<[Link];i++)
{
if(strcmp(f,[Link][i])==0)
{
printf("File%sisfound",f);
break;
}
}
if(i==[Link])
printf("File%snotfound",f);
break;
case 4: if([Link]==0)
printf("\nDirectoryEmpty");
else
{
printf("\nTheFilesare--");
for(i=0;i<[Link];i++)
printf("\t%s",[Link][i]);
}
break;
default:exit(0);
}
}

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

getch();
}
Output
Enter name of directory--cse
1. CreateFile2.DeleteFile3. Search File
4. Display [Link]
Enter your choice -- 1
Enter the name of the file--q
1. CreateFile2.DeleteFile3. Search File
4. Display [Link]
Enter your choice -- 1
Enter the name of the file--w
1. CreateFile2.DeleteFile3. Search File
4. Display [Link]
Enter your choice -- 4
The Files are--qw
1. CreateFile2.DeleteFile3. Search File
4. Display [Link]
Enter your choice -- 3
Enterthenameofthefile--r
File r not found
1. CreateFile2.DeleteFile3. Search File
4. Display [Link]
Enter your choice -- 2
Enter the name of the file--q
File q is deleted
1. CreateFile2.DeleteFile3. Search File
4. Display [Link]
Enter your choice -- 4
The Files are--w
1. CreateFile2.DeleteFile3. Search File
4. Display [Link]
Enter your choice – 5

a) Two level directory

#include<stdio.h
>
#include<cstring
>
#include<conio.h
>#include<cstdli
b>struct
{
Char dname[10],fname[10]
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

[10];
int fcnt;
}
dir[10];
int
main()
{
int i,ch,dcnt,k;charf[30],d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\[Link]\t\[Link]\[Link]\t Enter your choice--");
scanf("%d",&ch);
switch(ch)
{
case1:printf("\nEnter name of
directory--"); scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");break;
case 2:printf("\n Enter name of the directory--");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("Filecreated");
}
if(i==dcnt)
printf("Directory%snotfound",d);
break;
case3:printf("\nEnter name of the directory--");
scanf("%s",d);
for(i=0;i<dcnt;i++)
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file--");
scanf("%s",f); for(k=0;k<dir[i].fcnt;k+
+)

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

{
if(strcmp(f,dir[i].fname[k])==0)
{
printf("File%sisdeleted",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File%snot found",f);
gotojmp;
}
}
printf("Directory%snotfound",d);
jmp : break;
case 4:printf("\nEnter name of the directory--");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file--");
scanf("%s",f); for(k=0;k<dir[i].fcnt;k+
+)
{
if(strcmp(f,dir[i].fname[k])==0)
{
printf("File%sisfound",f);gotojmp1;
}
}
printf("File%snot found",f);gotojmp1;
}
}
printf("Directory%snotfound",d);jmp1:break;
case 5: if(dcnt==0)
printf("\nNo Directory's");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

}
break;
default:exit(0);
}
}
getch();
}

Output
1. [Link] [Link] File
[Link]
Enter your choice -- 1
Enter name of directory--DIR1Directorycreated
1. [Link] [Link]
[Link]
Enter your choice --1
Enter name of directory -- DIR2
Directory created
1. [Link] [Link]
[Link]
Enter your choice --2
Enter name of the directory – DIR1
Enter name of the file--A1
File created
1. [Link] [Link]
[Link]
Enter your choice -- 2
Enter name of the directory–DIR1
Enter name of the file -- A2
File created
[Link] [Link]
4.SearchFile5.Display6.
Exit Enter your choice – 6

Program9
Develop a C program to simulate the Linked file allocation strategies.

#include<stdio.h
>
#include<conio.h
>#include<cstdli
b>
#include<cstring
>main()
{
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

intf[50],p,i,j,k,a,st,len,n,c;
for(i=0;i<50;i++) f[i]=0;
printf("Enter how many blocks that are already
allocated"); scanf("%d",&p);
printf("\nEnter the blocks no. that are already
allocated"); for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("Enter the starting index
block&length"); scanf("%d%d",&st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n%d->file is already allocated
",j); k++;
}
}
printf("\n if you want to enter one more file?(yes-1/
no-0)"); scanf("%d",&c);
if(c==1)
goto
X; else
exit(0);
getch();
}

Output
Enter how many blocks that are already
allocated 3 Enter the blocks no.s that are already
allocated 4 7 1 Enter the starting index block &
length 3 7
3->1
4->file is already

Dept. of CSE, BKIT BhalkiPage 32


OPERATING SYSTEM LAB (BCS303)

allocated 5->1
6->1
7->file is already
allocated 8->1
9->1
10->1
11->1
If you want to enter one more file?(yes-1/no-0)0

Program10
Develop a C program to simulate SCAN disk scheduling algorithm.

#include<stdio.h
>
#include<conio.h
> main()
{
int t[20],d[20],h,i,j,n,temp,k,atr[20],tot,p,sum=0;
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter the tracks");
for(i=2;i<n+2;i++) scanf("%
d",&t[i]); for(i=0;i<n+2;i++)
{
for(j=0;j<(n+2)-i-1;j++)
{
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}}}
for(i=0;i<n+2;i++)
if(t[i]==h)
j=i;k=i;
p=0;
while(t[j]!=0)
{
atr[p]=t[j];j--;
p++;
}
atr[p]=t[j];
Dept. of CSE, BKIT BhalkiPage 32
OPERATING SYSTEM LAB (BCS303)

for(p=k+1;p<n+2;p++,k++)
atr[p]=t[k+1];
for(j=0;j<n+1;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];}
printf("\nAverage header movements:%f",(float)sum/n);
getch();
}

Output
Enter the no of tracks to be
traversed 9 enter the position of
head 55
Enter the tracks 55 5860 7018 90 150 160184
Average header movements: 59992340.000000

Dept. of CSE, BKIT BhalkiPage 32

You might also like