Operating System Lab Manual-3
Operating System Lab Manual-3
Operating Systems
ISO 9001:2000
Affiliated to Jawaharlal Nehru Technological University,
AP, Dhullapaly, Quthbullapur, Secunderabad-500014
St. Martin’s Engineering College Operating System Lab Manual
INDEX
Objective:
Intel based desktop PC with minimum of 166 MHZ or faster processor with at least
64 MB RAM and 100 MB free disk space.
Description:
Scheduling is a fundamental operating system function.
CPU scheduling is the basis of multi programming operating system. CPU scheduling
algorithm determines how the CPU will be allocated to the process. These are of two types.
1. Primitive scheduling algorithms
2. Non-Primitive scheduling algorithms
1) Primitive Scheduling algorithms: In this, the CPU can release the process even in the
middle of execution. For example: the cpu executes the process p1, in the middle of
execution the cpu received a request signal from process p2, then the OS compares the
priorities of p1&p2. If the priority p1 is higher than the p2 then the cpu continue the
execution of process p1.Otherwise the cpu preempt the process p1 and assigned to process
p2.
2) Non-Primitive Scheduling algorithm: In this, once the cpu assigned to a process the
processor do not release until the completion of that process. The cpu will assign to some
other job only after the previous job has finished.
Scheduling methodology:
Though put: It means how many jobs are completed by the CPU with in a time period.
Turn around time: The time interval between the submission of the process and the time of
the completion is the turn around time.
Turn around time=Finished time – arrival time
Waiting time: it is the sum of the periods spent waiting by a process in the ready queue
Waiting time=Starting time- arrival time
Response time: it is the time duration between the submission and first response
Response time=First response-arrival time
CPU Utilization: This is the percentage of time that the processor is busy. CPU utilization
may range from 0 to 100%.
Round Robin: It is a primitive scheduling algorithm it is designed especially for time sharing
systems. In this, the CPU switches between the processes. When the time quantum expired,
the CPU switches to another job. A small unit of time called a quantum or time slice. A time
quantum is generally is a circular queue new processes are added to the tail of the ready
queue.
If the process may have a CPU burst of less than one time slice then the process release
the CPU voluntarily. The scheduler will then process to next process ready queue otherwise;
the process will be put at the tail of the ready queue.
Step 4: Calculate the no. of time slices for each process where
No. of time slice for process(n) = burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst time of
process(n-1 ) + the time difference in getting the CPU from process(n-1)
(b) Turn around time for process(n) = waiting time of process(n) + burst time of
process(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop the process
time1=time1+tq;
}
else if(p[i].bt!=0)
{
time1+=p[i].bt;
p[i].bt=0;
}
else
continue;
if(p[i].bt==0)
{
full=full-1;
tat[i]=time1;
}
}
}
for(i=0;i<n;i++)
{
p[i].ct=tat[i];
wt[i]=tat[i]-p[i].time;
}
printf("----------------------------------\n");
printf("PN\tBt\tCt\tTat\tWt\n");
printf("----------------------------------\n");
for(i=0;i<n;i++)
{
printf("%2s\t%2d\t%2d\t%2d\t%2d\n",p[i].pn,p[i].time,p[i].ct,tat[i],wt[i]);
avgwt+=wt[i];
}
printf("----------------------------------\n");
avgwt=avgwt/n;
printf(" Average waiting time = %.2f\n",avgwt);
printf("-----------------------------------\n");
getch();
}
OUTPUT 1:
--------------------------------------
PN Bt Ct Tat Wt
--------------------------------------
1 10 28 28 18
2 5 10 10 5
3 15 43 43 28
4 3 18 18 15
5 20 53 53 33
--------------------------------------
Average waiting time = 19.79
--------------------------------------
OUTPUT 2:
}
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;
}
compt[0]=bt[0];
for(i=1;i<n;i++)
compt[i]=bt[i]+compt[i-1];
for(i=0;i<n;i++)
{
tat[i]=compt[i];
wt[i]=tat[i]-bt[i];
sumtat+=tat[i];
sumwt+=wt[i];
}
avgwt=sumwt/n;
avgtat=sumtat/n;
printf("------------------------------\n");
printf("Bt\tCt\tTat\tWt\n");
printf("------------------------------\n");
for(i=0;i<n;i++)
{
printf("%2d\t%2d\t%2d\t%2d\n",i,bt[i],compt[i],tat[i],wt[i]);
}
printf("------------------------------\n");
printf(" Avgwt = %.2f\tAvgtat = %.2f\n",avgwt,avgtat);
printf("-------------------------------\n");
getch();
}
OUTPUT 1:
Enter number of processes: 4
Enter the burst time of 4 process
6873
------------------------------------
Bt Ct Tat Wt
------------------------------------
3 3 3 0
6 9 9 3
7 16 16 9
8 24 24 16
--------------------------------------
Avgwt = 7.00 Avgtat = 13.00
OUTPUT 2:
{
scanf("%d",&at[i]);
}
compt[0]=bt[0]-at[0];
for(i=1;i<n;i++)
compt[i]=bt[i]+compt[i-1];
for(i=0;i<n;i++)
{
tat[i]=compt[i]-at[i];
wt[i]=tat[i]-bt[i];
sumtat+=tat[i];
sumwt+=wt[i];
}
avgwt=sumwt/n;
avgtat=sumtat/n;
printf("----------------------------------\n");
printf("PN\tBt\tCt\tTat\tWt\n");
printf("----------------------------------\n");
for(i=0;i<n;i++)
{
printf("%d\t%2d\t%2d\t%2d\t%2d\n",i,bt[i],compt[i],tat[i],wt[i]);
}
printf("----------------------------------\n");
printf(" Avgwt = %.2f\tAvgtat = %.2f\n",avgwt,avgtat);
printf("-----------------------------------\n");
getch();
}
OUTPUT 1:
Enter number of processes: 5
Enter the burst time of 5 process
36452
Enter the arrival time of 5 process
02468
----------------------------------------
PN Bt Ct Tat Wt
----------------------------------------
0 3 3 3 0
1 6 9 7 1
2 4 13 9 5
3 5 18 12 7
4 2 20 12 10
---------------------------------------
Avgwt = 4.60 Avgtat = 8.60
---------------------------------------
OUTPUT 2:
Enter number of processes: 5
Enter the burst time of 5 process
5 24 16 10 3
Enter the arrival time of 5 process
00000
--------------------------------------
PN Bt Ct Tat Wt
--------------------------------------
0 5 5 5 0
1 24 29 29 5
2 16 45 45 29
3 10 55 55 45
4 3 58 58 55
--------------------------------------
Avgwt = 26.80 Avgtat = 38.40
---------------------------------------
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(p[i]>p[j])
{
temp1=bt[i];
bt[i]=bt[j];
bt[j]=temp1;
temp2=p[i];
p[i]=p[j];
p[j]=temp2;
}
compt[0]=bt[0]; wt[0]=0;
for(i=1;i<n;i++)
compt[i]=bt[i]+compt[i-1];
for(i=0;i<n;i++)
{
tat[i]=compt[i];
wt[i]=tat[i]-bt[i];
sumtat+=tat[i];
sumwt+=wt[i];
}
avgwt=sumwt/n; avgtat=sumtat/n;
printf("------------------------------\n");
printf("Bt\tCt\tTat\tWt\n");
printf("------------------------------\n");
for(i=0;i<n;i++)
{
printf("%2d\t%2d\t%2d\t%2d\n",bt[i],compt[i],tat[i],wt[i]);
}
printf("------------------------------\n");
printf(" Avgwt = %.2f\tAvgtat = %.2f\n",avgwt,avgtat);
printf("-------------------------------\n");
getch();
}
OUTPUT 1:
Enter number of processes: 4
Enter the burst time of 4 process
6535
Enter the priority of 4 process
4263
------------------------------------
Bt Ct Tat Wt
-----------------------------------
5 5 5 0
5 10 10 5
6 16 16 10
3 19 19 16
------------------------------------
Avgwt = 7.75 Avgtat = 12.50
OUTPUT 2:
Enter number of processes: 5
Enter the burst time of 5 process
10 1 2 1 5
Enter the priority of 5 process
31452
------------------------------------
Bt Ct Tat Wt
------------------------------------
1 1 1 0
5 6 6 1
10 16 16 6
2 18 18 16
1 19 19 18
--------------------------------------
Avgwt = 8.20 Avgtat = 12.00
--------------------------------------
Description:
Files are normally stored on the disks. So the main problem is how to allocate space to those
files. So that disk space is utilized effectively and files can be accessed quickly. Three major
strategies of allocating disc space are in wide use. Sequential, indexed and linked.
Sequential allocation :
In this allocation strategy, each file occupies a set of contiguously blocks on the disk. This
strategy is best suited. For sequential files, the file allocation table consists of a single entry
for each file. It shows the filenames, staring block of the file and size of the file. The main
problem of this strategy is, it is difficult to find the contiguous free blocks in the disk and
some free blocks could happen between two files.
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
getch();
}
OUTPUT 1:
Files Allocated are :
Enter starting block and length of files: 14 3
14 1
15 1
16 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 1
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 4
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)0
OUTPUT 2:
Files Allocated are :
Enter starting block and length of files: 17 4
17 1
18 1
19 1
20 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 21 3
21 1
22 1
23 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 19 4
Description:
Indexed allocation :
Indexed allocation supports both sequential and direct access files. The file indexes are not
physically stored as a part of the file allocation table. Whenever the file size increases, we can
easily add some more blocks to the index. In this strategy, the file allocation table contains a
single entry for each file. The entry consisting of one index block, the index blocks having
the pointers to the other blocks. No external fragmentation.
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any consumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process.
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
OUTPUT 1:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
OUTPUT 2:
Enter the index block: 4
Enter no of blocks needed and no of files for the index 4 on the disk :
3
123
Allocated
File Indexed
4-------->1 : 1
4-------->2 : 1
4-------->3 : 1
Do you want to enter more file(Yes - 1/No - 0)0
Description:
Linked allocation:
It is easy to allocate the files, because allocation is on an individual block basis. Each block
contains a pointer to the next free block in the chain. Here also the file allocation table
consisting of a single entry for each file. Using this strategy any free block can be added to a
chain very easily. There is a link between one block to another block, that’s why it is said to
be linked allocation. We can avoid the external fragmentation.
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
OUTPUT 1:
Enter how many blocks already allocated: 3
Enter blocks already allocated: 1 3 5
Enter index starting block and length: 2 2
2-------->1
3 Block is already allocated
4-------->1
Do you want to enter mre file(Yes - 1/No - 0)0
OUTPUT 2:
Enter blocks already allocated: 2 4 6 8 10 12
Enter index starting block and length: 3 10
3-------->1
4 Block is already allocated
5-------->1
6 Block is already allocated
7-------->1
8 Block is already allocated
9-------->1
10 Block is already allocated
11-------->1
12 Block is already allocated
13-------->1
14-------->1
15-------->1
16-------->1
17-------->1
Do you want to enter more file(Yes - 1/No - 0)1
Enter index starting block and length: 5 1
5 starting block is already allocated
Do you want to enter more file(Yes - 1/No - 0)1
Description:
MVT:
MVT stands for multiprogramming with variable number of tasks. Multiprogramming is a
technique to execute number of programs simultaneously by a single processor. This is one of
the memory management techniques. To eliminate the same of the problems with fixed
partitions, an approach known as dynamic partitioning developed. In this technique, partitions
are created dynamically, so that each process is loaded into partition of exactly the same size
at that process. This scheme suffering from external fragmentation.
{
printf("\nExternal fragmentation is %d",m);
break;
}
if(m<p[i])
{
printf("\nRequired memory is not available\n");
goto abc;
}
}while((ch=='y')&&(m>=p[i]));
getch();
}
OUTPUT 1:
Enter memory to be allocated: 1000
Enter process size : 500
Remaining memory is 500
Do you want to continue: y
Enter the process size: 300
Remaining memory is 200
Do you want to continue: y
Enter the process size: 100
Remaining memory is 100
Do you want to continue: n
External fragmentation is 100
OUTPUT 2:
Enter memory to be allocated: 800
Enter process size : 300
Remaining memory is 500
Do you want to continue: y
Enter the process size: 200
Remaining memory is 300
Do you want to continue: y
Enter the process size: 200
Remaining memory is 100
Do you want to continue: y
Enter the process size: 100
Remaining memory is 0
Do you want to continue: n
External fragmentation is 0
Description:
MFT:
MFT stands for multiprogramming with fixed no of tasks.
MFT is the one of the memory management technique. In this technique, main memory
is divided into no of static partitions at the system generated time. A process may be loaded
into a partition of equal or greater size. The partition sizes are depending on o.s. in this
memory management scheme the o.s occupies the low memory, and the rest of the main
memory is available for user space. This scheme suffers from internal as well as external
fragmentation.
{
printf(“Required memory is not available”);
goto lable;
}
total=total+c[i];
}
printf(“Total internal fragmentation %d\n”,total);
getch();
}
OUTPUT 1:
Enter the total memory: 800
Enter the processes: 4
Enter memory for 0 process: 150
Internal fragmentation for this block: 50
Enter memory for 1 process: 100
Internal fragmentation for this block: 100
Enter memory for 2 process: 200
Internal fragmentation for this block: 0
Enter memory for 3 process: 200
Internal fragmentation for this block: 0
Total internal fragmentation 150
OUTPUT 2:
Enter the total memory: 1200
Enter the processes: 4
Enter memory for 0 process: 300
Internal fragmentation for this block: 0
Enter memory for 1 process: 300
Internal fragmentation for this block: 0
Enter memory for 2 process: 300
Internal fragmentation for this block: 0
Enter memory for 3 process: 300
Internal fragmentation for this block: 0
Total internal fragmentation 0
Description:
File Organization Techniques:
a)Single Level Directory
b)Two Level
c)Hierarchical
d)General Graph Directory
The directory contains information about the files, including attributes, location and
ownership. Sometimes the directories consisting of subdirectories also. The directory is itself
a file, owned by the operating system and accessible by various file management routines.
Single Level Directories: It is the simplest of all directory structures, in this the directory
system having only one directory, it consisting of the all files. Sometimes it is said to be root
directory. The following dig. Shows single level directory that contains four files (A, B, C,
D).
It has the simplicity and ability to locate files quickly. It is not used in the multi-user system,
it is used on small embedded system.
Step 1:Start
setfillstyle(1,MAGENTA);
Step 8: mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextstyle(1,1);
outtextxy(320,125,"rootdirectory");
setcolor(BLUE);
i++;
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int nf=0,i=0,j=0,ch;
char mdname[10],fname[10][10],name[10];
clrscr();
printf("Enter the directory name:");
scanf("%s",mdname);
printf("Enter the number of files:");
scanf("%d",&nf);
do
{
printf("Enter file name to be created:");
scanf("%s",name);
for(i=0;i<nf;i++)
{
if(!strcmp(name,fname[i]))
break;
}
if(i==nf)
{
strcpy(fname[j++],name);
nf++;
}
else
printf("There is already %s\n",name);
OUTPUT 1:
OUTPUT 2:
Enter the directory name:abc
Enter the number of files:3
Enter file name to be created:xyz
Do you want to enter another file(yes - 1 or no - 0):1
Enter file name to be created:klm
Do you want to enter another file(yes - 1 or no - 0):0
Directory name is:abc
Files names are:
xyz
klm
Description:
Two Level Directory: The problem in single level directory is different users may be
accidentally using the same names for their files. To avoid this problem, each user need a
private directory. In this way names chosen by one user don’t interface with names chosen by
a different user. The following dig 2-level directory
Here root directory is the first level directory it consisting of entries of user directory. User1,
User2, User3 are the user levels of directories. A, B, C are the files.
Step 1:Start
(*root)->ftype=1;
else(*root)->ftype=2;
(*root)->level=lev;
(*root)->y=50+lev*5;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
Step 19:End
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
if(lev==0||lev==1)
(*root)->ftype=1;
else
(*root)->ftype=2;
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
if(lev==0||lev==1)
{
if((*root)->level==0)
printf("How many users");
else
printf("hoe many files");
printf("(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
else
(*root)->nc=0;
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)-
>name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}}
OUTPUT:
Description:
Hierarchical Directory: The two level directories eliminate name conflicts among users but
it is not satisfactory for users but it is not satisfactory for users with a large no of files. To
avoid this, create the subdirectory and load the same type of the files into the subdirectory.
So, in this method each can have as many directories are needed.
This directory structure looks like tree, that’s why it is also said to be tree-level directory
structure.
Step 1:Start
*root->lx=lx
*root->rx = rx
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) : ",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 for file :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)-
>name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root !=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}
OUTPUT:
Description:
General graph Directory: When we add links to an existing tree structured directory, the
tree structure is destroyed, resulting in a simple graph structure. This structure is used to
traversing is easy and file sharing also possible.
display(root);
getch();
closegraph();
}
read_links()
{
int i;
printf("how many links");
scanf("%d",&nofl);
for(i=0;i<nofl;i++)
{
printf("File/dir:");
fflush(stdin);
gets(L[i].from);
printf("user name:");
fflush(stdin);
gets(L[i].to);
}
}
draw_link_lines()
{
int i,x1,y1,x2,y2;
for(i=0;i<nofl;i++)
{
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1);
}
}
search(node *root,char *s,int *x,int *y)
{
int i;
if(root!=NULL)
{
if(strcmpi(root->name,s)==0)
{
*x=root->x;
*y=root->y;
return;
}
else
{
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}
}
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
OUTPUT:
Enter Name of dir/file (under root): ROOT
Enter 1 for Dir / 2 For File : 1
No of subdirectories / files (for ROOT) :2
Enter Name of dir/file (under ROOT): USER 1
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER 1): 2
Enter Name of dir/file (under USER1): VB
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for VB): 2
Enter Name of dir/file (under VB): A
Description:
Deadlock: A process request the resources, the resources are not available at that time, so the
process enter into the waiting state. The requesting resources are held by another waiting
process,both are in waiting state, this situation is said to be Deadlock.
A deadlocked system must satisfied the following 4 conditions. These are:
(i) Mutual Exclusion: Mutual Exclusion means resources are in non-sharable mode only, it
means only one process at a time can use a process.
(ii) Hold and Wait: Each and every process is the deadlock state, must holding at least one
resource and is waiting for additional resources, that are currently being held by another
process.
(iii) No Preemption: No Preemption means resources are not released in the middle of the
work, they released only after the process has completed its task.
(iv) Circular Wait: If process P1 is waiting for a resource R1, it is held by P2, process P2 is
waiting for R2, R2 held by P3, P3 is waiting for R4, R4 is held by P2, P2 waiting for resource
R3, it is held by P1.
Deadlock Avoidance: It is one of the method of dynamically escaping from the deadlocks.
In this scheme, if a process request for resources, the avoidance algorithm checks before the
allocation of resources about the state of system. If the state is safe, the system allocate the
resources to the requesting process otherwise (unsafe) do not allocate the resources. So taking
care before the allocation said to be deadlock avoidance.
Banker’s Algorithm: It is the deadlock avoidance algorithm, the name was chosen because
the bank never allocates more than the available cash.
Available: A vector of length ‘m’ indicates the number of available resources of each type. If
available[j]=k, there are ‘k’ instances of resource types Rj available.
Allocation: An nxm matrix defines the number of resources of each type currently allocated
to each process. If allocation[i,j]=k, then process Pi is currently allocated ‘k’ instances of
resources type Rj.
Max: An nxm matrix defines the maximum demand of each process. If max[i,j]=k, then Pi
may request at most ‘k’ instances of resource type Rj.
Need: An nxm matrix indicates the remaining resources need of each process. If need[I,j]=k,
then Pi may need ‘k’ more instances of resource type Rj to complete this task. There fore,
Need[i,j]=Max[i,j]-Allocation[I,j]
Safety Algorithm:
1. Work and Finish be the vector of length m and n respectively, Work=Available and
Finish[i] =False.
2. Find an i such that both
Finish[i] =False
Need<=Work
If no such I exists go to step 4.
3. work=work+Allocation, Finish[i] =True;
4. if Finish[1]=True for all I, then the system is in safe state.
Resource request algorithm
Let Request i be request vector for the process Pi, If request i=[j]=k, then process Pi
wants k instances of resource type Rj.
1. if Request<=Need I go to step 2. Otherwise raise an error condition.
2. if Request<=Available go to step 3. Otherwise Pi must since the resources are
available.
3. Have the system pretend to have allocated the requested resources to process Pi by
modifying the state as follows;
Available=Available-Request I;
Allocation I =Allocation +Request I;
Need i=Need i-Request I;
If the resulting resource allocation state is safe, the transaction is completed and process Pi is
allocated its resources. However, if the state is unsafe, the Pi must wait for Request i and the
old resource-allocation state is restored.
goto A;
else
printf("\n System is in safe mode");
printf("\n The given state is safe state");
getch();
}
OUTPUT 1:
ind++;
}
else
{
printf("No safe sequence\n");
}
}
printf("Safe sequence is:");
for(i=1;i<ind; i++)
printf(" %s %d\n",job[safe[i]],time[safe[i]]);
getch();
}
OUTPUT 1:
Enter no of jobs:4
Enter name and time: A 1
Enter name and time: B 4
Enter name and time: C 2
Enter name and time: D 3
Enter the available resources: 20
Safe sequence is: A 1, C 2, D 3, B 4.
Description:
FIFO (First in First Out) algorithm: FIFO is the simplest page replacement algorithm, the
idea behind this is, “Replace a page that page is oldest page of main memory” or “Replace
the page that has been in memory longest”. FIFO focuses on the length of time a page has
been in the memory rather than how much the page is being used.
#include<stdio.h>
#include<conio.h>
int fr[3],m;
void display();
void main()
{
int i,j,page[20];
int flag1=0,flag2=0,pf=0;
int n, top=0;
float pr;
clrscr();
printf("Enter length of the reference string: ");
scanf("%d",&n);
printf("Enter the reference string: ");
for(i=0;i<n;i++)
scanf("%d",&page[i]);
printf("Enter no of frames: ");
scanf("%d",&m);
for(i=0;i<m;i++)
fr[i]=-1;
for(j=0;j<n;j++)
{
flag1=0;
flag2=0;
for(i=0;i<m;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
if(fr[i]==-1)
{
fr[i]=page[i];
flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=m)
top=0;
}
display();
}
pf+=m;
printf("Number of page faults : %d\n", pf);
pr=(float)pf/n*100;
printf("Page fault rate = %f \n", pr);
getch();
}
void display()
{
int i;
for(i=0;i<m;i++)
printf("%d\t", fr[i]);
printf("\n");
}
OUTPUT 1:
Enter length of the reference string: 12
Enter the reference string: 1 2 3 4 1 2 5 1 2 3 4 5
Enter no of frames: 3
1 -1 -1
1 2 -1
1 2 3
4 2 3
4 1 3
4 1 2
5 1 2
5 1 2
5 1 2
5 3 2
5 3 4
5 3 4
Number of page faults : 9
Page fault rate = 75.000000
OUTPUT 2:
Enter length of the reference string: 12
Enter the reference string: 1 2 3 4 1 2 5 1 2 3 4 5
Enter no of frames: 4
1 -1 -1 -1
1 2 -1 -1
1 2 3 -1
1 2 3 4
1 2 3 4
1 2 3 4
5 2 3 4
5 1 3 4
5 1 2 4
5 1 2 3
4 1 2 3
4 5 2 3
Number of page faults : 10
Page fault rate = 83.333336
OUTPUT 3:
Enter length of the reference string: 20
Enter the reference string: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter no of frames: 3
7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 1
2 3 1
2 3 0
4 3 0
4 2 0
4 2 3
0 2 3
0 2 3
0 2 3
0 1 3
0 1 2
0 1 2
0 1 2
7 1 2
7 0 2
7 0 1
Number of page faults : 15
Page fault rate = 75.000000
OUTPUT 4:
Enter length of the reference string: 20
Enter the reference string: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter no of frames: 4
7 -1 -1 -1
7 0 -1 -1
7 0 1 -1
7 0 1 2
7 0 1 2
3 0 1 2
3 0 1 2
3 4 1 2
3 4 1 2
3 4 1 2
3 4 0 2
3 4 0 2
3 4 0 2
3 4 0 1
2 4 0 1
2 4 0 1
2 4 0 1
2 7 0 1
2 7 0 1
2 7 0 1
Number of page faults : 10
Page fault rate = 50.000000
Description:
LRU (Least Recently Used): the criteria of this algorithm is “Replace a page that has been
used for the longest period of time”. This strategy is the page replacement algorithm looking
backward in time, rather than forward.
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<m;i++)
fs[i]=0;
for(k=j-1,l=1;l<=m;l++,k--)
{
for(i=0;i<m;i++)
{
if(fr[i]==page[k])
fs[i]=1;
}
}
for(i=0;i<m;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=page[j];
pf++;
}
for(i=0;i<m;i++)
printf("%d\t", fr[i]);
printf("\n");
}
pf+=m;
printf("Number of page faults : %d\n", pf);
pr=(float)pf/n*100;
printf("Page fault rate = %f \n", pr);
getch();
}
OUTPUT 1:
Enter length of the reference string: 20
Enter the reference string: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter no of frames: 4
7 -1 -1 -1
7 0 -1 -1
7 0 1 -1
7 0 1 2
7 0 1 2
3 0 1 2
3 0 1 2
3 0 4 2
3 0 4 2
3 0 4 2
3 0 4 2
3 0 4 2
3 0 4 2
3 0 1 2
3 0 1 2
3 0 1 2
3 0 1 2
7 0 1 2
7 0 1 2
7 0 1 2
Number of page faults : 8
Page fault rate = 40.000000
OUTPUT 2:
Enter length of the reference string: 20
Enter the reference string: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter no of frames: 3
7 -1 -1
7 0 -1
7 0 1
7 0 2
7 0 2
3 0 2
3 0 2
3 0 4
3 0 2
3 0 2
3 0 2
3 0 2
3 0 2
3 0 1
3 2 1
0 2 1
0 2 1
7 2 1
7 0 1
7 0 1
Number of page faults : 12
Page fault rate = 60.000000
OUTPUT 3:
Enter length of the reference string: 12
Enter the reference string: 1 2 3 4 1 2 5 1 2 3 4 5
Enter no of frames: 3
1 -1 -1
1 2 -1
1 2 3
4 2 3
1 2 3
1 2 3
1 2 5
1 2 5
1 2 5
1 2 3
1 2 4
5 2 4
Number of page faults : 9
Page fault rate = 75.000000
OUTPUT 4:
Enter length of the reference string: 12
Enter the reference string: 1 2 3 4 1 2 5 1 2 3 4 5
Enter no of frames: 4
1 -1 -1 -1
1 2 -1 -1
1 2 3 -1
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Number of page faults : 6
Page fault rate = 50.000000
Description:
LFU (Least Frequently Used): The least frequently used algorithm “select a page for
replacement, if the page has not been used for the often in the past” or “Replace page that
page has smallest count” for this algorithm each page maintains as counter which counter
value shows the least count, replace that page. The frequency counter is reset each time is
page is loaded.
Here we select the page that will not be used for the longest period of time.
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<m;i++)
lg[i]=0;
for(i=0;i<m;i++)
{
for(k=j+1;k<=n;k++)
{
if(fr[i]==page[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<m;i++)
{
if(lg[i]==0)
{
index=i;
found = 1;
break;
}
}
if(found==0)
{
max=lg[0];
index=0;
for(i=0;i<m;i++)
{
if(max<lg[i])
{
max=lg[i];
index=i;
}
}
}
fr[index]=page[j];
pf++;
}
display();
}
printf("Number of page faults : %d\n", pf);
pr=(float)pf/n*100;
printf("Page fault rate = %f \n", pr);
getch();
}
void display()
{
int i;
for(i=0;i<m;i++)
printf("%d\t",fr[i]);
printf("\n");
}
OUTPUT 1:
Enter length of the reference string: 12
Enter the reference string: 1 2 3 4 1 2 5 1 2 3 4 5
Enter no of frames: 3
1 -1 -1
1 2 -1
1 2 3
1 2 4
1 2 4
1 2 4
1 2 5
1 2 5
1 2 5
3 2 5
4 2 5
4 2 5
Number of page faults : 7
Page fault rate = 58.333332
OUTPUT 2:
Enter length of the reference string: 12
Enter the reference string: 1 2 3 4 1 2 5 1 2 3 4 5
Enter no of frames: 3
1 -1 -1
1 2 -1
1 2 3
1 2 4
1 2 4
1 2 4
1 2 5
1 2 5
1 2 5
3 2 5
4 2 5
4 2 5
Number of page faults : 7
Page fault rate = 58.333332
OUTPUT 3:
Enter length of the reference string: 20
Enter the reference string: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter no of frames: 3
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
Number of page faults : 9
Page fault rate = 45.000000
OUTPUT 4:
Enter length of the reference string: 20
Enter the reference string: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter no of frames: 4
7 -1 -1 -1
7 0 -1 -1
7 0 1 -1
7 0 1 2
7 0 1 2
3 0 1 2
3 0 1 2
3 0 4 2
3 0 4 2
3 0 4 2
3 0 4 2
3 0 4 2
3 0 4 2
1 0 4 2
1 0 4 2
1 0 4 2
1 0 4 2
1 0 7 2
1 0 7 2
1 0 7 2
Number of page faults : 8
Page fault rate = 40.000000
OUTPUT 1:
Enter how many pages: 5
Enter page size: 4
Page 0 address is 3080
Page 1 address is 3088
OUTPUT 2:
Enter how many pages: 7
Enter page size: 8
Page 0 address is 3080
Page 1 address is 3096
Page 2 address is 3112
Page 3 address is 3128
Page 4 address is 3144
Page 5 address is 3160
Page 6 address is 3176
Text Books:
1. Operating System Principles, Abraham Silberchatz, Peter B. Galvin, Greg Gagne, 8th
Edition, Wiley Student Edition.
2. Operating Systems – Internals and Design Principles, W.Stallings, 6th Edition, Pearson.
Reference Books:
1. Modern Operating Systems, Andrew S Tanenbaum, 3rd Edition,PH.
2. Operating Systems A concept-based Approach, 2nd Edition,D.M.Dhamdhere, TMH.
3. Principles of Operating Systems, B.L.Stuart, Cengage learning, India Edition.