Os Lab Manual
Os Lab Manual
BE - III SEMESTER
OPERATING SYSTEMS
LABORATORY MANUAL
BCS303
Program : fork()
#include<stdio.h>
#include<sys/types.h>
int main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("\n I am the child");
printf("\n I am the parent:%d", getppid());
printf("\n I am the child :%d", getpid());
}
else
{
printf("\n I am the parent");
printf("\n I am the parents parent:%d", getppid());
printf("\n I am the parent :%d\n", getpid());
}
return 0;
}
Output:
cc frk.c
./a.out
I am the child
I am the parent: 3944
I am the child: 3945
I am the parent
I am the parents parent: 3211
I am the parent: 3944
Page No: 1
Program : exec()
#include<stdio.h>
#include<unistd.h>
int main()
{
char*temp[3]; temp[0]="ls";
temp[1]="-l"; temp[2]=(char *)0; execv("/bin/ls",temp);
printf("thiswillnot print\n");
return 0;
}
Output:
cc execv.c
./a.out
total76
-rwxr-xr-x 1 be322 group 4716Mar 7 10:13 a.out
-rw-r--r-- 1 be322 group 688Feb2013:52 comm.c
-rw-r--r-- 1 be322 group 925Feb 2013:54 echomsg.c
-rw-r--r-- 1 be322 group 722Feb2013:55 echopipe.c
-rw-r--r-- 1 be322 group 178Feb2013:57 exel.c
-rw-r--r-- 1 be322 group 167Mar 710:13 exev.c
-rw-r--r-- 1 be322 group 1109Feb 2013:57 fflag.c
-rw-r--r-- 1 be322 group 341Dec2614:47 frk.c
-rw-r--r-- 1 be322 group 140Feb2013:57 linearg.c
-rw-r--r-- 1 be322 group 528Feb2013:57 lock.c
-rw-r--r-- 1 be322 group 254Feb2013:57 msg.c
-rw-r--r-- 1 be322 group 1036Feb2013:57 msgpass.c
-rw-r--r-- 1 be322 group 203Feb2013:58 sem.c
-rw-r--r-- 1 be322 group 1167Feb2013:58 sharememory.c
-rw-r--r-- 1 be322 group 312Feb2013:58 slp.c
-rw-r--r-- 1 be322 group 1182Feb2013:58 threadf.c
-rw-r--r-- 1 be322 group 287Feb2013:59 wt.c
Page No: 2
Program : exec()
#include<stdio.h>
#include<unistd.h>
int main()
{
char*temp[3];
temp[0]="ls";
temp[1]="-l"; temp[2]=(char *)0; execv("/bin/ls",temp);
printf("thiswillnot print\n");
return 0;
}
Output:
cc execv.c
./a.out
total76
-rwxr-xr-x 1 be322 group 4716Mar 7 10:13 a.out
-rw-r--r-- 1 be322 group 688Feb2013:52 comm.c
-rw-r--r-- 1 be322 group 925Feb 2013:54 echomsg.c
-rw-r--r-- 1 be322 group 722Feb2013:55 echopipe.c
-rw-r--r-- 1 be322 group 178Feb2013:57 exel.c
-rw-r--r-- 1 be322 group 167Mar 710:13 exev.c
-rw-r--r-- 1 be322 group 1109Feb 2013:57 fflag.c
-rw-r--r-- 1 be322 group 341Dec2614:47 frk.c
-rw-r--r-- 1 be322 group 140Feb2013:57 linearg.c
-rw-r--r-- 1 be322 group 528Feb2013:57 lock.c
-rw-r--r-- 1 be322 group 254Feb2013:57 msg.c
-rw-r--r-- 1 be322 group 1036Feb2013:57 msgpass.c
-rw-r--r-- 1 be322 group 203Feb2013:58 sem.c
-rw-r--r-- 1 be322 group 1167Feb2013:58 sharememory.c
-rw-r--r-- 1 be322 group 312Feb2013:58 slp.c
-rw-r--r-- 1 be322 group 1182Feb2013:58 threadf.c
-rw-r--r-- 1 be322 group 287Feb2013:59 wt.c
Page No: 3
Program : wait()
#include<unistd.h>
#include<stdio.h>
int main()
{
int i=0,pid;
pid=fork();
if(pid==0)
{
printf("childprocessstarted\n");
for(i=0;i<10;i++)
printf("\n%d",i);
printf("\nchildprocess ends");
}
else
{
printf("\nparentprocessstarts"); wait(0);
printf("\nparentprocess ends")
}
}
OUTPUT:
cc wt.c
./a.out
parent process starts
child process started
0
1
2
3
4
5
6
7
8
9
child process ends
parent process ends
Page No: 4
Program : Sleep()
#include<unistd.h>
#include<stdio.h>
int main()
{
int i=0,pid;
printf("\n ready for fork\n");
pid=fork();
if(pid==0)
{
printf("\n child process started\n");
sleep(4);
for(i=0;i<10;i++)
printf("\n %d" , i);
printf("\n child process ends");
}
else
{
printf("\n I am the parent");
printf("\n parent process ends");
}
}
OUTPUT:
cc slp.c
./a.out
ready for fork
I am the parent
parent process ends
child process started
Page No: 5
2. Simulate the following CPU scheduling algorithms to find turnaround
time and waiting time a) FCFS b) SJF c) Round Robin d) Priority.
#include<stdio.h>
#include<conio.h>
intmain()
{
int bt[20], wt[20],
tat[20], i, n; float
wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes--");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for
p rocess %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("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME\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();
}
Page No: 6
INPUT
Enter the number of processes -- 3
Enter Burst Time for Process 0 -- 24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3
OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND
TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time-- 17.000000
Average Turnaround Time -- 27.000000
C).ROUND ROBIN
#include<stdio.h>
intmain()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clrscr();
printf("Enter the no of processes -- ");
scanf("%d",&n);
Page No: 8
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %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;
}
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\t BURST TIME \t WAITING TIME\tTURNAROUND
TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d\n",i+1,ct[i],wa[i],tat[i]);
getch();
}
INPUT:
Enter the no of processes – 3
Enter Burst Time for process 1 – 24
Enter Burst Time for process 2 -- 3
Enter Burst Time for process 3 – 3
Enter the size of time slice – 3
Page No: 9
OUTPUT:
PROCESS BURST TIME WAITING TIME TURNAROUNDTIME
1 2 6 30
2 3 4 7
3 3 7 10
The Average Turnaround time is – 15.666667 The
Average Waiting time is ---- 5.666667
D). PRIORITY:
#include<stdio.h>
int main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
clrscr();
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++)
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;
}
Page No: 10
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\tBURST TIME\tWAITING
TIME\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("\nAverage
Turnaround Time is --- %f",tatavg/n);
getch();
}
INPUT
Enter the number of processes -- 5
Enter the Burst Time & Priority of Process 0 --- 10 3
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
OUTPUT
PROCESS PRIORITY BURST TIME WAITIN TURNARO
G TIME UND TIME
1 1 1 0 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
Page No: 11
3. Develop a C program to simulate producer-consumer problem
using semaphores.
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0; out = 0; bufsize = 10;
while(choice !=3)
{
printf(“\n1.Produce\t 2.Consume\t3.Exit”);
printf(“\nEnter your choice: ”);
scanf(“%d”,&choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “); scanf(“%d”, &produce); buffer[in] = produce;
in = (in+1)%bufsize;
}
break;;;
case 2: if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume); out = (out+1)%bufsize;
}
break;
}
}
}
OUTPUT
1. Produce 2. Consume 3. Exit Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Exit Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3. Exit Enter your choice: 3
Page No: 12
4. 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.
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
void main()
{
charbuff; int fd,fd1;
fd=open("one.txt",O_RDONLY);
fd1=open("two.txt",O_WRONLY|O_CREAT);
while(read(fd,&buff,1))
write(fd1,&buff,1);
printf("Thefileissuccessfullycopied!!!\n");
close(fd);
close(fd1);
}
do
{
printf("1.One can eat at a time\t2.Two can eat at a time\t3.Exit\nEnter your
choice:"); scanf("%d", &cho);
switch(cho)
{
case 1:one();
break;
case 2:two();
break;
case 3:exit(0);
default: printf("\nInvalid option..");
}
}
While(1);
}
}
One()
{
int pos=0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
Page No: 13
for(i=0;i<howhung; i++, pos++)
{
printf("\nP %d is granted to eat", philname[hu[pos]]);
for(x=pos;x<howhung;x++)
printf("\nP %d is waiting", philname[hu[x]]);
}
}
two()
{
int i, j, s=0, t, r, x;
printf("\n Allow two philosophers to eat at same time\n");
for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1&& abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination
%d \n", (s+1)); t=hu[i];
r=hu[j]; s++;
printf("\nP %d and P %d are granted to eat",
philname[hu[i]], philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}
}}
}
INPUT
DINING PHILOSOPHER PROBLEM
Enter the total no. of philosophers: 5
How many are hungry : 3
Enter philosopher 1 position: 2
Enter philosopher 2 position: 4
Enter philosopher 3 position: 5
Page No: 14
5. Develop a C program to simulate Bankers Algorithm for DeadLock
Avoidance.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int alloc[10][10],max[10][10];
int avail[10],work[10],total[10];
int i,j,k,n,need[10][10];
int m;
int count=0,c=0;
char finish[10];
clrscr();
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++)
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:
Page No: 15
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 to Process %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 %d executed?:%c \n",i+1,finish[i]);
count++;
}
}
if(count!=n) goto A;
else
printf("\n System is in safe mode");
printf("\n The given state is safe state"); getch();
}
Page No: 16
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:9 3 6
All the resources can be allocated to Process 2
Available resources are: 6 2 3
Process 2 executed?:y
All the resources can be allocated to Process 3 Available
resources are: 8 3 4
Process 3 executed?:y
All the resources can be allocated to Process 4
Available resources are: 8 3 6
Process 4 executed?:y
All the resources can be allocated to Process 1
Available resources are: 9 3 6
Process 1 executed?:y
System is in safe mode
The given state is safe state
Page No: 17
6. 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>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,t emp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme-First Fit");
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)
{
temp=b[j]-f[i]; if(temp>=0)
{
Page No: 18
ff[i]=j; break;
}
}
}
frag[i]=temp; bf[ff[i]]=1;
}
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();
}
INPUT:
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:- Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:-
File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
b) Best fit
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
Int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:")
scanf("%d",&nb);
Page No: 19
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)
{
temp=b[j]-f[i]; if(temp>=0)
if(lowest>temp)
{
ff[i]=j; lowest=temp;
}
}}
frag[i]=lowest; bf[ff[i]]=1; lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;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();
}
Page No: 20
INPUT Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:-
File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1
c)FIRST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
intfrag[max],b[max],f[max],i,j,nb,nf,temp,highes t=0; static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit");
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]);
}
Page No: 21
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)
{
}
}
frag[i]=highest; bf[ff[i]]=1; highest=0;
}
ff[i]=j; highest=temp;
}
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFrage
ment"); 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();
}
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:-
File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 3 7 6
2 4 1 5 1
Page No: 22
7. Develop a C program to simulate page replacement algorithms: a)
FIFO b) LRU
a) FIFO
#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int flag1=0,flag2=0,pf=0,frsize=3,top=0; clrscr();
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)
{
Page No: 23
fr[top]=page[j]; top++;
pf++;
if(top>=frsize)
top=0;
}
display();
}
printf("Number of page faults :
%d ",pf+frsize);
getch();
}
void display()
{
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
5 3 1
5 2 1
5 2 4
5 2 4
3 2 4
3 2 4
3 5 4
3 5 2
Number of page faults: 9
c) LRU
#include<stdio.h>
#include<conio.h>
int fr[3];
Page No: 24
void main()
{
void display();
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; clrscr();
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)
{
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--)
Page No: 25
{
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("\n no of page faults :%d",pf+frsize);
getch();
}
void display()
{
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
2 5 4
2 5 4
3 5 4
3 5 2
3 5 2
3 5 2
No of page faults: 7
Page No: 26
8. Simulate following File Organization Techniques a) Single level
directory b) Two level directory
a) Single level directory
#include<stdio.h> struct
{
char dname[10],fname[10][10];
int fcnt;
}
dir;
void main()
{
int i,ch;
char f[30];
clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory--");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n
4. Display Files\t5. Exit\nEnter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++; break;
case 2: printf("\nEnter the name
of the file -- "); scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
Page No: 27
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
{
dir.fcnt--;
break;
case 3: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty"); else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
getch();
}
OUTPUT:
Enter name of directory -- CSE
Create File 2. Delete File 3. Search File 4. Display Files 5. Exit
Enter your choice – 1
Page No: 28
Enter the name of the file -- A
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 1
OUTPUT:
Page No: 35