OS LAB2
OS LAB2
MOOSARAMBAGH
MCA DEPARTMENT
OPERATING SYSTEMS
LAB MANUAL
Exp.
No.
Division of
No.
CPU
Scheduling
Algorithms
considering the following scenario. All the processes in the system are
File
Allocation
Strategies
Memory
Management
Techniques
techniques. 13
allocation techniques
16
File
Organization
Techniques
Write a C program to simulate the following file organization techniques
8 Deadlock
Management
Techniques
deadlock avoidance. 29
10 Page
Replacement
Algorithms
a) Optimal 41
12 Process
Synchroniza-
tion
semaphores. 44
problem. 45
This lab complements the operating systems course. Students will gain practical experience with
designing and
implementing concepts of operating systems such as system calls, CPU scheduling, process
management,
memory management, file systems and deadlock handling using C language in Linux environment.
OUTCOMES:
Upon the completion of Operating Systems practical course, the student will be able to:
1. Understand and implement basic services and functionalities of the operating system using system
calls.
2. Use modern operating system calls and synchronization libraries in software/ hardware interfaces.
3. Understand the benefits of thread over process and implement synchronized programs using
multithreading concepts.
4. Analyze and simulate CPU Scheduling Algorithms like FCFS, Round Robin, SJF, and Priority.
7. Understand the concepts of deadlock in operating systems and implement them in multiprogramming
system.
EXPERIMENT 1
1.1 OBJECTIVE
Write a C program to simulate the following non-preemptive CPU scheduling algorithms to find
turnaround time
1.2 DESCRIPTION
For FCFS scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times.
The
scheduling is performed on the basis of arrival time of the processes irrespective of their other
parameters. Each
process will be executed according to its arrival time. Calculate the waiting time and turnaround time of
each of
For SJF scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times.
Arrange
all the jobs in order with respect to their burst times. There may be two jobs in queue with the same
execution
time, and then FCFS approach is to be performed. Each process will be executed according to the length
of its
burst time. Then calculate the waiting time and turnaround time of each of the processes accordingly.
For round robin scheduling algorithm, read the number of processes/jobs in the system, their CPU burst
times,
and the size of the time slice. Time slices are assigned to each process in equal portions and in circular
order,
handling all processes execution. This allows every process to get an equal chance. Calculate the waiting
time
For priority scheduling algorithm, read the number of processes/jobs in the system, their CPU burst
times, and
the priorities. Arrange all the jobs in order with respect to their priorities. There may be two jobs in
queue with
the same priority, and then FCFS approach is to be performed. Each process will be executed according
to its
priority. Calculate the waiting time and turnaround time of each of the processes accordingly.
1.3 PROGRAM
#include<stdio.h>
#include<conio.h>
main()
clrscr();
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d", &bt[i]);
wt[0] = wtavg = 0;
for(i=1;i<n;i++)
for(i=0;i<n;i++)
getch();
INPUT
OUTPUT
P0 24 0 24
P1 3 24 27
P2 3 27 30
#include<stdio.h>
#include<conio.h>
main()
clrscr();
scanf("%d", &n);
for(i=0;i<n;i++)
p[i]=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;
wt[0] = wtavg = 0;
for(i=1;i<n;i++)
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
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]);
getch();
INPUT
OUTPUT
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
#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;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&bu[i]);
ct[i]=bu[i];
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)
for(i=0;i<n;i++)
else
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
bu[i]=bu[i]-t;
temp=temp+t;
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
getch();
INPUT
OUTPUT
1 24 6 30
2347
3 3 7 10
#include<stdio.h>
main()
clrscr();
for(i=0;i<n;i++)
p[i] = i;
for(i=0;i<n;i++)
for(k=i+1;k<n;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;
for(i=1;i<n;i++)
{
for(i=0;i<n;i++)
getch();
INPUT
OUTPUT
11101
42516
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19
EXPERIMENT 2
2.1 OBJECTIVE
*Write a C program to simulate multi-level queue scheduling algorithm considering the following
scenario. All
the processes in the system are divided into two categories – system processes and user processes.
System
processes are to be given higher priority than user processes. The priority of each process ranges from 1
to 3.
2.2 DESCRIPTION
Multi-level queue scheduling algorithm is used in scenarios where the processes can be classified into
groups
based on property like process type, CPU time, IO access, memory size, etc. In a multi-level queue
scheduling
algorithm, there will be 'n' number of queues, where 'n' is the number of groups the processes are
classified
into. Each queue will be assigned a priority and will have its own scheduling algorithm like round-robin
scheduling or FCFS. For the process in a queue to execute, all the queues of priority higher than it should
be
empty, meaning the process in those high priority queues should have completed its execution. In this
scheduling algorithm, once assigned to a queue, the process will not move to any other queues.
2.3 PROGRAM
main()
{
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
p[i] = i;
scanf("%d",&bt[i]);
scanf("%d", &su[i]);
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=su[i];
su[i]=su[k];
su[k]=temp;
wtavg = wt[0] = 0;
for(i=1;i<n;i++)
for(i=0;i<n;i++)
getch();
INPUT
OUTPUT
10202
30123
21538
0 1 3 8 11
EXPERIMENT 3
3.1 OBJECTIVE
3.2 DESCRIPTION
A file is a collection of data, usually stored on disk. As a logical entity, a file enables to divide data into
meaningful groups. As a physical entity, a file should be considered in terms of its organization. The term
"file
organization" refers to the way in which data is stored in a file and, consequently, the method(s) by
which it can
be accessed.
record with sequence number 16 is located just after the 15th record. A record of a sequential file can
only be
With linked allocation, each file is a linked list of disk blocks; the disk blocks may be scattered anywhere
on the
disk. The directory contains a pointer to the first and last blocks of the file. Each block contains a pointer
to the
next block.
Indexed file allocation strategy brings all the pointers together into one location: an index block. Each
file has its
own index block, which is an array of disk-block addresses. The ith entry in the index block points to the
ith block
of the file. The directory contains the address of the index block. To find and read the ith block, the
pointer in the
3.3 PROGRAM
#include<stdio.h>
#include<conio.h>
struct fileTable
char name[20];
}ft[30];
void main()
int i, j, n;
char s[20];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",ft[i].name);
scanf("%d",&ft[i].sb);
scanf("%d",&ft[i].nob);
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
10
if(i==n)
else
}
getch();
break;
printf("\n%s\t\t%d\t\t%d\t",ft[i].name,ft[i].sb,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].sb+j);
INPUT:
Enter no of files :3
OUTPUT:
#include<stdio.h>
#include<conio.h>
struct fileTable
char name[20];
int nob;
}ft[30];
struct block
int bno;
};
void main()
int i, j, n;
char s[20];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",ft[i].name);
11
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
temp = ft[i].sb;
scanf("%d",&temp->bno);
temp->next=NULL;
for(j=1;j<ft[i].nob;j++)
temp = temp->next;
scanf("%d",&temp->bno);
temp->next = NULL;
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
else
temp=ft[i].sb;
for(j=0;j<ft[i].nob;j++)
printf("%d ",temp->bno);
temp = temp->next;
getch();
INPUT:
Enter no of files 2
Enter file 1 : A
Enter file 2 : G
OUTPUT:
#include<stdio.h>
#include<conio.h>
struct fileTable
char name[20];
12
}ft[30];
void main()
int i, j, n;
char s[20];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",ft[i].name);
scanf("%d",&ft[i].nob);
for(j=0;j<ft[i].nob;j++)
scanf("%d",&ft[i].blocks[j]);
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
else
getch();
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].blocks[j]);
INPUT:
Enter no of files 2
Enter file 1 : A
Enter file 2 : G
OUTPUT:
13
EXPERIMENT 4
4.1 OBJECTIVE
Write a C program to simulate the MVT and MFT memory management techniques
4.2 DESCRIPTION
MFT (Multiprogramming with a Fixed number of Tasks) is one of the old memory management
techniques in
which the memory is partitioned into fixed size partitions and each job is assigned to a partition. The
memory
assigned to a partition does not change. MVT (Multiprogramming with a Variable number of Tasks) is
the
memory management technique in which each job gets just the amount of memory it needs. That is, the
partitioning of memory is dynamic and changes as jobs enter and leave the system. MVT is a more
``efficient''
user of resources. MFT suffers with the problem of internal fragmentation and MVT suffers with external
fragmentation.
4.3 PROGRAM
#include<stdio.h>
#include<conio.h>
main()
int i,p=0;
clrscr();
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&mp[i]);
printf("\n %d\t\t%d",i+1,mp[i]);
printf("\t\tNO\t\t---");
if(i<n)
else
printf("\t\tYES\t%d",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
getch();
14
INPUT
OUTPUT
1 275 YES 25
2 400 NO -----
3 290 YES 10
4 293 YES 7
#include<stdio.h>
#include<conio.h>
main()
char ch = 'y';
clrscr();
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
scanf("%d",&mp[i]);
if(mp[i]<=temp)
else
break;
for(i=0;i<n;i++)
printf("\n \t%d\t\t%d",i+1,mp[i]);
15
getch();
INPUT
OUTPUT
Memory is Full
Total Memory Available – 1000
1 400
2 275
16
EXPERIMENT 5
5.1 OBJECTIVE
5.2 DESCRIPTION
One of the simplest methods for memory allocation is to divide memory into several fixed-sized
partitions. Each
partition may contain exactly one process. In this multiple-partition method, when a partition is free, a
process is
selected from the input queue and is loaded into the free partition. When the process terminates, the
partition
becomes available for another process. The operating system keeps a table indicating which parts of
memory
are available and which are occupied. Finally, when a process arrives and needs memory, a memory
section
large enough for this process is provided. When it is time to load or swap a process into main memory,
and if
there is more than one free block of memory of sufficient size, then the operating system must decide
which
free block to allocate. Best-fit strategy chooses the block that is closest in size to the request. First-fit
chooses
the first available block that is large enough. Worst-fit chooses the largest available block.
5.3 PROGRAM
5.3.1 WORST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp;
clrscr();
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
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)
ff[i]=j;
break;
17
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
Block 1: 5
Block 2: 2
Block 3: 7
File 1: 1
File 2: 4
OUTPUT
11154
24373
5.3.2 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;
clrscr();
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);scanf("%d",&b[i]);
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;
18
lowest=temp;
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
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
Block 1: 5
Block 2: 2
Block 3: 7
File 1: 1
File 2: 4
OUTPUT
11221
24151
5.3.3 FIRST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
clrscr();
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
19
for(j=1;j<=nb;j++)
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:\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
Block 1: 5
Block 2: 2
Block 3: 7
File 1: 1
File 2: 4
OUTPUT
11376
24151
20
EXPERIMENT 6
6.1 OBJECTIVE
6.2 DESCRIPTION
In computer operating systems, paging is one of the memory management schemes by which a
computer stores
and retrieves data from the secondary storage for use in main memory. In the paging memory-
management
scheme, the operating system retrieves data from secondary storage in same-size blocks called pages.
Paging is a
memory-management scheme that permits the physical address space a process to be noncontiguous.
The basic
method for implementing paging involves breaking physical memory into fixed-sized blocks called
frames and
breaking logical memory into blocks of the same size called pages. When a process is to be executed, its
pages
are loaded into any available memory frames from their source.
6.3 PROGRAM
#include<stdio.h>
#include<conio.h>
main()
printf("\nMemory is Full");
break;
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
21
else
getch();
pa=fno[x][y]*ps+offset;
INPUT
OUTPUT
Memory is Full
22
EXPERIMENT 7
7.1 OBJECTIVE
7.2 DESCRIPTION
The directory structure is the organization of files into a hierarchy of folders. In a single-level directory
system,
all the files are placed in one directory. There is a root directory which has all files. It has a simple
architecture
and there are no sub directories. Advantage of single level directory system is that it is easy to find a file
in the
directory. In the two-level directory system, each user has own user file directory (UFD). The system
maintains a
master block that has one entry for each user. This master block contains the addresses of the directory
of the
users. When a user job starts or a user logs in, the system's master file directory (MFD) is searched.
When a user
refers to a particular file, only his own UFD is searched. This effectively solves the name collision
problem and
isolates users from one another. Hierarchical directory structure allows users to create their own
subdirectories
and to organize their files accordingly. A tree is the most common directory structure. The tree has a
root
directory, and every file in the system has a unique path name. A directory (or subdirectory) contains a
set of
files or subdirectories.
7.3 PROGRAM
#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;
scanf("%s", dir.dname);
while(1)
scanf("%d",&ch);
switch(ch)
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
if(strcmp(f, dir.fname[i])==0)
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
if(i==dir.fcnt)
23
else
break;
dir.fcnt--;
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
if(strcmp(f, dir.fname[i])==0)
break;
}
if(i==dir.fcnt)
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
break;
default: exit(0);
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
getch();
OUTPUT:
24
File B is deleted
#include<stdio.h>
struct
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
clrscr();
dcnt=0;
while(1)
scanf("%d",&ch);
switch(ch)
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
}
if(i==dcnt)
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
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)
25
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
goto jmp;
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)
goto jmp1;
jmp1: break;
case 5: if(dcnt==0)
else
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
break;
default:exit(0);
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
getch();
OUTPUT:
Directory created
26
Directory created
File created
File created
File created
1. Create Directory
4. Search File
2. Create File
5. Display
3. Delete File
Directory
DIR1
Files
A1 A2
DIR2 B1
1. Create Directory
4. Search File
2. Create File
5. Display
3. Delete File
File A2 is deleted
#include<graphics.h>
struct tree_element
char name[20];
};
void main()
27
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
getch();
closegraph();
{
int i, gap;
if(*root==NULL)
(*root)=(node *)malloc(sizeof(node));
fflush(stdin);
gets((*root)->name);
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)
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
else
}
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);
(*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]);
28
}
INPUT
No of subdirectories/files(for ROOT): 2
No of subdirectories/files(for USER1): 1
No of subdirectories/files(for SUBDIR1): 2
No of subdirectories/files(for JAVA): 0
No of subdirectories/files(for VB): 0
No of subdirectories/files(for USER2): 2
No of subdirectories/files(for PPL): 2
No of subdirectories/files(for AI): 2
OUTPUT
A SUBDIR
BC
PPL
DE
AI
USER1 USER2
ROOT
SUBDIR
VBJAVA
29
EXPERIMENT 8
8.1 OBJECTIVE
Write a C program to simulate Bankers algorithm for the purpose of deadlock avoidance.
8.2 DESCRIPTION
In a multiprogramming environment, several processes may compete for a finite number of resources. A
process
requests resources; if the resources are not available at that time, the process enters a waiting state.
Sometimes, a waiting process is never again able to change state, because the resources it has
requested are
held by other waiting processes. This situation is called a deadlock. Deadlock avoidance is one of the
techniques
for handling deadlocks. This approach requires that the operating system be given in advance additional
information concerning which resources a process will request and use during its lifetime. With this
additional
knowledge, it can decide for each request whether or not the process should wait. To decide whether
the
current request can be satisfied or must be delayed, the system must consider the resources currently
available,
the resources currently allocated to each process, and the future requests and releases of each process.
Banker’s algorithm is a deadlock avoidance algorithm that is applicable to a system with multiple
instances of
8.3 PROGRAM
#include<stdio.h>
struct file
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
int fl;
int avail[10],seq[10];
clrscr();
scanf("%d",&n);
scanf("%d",&r);
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
scanf("%d",&id);
for(i=0;i<r;i++)
scanf("%d",&newr);
f[id].all[i] += newr;
30
avail[i]=avail[i] - newr;
for(i=0;i<n;i++)
for(j=0;j<r;j++)
cnt=0;
fl=0;
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
while(cnt!=n)
g=0;
for(j=0;j<n;j++)
if(f[j].flag==0)
b=0;
for(p=0;p<r;p++)
if(avail[p]>=f[j].need[p])
b=b+1;
if(b==r)
else
b=b-1;
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;
for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("(");
for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
if(g==0)
goto y;
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
printf("P%d\t",i);
for(j=0;j<r;j++)
31
}
getch();
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
INPUT
-- 3
Enter allocation -- 0 1 0
Enter Max -- 7 5 3
Enter allocation -- 2 0 0
Enter Max -- 3 2 2
Enter allocation -- 3 0 2
Enter Max -- 9 0 2
Enter allocation -- 2 1 1
Enter Max -- 2 2 2
Enter details for P4
Enter allocation -- 0 0 2
Enter Max -- 4 3 3
Enter pid -- 1
OUTPUT
P1 is visited( 5 3 2)
P3 is visited( 7 4 3)
P4 is visited( 7 4 5)
P0 is visited( 7 5 5)
P2 is visited( 10 5 7)
P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
32
EXPERIMENT 9
9.1 OBJECTIVE
9.2 DESCRIPTION
One of the responsibilities of the operating system is to use the hardware efficiently. For the disk drives,
meeting
this responsibility entails having fast access time and large disk bandwidth. Both the access time and the
bandwidth can be improved by managing the order in which disk I/O requests are serviced which is
called as disk
scheduling. The simplest form of disk scheduling is, of course, the first-come, first-served (FCFS)
algorithm. This
algorithm is intrinsically fair, but it generally does not provide the fastest service. In the SCAN algorithm,
the disk
arm starts at one end, and moves towards the other end, servicing requests as it reaches each cylinder,
until it
gets to the other end of the disk. At the other end, the direction of head movement is reversed, and
servicing
continues. The head continuously scans back and forth across the disk. C-SCAN is a variant of SCAN
designed to
provide a more uniform wait time. Like SCAN, C-SCAN moves the head from one end of the disk to the
other,
servicing requests along the way. When the head reaches the other end, however, it immediately
returns to the
beginning of the disk without servicing any requests on the return trip
9.3 PROGRAM
#include<stdio.h>
main()
float avhm;
clrscr();
scanf(“%d”,&n);
for(i=2;i<n+2;i++)
scanf(“%d”,&t*i+);
for(i=1;i<n+1;i++)
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
for(i=1;i<n+1;i++)
printf(“%d\t\t\t%d\n”,t*i+,tohm*i+);
getch();
INPUT
OUTPUT
Tracks traversed Difference between tracks
55
58
60
70
18
90
45
10
52
72
33
150
160
184
60
10
24
#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
scanf("%d'",&n);
scanf("%d",&h);
t[0]=0;t[1]=h;
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];
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];
getch();
INPUT
OUTPUT
150 50
34
160
184
90
70
60
58
55
18
10
24
94
20
10
37
#include<stdio.h>
main()
clrscr();
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h);
t[0]=0;t[1]=h;
scanf("%d",&tot);
t[2]=tot-1;
for(i=3;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])
for(i=0;i<=n+2;i++)
if(t[i]==h)
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
p=0;
while(t[j]!=tot-1)
j=i;break;
atr[p]=t[j];
j++;
p++;
atr[p]=t[j];
p++;
i=0;
atr[p]=t[i];
i++;
p++;
for(j=0;j<n+2;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];
35
printf("avg is %f",(float)sum/n);
getch();
INPUT
Enter the track position : 55 58 60 70 18 90 150 160 184
OUTPUT
150
160
184
18
55
58
60
70
90
50
10
24
240
37
10
20
36
EXPERIMENT 10
10.1 OBJECTIVE
10.2 DESCRIPTION
Page replacement is basic to demand paging. It completes the separation between logical memory and
physical
memory. With this mechanism, an enormous virtual memory can be provided for programmers on a
smaller
physical memory. There are many different page-replacement algorithms. Every operating system
probably has
its own replacement scheme. A FIFO replacement algorithm associates with each page the time when
that page
was brought into memory. When a page must be replaced, the oldest page is chosen. If the recent past
is used
as an approximation of the near future, then the page that has not been used for the longest period of
time can
be replaced. This approach is the Least Recently Used (LRU) algorithm. LRU replacement associates with
each
page the time of that page's last use. When a page must be replaced, LRU chooses the page that has not
been
used for the longest period of time. Least frequently used (LFU) page-replacement algorithm requires
that the
page with the smallest count be replaced. The reason for this selection is that an actively used page
should have
10.3 PROGRAM
#include<stdio.h>
#include<conio.h>
main()
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
for(i=0;i<n;i++)
for(k=0;k<f;k++)
if(k==f)
if(m[k]==rs[i])
break;
m[count++]=rs[i];
pf++;
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\n");
if(count==f)
count=0;
getch();
37
INPUT
OUTPUT
7 -1 -1 PF No. 1
7 0 -1 PF No. 2
7 0 1 PF No. 3
2 0 1 PF No. 4
201
2 3 1 PF No. 5
2 3 0 PF No. 6
4 3 0 PF No. 7
4 2 0 PF No. 8
4 2 3 PF No. 9
0 2 3 PF No. 10
023
023
0 1 3 PF No. 11
0 1 2 PF No. 12
012
012
7 1 2 PF No. 13
7 0 2 PF No. 14
7 0 1 PF No. 15
#include<stdio.h>
#include<conio.h>
main()
clrscr();
scanf("%d",&n);
scanf("%d",&rs[i]);
flag[i]=0;
scanf("%d",&f);
for(i=0;i<f;i++)
count[i]=0;
m[i]=-1;
for(i=0;i<n;i++)
for(j=0;j<f;j++)
if(m[j]==rs[i])
flag[i]=1;
38
if(flag[i]==0)
{
count[j]=next;
next++;
if(i<f)
else
pf++;
m[i]=rs[i];
count[i]=next;
next++;
min=0;
for(j=1;j<f;j++)
min=j;
m[min]=rs[i];
count[min]=next;
next++;
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("\n");
}
getch();
INPUT
OUTPUT
7 -1 -1 PF No. -- 1
7 0 -1 PF No. -- 2
7 0 1 PF No. -- 3
2 0 1 PF No. -- 4
201
2 0 3 PF No. -- 5
203
4 0 3 PF No. – 6
4 0 2 PF No. – 7
4 3 2 PF No. – 8
0 3 2 PF No. – 9
032
032
1 3 2 PF No. – 10
132
1 0 2 PF No. – 11
102
1 0 7 PF No. – 12
107
39
107
#include<stdio.h>
#include<conio.h>
main()
clrscr();
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
scanf("%d",&f);
for(i=0;i<f;i++)
cntr[i]=0;
a[i]=-1;
}
for(i=0;i<m;i++)
for(j=0;j<f;j++)
if(rs[i]==a[j])
if(j==f)
min = 0;
cntr[j]++;
break;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i];
cntr[min]=1;
pf++;
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
}
printf(“\tPF No. %d”,pf);
getch();
INPUT
40
OUTPUT
1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
523
523
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8
41
EXPERIMENT 11
11.1 OBJECTIVE
*Write a C program to simulate page replacement algorithms
a) Optimal
11.2 DESCRIPTION
Optimal page replacement algorithm has the lowest page-fault rate of all algorithms and will never
suffer from
Belady's anomaly. The basic idea is to replace the page that will not be used for the longest period of
time. Use
of this page-replacement algorithm guarantees the lowest possible page fault rate for a fixed number of
frames.
11.3 PROGRAM
#include<stdio.h>
int n;
main()
int seq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s;
int count=1,pf=0,p=0;
float pfr;
clrscr();
scanf("%d",&max);
for(i=0;i<max;i++)
scanf("%d",&seq[i]);
fr[0]=seq[0];
pf++;
printf("%d\t",fr[0]);
i=1;
while(count<n)
flag=1;
p++;
for(j=0;j<i;j++)
if(seq[i]==seq[j])
flag=0;
if(flag!=0)
i++;
fr[count]=seq[i];
printf("%d\t",fr[count]);
count++;
pf++;
printf("\n");
for(i=p;i<max;i++)
{
flag=1;
for(j=0;j<n;j++)
if(seq[i]==fr[j])
flag=0;
if(flag!=0)
42
for(j=0;j<n;j++)
m=fr[j];
for(k=i;k<max;k++)
if(seq[k]==m)
else
pos[j]=k;
break;
pos[j]=1;
for(k=0;k<n;k++)
{
if(pos[k]==1)
flag=0;
if(flag!=0)
s=findmax(pos);
if(flag==0)
for(k=0;k<n;k++)
if(pos[k]==1)
s=k;
break;
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%d\t",fr[k]);
pf++;
printf("\n");
pfr=(float)pf/(float)max;
printf("\nThe no. of page faults are %d",pf);
getch();
int max,i,k=0;
max=a[0];
for(i=0;i<n;i++)
if(max<a[i])
INPUT
return k;
max=a[i];
k=i;
43
OUTPUT
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
523
523
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8
44
EXPERIMENT 12
12.1 OBJECTIVE
12.2 DESCRIPTION
information that is consumed by a consumer process. One solution to the producer-consumer problem
uses
shared memory. To allow producer and consumer processes to run concurrently, there must be
available a
buffer of items that can be filled by the producer and emptied by the consumer. This buffer will reside in
a
region of memory that is shared by the producer and consumer processes. A producer can produce one
item
while the consumer is consuming another item. The producer and consumer must be synchronized, so
that the
consumer does not try to consume an item that has not yet been produced.
12.3 PROGRAM
#include<stdio.h>
void main()
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
scanf(“%d”, &choice);
switch(choice) {
case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
Break;
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
case 2: if(in == out)
printf(“\nBuffer is Empty”);
}}}
else
break;
consume = buffer[out];
out = (out+1)%bufsize;
OUTPUT
Buffer is Empty
45
EXPERIMENT 13
13.1 OBJECTIVE
*Write a C program to simulate the concept of Dining-Philosophers problem.
13.2 DESCRIPTION
large class of concurrency-control problems. It is a simple representation of the need to allocate several
resources among several processes in a deadlock-free and starvation-free manner. Consider five
philosophers
who spend their lives thinking and eating. The philosophers share a circular table surrounded by five
chairs, each
belonging to one philosopher. In the center of the table is a bowl of rice, and the table is laid with five
single
chopsticks. When a philosopher thinks, she does not interact with her colleagues. From time to time, a
philosopher gets hungry and tries to pick up the two chopsticks that are closest to her (the chopsticks
that are
between her and her left and right neighbors). A philosopher may pick up only one chopstick at a time.
Obviously, she cam1ot pick up a chopstick that is already in the hand of a neighbor. When a hungry
philosopher
has both her chopsticks at the same time, she eats without releasing her chopsticks. When she is
finished eating,
she puts down both of her chopsticks and starts thinking again. The dining-philosophers problem may
lead to a
deadlock situation and hence some rules have to be framed to avoid the occurrence of deadlock.
13.3 PROGRAM
main()
int i;
clrscr();
scanf("%d",&tph);
for(i=0;i<tph;i++)
philname[i] = (i+1);
status[i]=1;
scanf("%d", &howhung);
if(howhung==tph)
else
printf("\nExiting..");
for(i=0;i<howhung;i++)
scanf("%d", &hu[i]);
status[hu[i]]=2;
do
switch(cho)
case 1: one();
break;
case 2: two();
break;
case 3: exit(0);
46
}while(1);
one()
int pos=0, x, i;
two()
int i, j, s=0, t, r, x;
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)
t=hu[i];
r=hu[j];
s++;
philname[hu[j]]);
for(x=0;x<howhung;x++)
if((hu[x]!=t)&&(hu[x]!=r))
}
INPUT
OUTPUT
P 3 is granted to eat
P 3 is waiting
P 5 is waiting
P 0 is waiting
P 5 is granted to eat
P 5 is waiting
P 0 is waiting
P 0 is granted to eat
P 0 is waiting
47
combination 1
P 3 and P 5 are granted to eat
P 0 is waiting
combination 2
P 5 is waiting
combination 3
P 3 is waiting
48
ASSIGNMENTS
49
EXPERIMENT 1
3. Define a process?
3. In general, which CPU scheduling algorithm works with highest waiting time?
4. Is it possible to use optimal CPU scheduling algorithm in practice?
5. What is the real difficulty with the SJF CPU scheduling algorithm?
1. Write a C program to implement round robin CPU scheduling algorithm for the following given
scenario. All
the processes in the system are divided into two categories – system processes and user processes.
System
processes are to be given higher priority than user processes. Consider the time quantum size for the
EXPERIMENT 2
2. Differentiate between the general CPU scheduling algorithms like FCFS, SJF etc and multi-level queue
CPU
Scheduling?
1. What are the parameters to be considered for designing a multilevel feedback queue scheduler?
2. Differentiate multi-level queue and multi-level feedback queue CPU scheduling algorithms?
3. What are the advantages of multi-level queue and multi-level feedback queue CPU scheduling
algorithms?
1. Write a C program to simulate multi-level queue scheduling algorithm considering the following
scenario.
All the processes in the system are divided into two categories – system processes and user processes.
System processes are to be given higher priority than user processes. Consider each process priority to
be
from 1 to 3. Use priority scheduling for the processes in each queue.
EXPERIMENT 3
1. Define file?
1. Identify ideal scenarios where sequential, indexed and linked file allocation strategies are most
appropriate?
50
EXPERIMENT 4
EXPERIMENT 5
1. Which of the dynamic contiguous memory allocation strategies suffer with external fragmentation?
2. What are the possible solutions for the problem of external fragmentation?
4. What is compaction?
5. Which of the memory allocation techniques first-fit, best-fit, worst-fit is efficient? Why?
EXPERIMENT 6
2. What is the process of mapping a logical address to physical address with respect to the paging
memory
management technique?
3. Whether the paging memory management technique suffers with internal or external fragmentation
problem. Why?
4. What is the effect of paging on the overall context-switching time?
51
EXPERIMENT 7
1. Define directory?
2. Which directory structure does not provide user-level isolation and protection?
EXPERIMENT 8
2. What is deadlock?
1. How can be the resource allocation graph used to identify a deadlock situation?
1. Write a C program to implement deadlock detection technique for the following scenarios?
EXPERIMENT 9
3. Define the terms – disk seek time, disk access time and rotational latency?
EXPERIMENT 10
52
1. Which page replacement algorithm suffers with the problem of Belady’s anomaly?
2. Define the concept of thrashing? What is the scenario that leads to the situation of thrashing?
b. Second-chance algorithm
EXPERIMENT 11
1. What are the benefits of optimal page replacement algorithm over other page replacement
algorithms?
EXPERIMENT 12
2. Define a semaphore?
2. Can producer and consumer processes access the shared memory concurrently? If not which
technique
EXPERIMENT 13
1. Identify the scenarios in the dining-philosophers problem that leads to the deadlock situations?