Operating Systems Lab Manual
Operating Systems Lab Manual
in
JNTU World
OPERATING SYSTEMS
JN
TU
or
ld
LAB MANUAL
www.alljntuworld.in
JNTU World
Program Outcomes
PO3
PO4
PO5
PO6
PO7
PO8
PO9
TU
PO10
or
ld
PO2
Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and
an engineering specialization to the solution of complex engineering problems.
Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and
engineering sciences.
Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.
Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice.
Individual and team work: Function effectively as an individual, and as a member or leader in diverse
teams, and in multidisciplinary settings.
Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
Project management and finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to ones own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.
Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
PO1
PO11
PO12
Professional Skills: The ability to research, understand and implement computer programs in the areas
related to algorithms, system software, multimedia, web design, big data analytics, and networking for
efficient analysis and design of computer-based systems of varying complexity.
Problem-Solving Skills: The ability to apply standard practices and strategies in software project
development using open-ended programming environments to deliver a quality product for business
success.
Successful Career and Entrepreneurship: The ability to employ modern computer languages,
environments, and platforms in creating innovative career paths, to be an entrepreneur, and a zest for
higher studies.
JN
PSO1
PSO2
PSO3
www.alljntuworld.in
JNTU World
Division of
Experiments
List of Experiments
CPU
Scheduling
Algorithms
File
Allocation
Strategies
13
16
20
22
29
3
4
5
Memory
Management
Techniques
6
7
Deadlock
Management
Techniques
TU
File
Organization
Techniques
10
11
12
Page
Replacement
Algorithms
32
36
41
44
45
13
Process
Synchronization
14
JN
Page
No.
or
ld
Exp.
No.
47
www.alljntuworld.in
JNTU World
Exp.
No.
Program
Specific
Outcomes
Attained
PSO1
or
ld
Experiment
Program
Outcomes
Attained
PSO1, PSO2
PSO1
PSO1
PSO1
PSO1
PSO1
PSO1
PSO1
10
PO1, PO2
PSO1
11
PSO1
12
PSO1, PSO2
PSO1, PSO2
JN
TU
13
www.alljntuworld.in
JNTU World
OUTCOMES:
or
ld
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.
Upon the completion of Operating Systems practical course, the student will be able to:
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.
5.
6.
7.
Understand the concepts of deadlock in operating systems and implement them in multiprogramming
system.
JN
TU
1.
www.alljntuworld.in
JNTU World
EXPERIMENT 1
1.1
OBJECTIVE
Write a C program to simulate the following non-preemptive CPU scheduling algorithms to find turnaround time
and waiting time for the above problem.
a) FCFS
b) SJF
c) Round Robin d) Priority
1.2
DESCRIPTION
Assume all the processes arrive at the same time.
FCFS CPU SCHEDULING ALGORITHM
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
the processes accordingly.
1.2.2
1.2.3
1.2.4
1.3
PROGRAM
1.3.1
JN
TU
or
ld
1.2.1
2
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
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
PROCESS
P0
P1
P2
BURST TIME
24
3
3
3
24
3
3
WAITING TIME
0
24
27
TURNAROUND TIME
24
27
30
or
ld
INPUT
Enter the number of processes -Enter Burst Time for Process 0 -Enter Burst Time for Process 1 -Enter Burst Time for Process 2 --
TU
1.3.2
JN
}
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\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
3
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
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("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}
INPUT
Enter the number of processes -Enter Burst Time for Process 0 -Enter Burst Time for Process 1 -Enter Burst Time for Process 2 -Enter Burst Time for Process 3 --
4
6
8
7
3
1.3.3
TURNAROUND TIME
3
9
16
24
or
ld
OUTPUT
PROCESS
BURST TIME
WAITING TIME
P3
3
0
P0
6
3
P2
7
9
P1
8
16
Average Waiting Time -7.000000
Average Turnaround Time -13.000000
JN
TU
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];
4
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
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 \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
getch();
24
3
3
or
ld
}
INPUT
Enter the no of processes 3
Enter Burst Time for process 1
Enter Burst Time for process 2 -Enter Burst Time for process 3 --
OUTPUT
The Average Turnaround time is 15.666667
The Average Waiting time is -5.666667
PROCESS
1
2
3
WAITING TIME
6
4
7
TURNAROUND TIME
30
7
10
TU
1.3.4
BURST TIME
24
3
3
JN
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;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
5
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
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]);
or
ld
INPUT
Enter the number of processes -- 5
Enter the Burst Time & Priority of Process 0 --Enter the Burst Time & Priority of Process 1 --Enter the Burst Time & Priority of Process 2 --Enter the Burst Time & Priority of Process 3 --Enter the Burst Time & Priority of Process 4 ---
10
1
2
1
5
3
1
4
5
2
WAITING TIME
0
1
6
16
18
JN
TU
OUTPUT
PROCESS
PRIORITY
BURST TIME
1
1
1
4
2
5
0
3
10
2
4
2
3
5
1
Average Waiting Time is --- 8.200000
Average Turnaround Time is --- 12.000000
6
Downloaded From JNTU World (http://www.alljntuworld.in)
TURNAROUND TIME
1
6
16
18
19
www.alljntuworld.in
JNTU World
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.
Use fixed priority scheduling for all the processes.
2.2
DESCRIPTION
2.3
or
ld
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.
PROGRAM
main()
{
int p[20],bt[20], su[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 of Process %d --- ", i);
scanf("%d",&bt[i]);
printf("System/User Process (0/1) ? --- ");
scanf("%d", &su[i]);
JN
TU
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(su[i] > su[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;
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];
7
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\t SYSTEM/USER PROCESS \tBURST TIME\tWAITING TIME\tTURNAROUND
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],su[i],bt[i],wt[i],tat[i]);
TIME");
INPUT
Enter the number of processes --- 4
Enter the Burst Time of Process 0 --- 3
System/User Process (0/1) ? --- 1
Enter the Burst Time of Process 1 --- 2
System/User Process (0/1) ? --- 0
Enter the Burst Time of Process 2 --- 5
System/User Process (0/1) ? --- 1
Enter the Burst Time of Process 3 --- 1
System/User Process (0/1) ? --- 0
SYSTEM/USER PROCESS
0
0
1
1
BURST TIME
2
1
5
3
OUTPUT
PROCESS
1
3
2
0
or
ld
JN
TU
8
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 3
3.1
OBJECTIVE
Write a C program to simulate the following file allocation strategies.
a) Sequential
b) Linked
c) ) Indexed
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.
SEQUENTIAL FILE ALLOCATION
In this file organization, the records of the file are stored one after another both physically and logically. That is,
record with sequence number 16 is located just after the 15th record. A record of a sequential file can only be
accessed by reading all the previous records.
3.2.2
3.2.3
3.3
PROGRAM
3.3.1
or
ld
3.2.1
TU
struct fileTable
{
char name[20];
int sb, nob;
}ft[30];
JN
void main()
{
int i, j, n;
char s[20];
clrscr();
printf("Enter no of files
scanf("%d",&n);
:");
for(i=0;i<n;i++)
{
printf("\nEnter file name %d
:",i+1);
scanf("%s",ft[i].name);
printf("Enter starting block of file %d
:",i+1);
scanf("%d",&ft[i].sb);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
}
printf("\nEnter the file name to be searched -- ");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
9
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME START BLOCK NO OF BLOCKS BLOCKS OCCUPIED\n");
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);
}
getch();
}
START BLOCK
102
NO OF BLOCKS
4
BLOCKS OCCUPIED
102, 103, 104, 105
TU
3.3.2
or
ld
INPUT:
Enter no of files :3
struct fileTable
{
char name[20];
int nob;
struct block *sb;
}ft[30];
JN
struct block
{
int bno;
struct block *next;
};
void main()
{
int i, j, n;
char s[20];
struct block *temp;
clrscr();
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d
scanf("%s",ft[i].name);
:",i+1);
10
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
ft[i].sb=(struct block*)malloc(sizeof(struct block));
temp = ft[i].sb;
printf("Enter the blocks of the file :");
scanf("%d",&temp->bno);
temp->next=NULL;
for(j=1;j<ft[i].nob;j++)
{
temp->next = (struct block*)malloc(sizeof(struct block));
temp = temp->next;
scanf("%d",&temp->bno);
}
temp->next = NULL;
or
ld
}
printf("\nEnter the file name to be searched -- ");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME NO OF BLOCKS BLOCKS OCCUPIED");
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob);
temp=ft[i].sb;
for(j=0;j<ft[i].nob;j++)
{
printf("%d ",temp->bno);
temp = temp->next;
}
}
getch();
TU
}
INPUT:
Enter no of files
:2
: 12 23 9 4
Enter file 2 : G
Enter no of blocks in file 2 : 5
Enter the blocks of the file 2
: 88 77 66 55 44
JN
Enter file 1 : A
Enter no of blocks in file 1 : 4
Enter the blocks of the file 1
3.3.3
NO OF BLOCKS
5
BLOCKS OCCUPIED
88 77 66 55 44
11
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
}ft[30];
or
ld
void main()
{
int i, j, n;
char s[20];
clrscr();
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d
:",i+1);
scanf("%s",ft[i].name);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
printf("Enter the blocks of the file :");
for(j=0;j<ft[i].nob;j++)
scanf("%d",&ft[i].blocks[j]);
}
TU
}
INPUT:
Enter no of files
:2
Enter file 1 : A
Enter no of blocks in file 1 : 4
Enter the blocks of the file 1
: 12 23 9 4
JN
Enter file 2 : G
Enter no of blocks in file 2 : 5
Enter the blocks of the file 2
: 88 77 66 55 44
Enter the file to be searched : G
OUTPUT:
FILE NAME
G
NO OF BLOCKS
5
BLOCKS OCCUPIED
88, 77, 66, 55, 44
12
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 4
4.1
OBJECTIVE
Write a C program to simulate the MVT and MFT memory management techniques
4.2
DESCRIPTION
or
ld
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
4.3.1
TU
clrscr();
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
printf("\nEnter the number of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter memory required for process %d (in Bytes)-- ",i+1);
scanf("%d",&mp[i]);
}
JN
13
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
}
INPUT
Enter the total memory available (in Bytes) -Enter the block size (in Bytes)-300
Enter the number of processes 5
Enter memory required for process 1 (in Bytes) -Enter memory required for process 2 (in Bytes) -Enter memory required for process 3 (in Bytes) -Enter memory required for process 4 (in Bytes) -Enter memory required for process 5 (in Bytes) -No. of Blocks available in memory --
275
400
290
293
100
MEMORY REQUIRED
275
400
290
293
ALLOCATED
YES
NO
YES
YES
INTERNAL FRAGMENTATION
25
----10
7
or
ld
OUTPUT
PROCESS
1
2
3
4
1000
4.3.2
main()
{
int ms,mp[10],i, temp,n=0;
char ch = 'y';
JN
TU
clrscr();
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("\nMemory is allocated for Process %d ",i+1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue(y/n) -- ");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for(i=0;i<n;i++)
printf("\n \t%d\t\t%d",i+1,mp[i]);
printf("\n\nTotal Memory Allocated is %d",ms-temp);
printf("\nTotal External Fragmentation is %d",temp);
14
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
getch();
}
INPUT
Enter the total memory available (in Bytes) --
1000
400
Enter memory required for process 2 (in Bytes) -Memory is allocated for Process 2
Do you want to continue(y/n) -y
275
550
OUTPUT
Memory is Full
Total Memory Available -- 1000
PROCESS
1
2
MEMORY ALLOCATED
400
275
325
JN
TU
or
ld
Enter memory required for process 1 (in Bytes) -Memory is allocated for Process 1
Do you want to continue(y/n) -y
15
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 5
5.1
OBJECTIVE
*Write a C program to simulate the following contiguous memory allocation techniques
a) Worst-fit
b) Best-fit
c) First-fit
5.2
DESCRIPTION
5.3
PROGRAM
5.3.1
WORST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
or
ld
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.
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
JN
TU
16
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
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();
BEST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
Block No
1
3
Block Size
5
7
Fragment
4
3
5.3.2
File Size
1
4
or
ld
}
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
TU
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);
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]);
JN
17
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
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();
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
1
2
Block Size
2
5
FIRST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
Block No
2
1
Fragment
1
1
TU
5.3.3
File Size
1
4
or
ld
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
JN
18
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
or
ld
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;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
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
TU
OUTPUT
File No
1
2
Block No
3
1
Block Size
7
5
Fragment
6
1
JN
File Size
1
4
19
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 6
6.1
OBJECTIVE
Write a C program to simulate paging technique of memory management.
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.
PROGRAM
or
ld
6.3
#include<stdio.h>
#include<conio.h>
main()
{
nop = ms/ps;
printf("\nThe no. of pages available in memory are -- %d ",nop);
TU
for(i=1;i<=np;i++)
{
JN
}
printf("\nEnter Logical Address to find Physical Address ");
printf("\nEnter process no. and pagenumber and offset -- ");
scanf("%d %d %d",&x,&y, &offset);
20
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
Enter no. of pages required for p[2] -Enter pagetable for p[2] --1
60
or
ld
INPUT
Enter the memory size 1000
Enter the page size -100
The no. of pages available in memory are -- 10
Enter number of processes -3
Enter no. of pages required for p[1] -4
Enter pagetable for p[1] --8
6
5
4
JN
TU
21
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 7
7.1
OBJECTIVE
Write a C program to simulate the following file organization techniques
a) Single level directory
b) Two level directory
c) Hierarchical
7.2
DESCRIPTION
or
ld
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.
PROGRAM
7.3.1
7.3
JN
TU
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;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
22
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
else
dir.fcnt--;
break;
or
ld
}
getch();
}
TU
OUTPUT:
Enter name of directory -- CSE
1. Create File
2. Delete File 3. Search File
4. Display Files 5. Exit
Enter your choice 1
Enter the name of the file -- A
1. Create File
4. Display Files
JN
1. Create File
4. Display Files
23
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
clrscr();
dcnt=0;
while(1)
{
or
ld
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
JN
TU
24
Downloaded From JNTU World (http://www.alljntuworld.in)
");
www.alljntuworld.in
JNTU World
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
JN
TU
or
ld
}
getch();
}
OUTPUT:
1. Create Directory
4. Search File
2. Create File
5. Display
3. Delete File
6. Exit
Enter your choice --
25
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
1. Create Directory
2. Create File
4. Search File
5. Display
Enter name of directory -- DIR2
Directory created
3. Delete File
6. Exit
Enter your choice --
1. Create Directory
4. Search File
3. Delete File
6. Exit
Enter your choice --
3. Delete File
6. Exit
Enter your choice --
2. Create File
5. Display
2. Create File
5. Display
Directory
DIR1
DIR2
Files
A1
B1
3. Delete File
6. Exit
Enter your choice --
3. Delete File
6. Exit
Enter your choice --
3. Delete File
6. Exit
Enter your choice --
3. Delete File
6. Exit
Enter your choice --
A2
2. Create File
5. Display
TU
1. Create Directory
4. Search File
2. Create File
5. Display
3. Delete File
6. Exit
Enter your choice --
1. Create Directory
4. Search File
or
ld
1. Create Directory
4. Search File
2. Create File
5. Display
JN
7.3.3
2. Create File
5. Display
26
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
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();
JN
TU
or
ld
}
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]);
27
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
TU
or
ld
}
}
INPUT
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): USER1
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for USER1): 1
Enter Name of dir/file(under USER1): SUBDIR1
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for SUBDIR1): 2
Enter Name of dir/file(under USER1): JAVA
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for JAVA): 0
Enter Name of dir/file(under SUBDIR1): VB
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for VB): 0
Enter Name of dir/file(under ROOT): USER2
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for USER2): 2
Enter Name of dir/file(under ROOT): A
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under USER2): SUBDIR2
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for SUBDIR2): 2
Enter Name of dir/file(under SUBDIR2): PPL
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for PPL): 2
Enter Name of dir/file(under PPL): B
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under PPL): C
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under SUBDIR): AI
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for AI): 2
Enter Name of dir/file(under AI): D
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under AI): E
Enter 1 for Dir/2 for File: 2
OUTPUT
JN
ROOT
USER1
USER2
SUBDIR
1
JAVA
SUBDIR
2
VB
PPL
AI
28
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 8
8.1
OBJECTIVE
Write a C program to simulate Bankers algorithm for the purpose of deadlock avoidance.
8.2
DESCRIPTION
PROGRAM
#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
clrscr();
printf("Enter number of processes -- ");
scanf("%d",&n);
printf("Enter number of resources -- ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i);
printf("\nEnter allocation\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t -- \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
JN
TU
8.3
or
ld
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.
Bankers algorithm is a deadlock avoidance algorithm that is applicable to a system with multiple instances of
each resource type.
29
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
avail[i]=avail[i] - newr;
}
JN
TU
or
ld
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=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;
else
b=b-1;
}
if(b==r)
{
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)
{
printf("\n REQUEST NOT GRANTED -- DEADLOCK OCCURRED");
printf("\n SYSTEM IS IN UNSAFE STATE");
goto y;
}
}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe Safe Sequence is -- (");
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++)
30
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
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");
}
getch();
}
--
5
3
1
7
0
5
0
2
0
2
0
0
2
2
1
2
1
2
---
2
3
---
3
9
---
2
2
or
ld
INPUT
Enter number of processes
Enter number of resources
Enter details for P0
Enter allocation
-Enter Max
--
---
0
3
--
2
3
TU
0
4
JN
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)
SYSTEM IS IN SAFE STATE
The Safe Sequence is -- (P1 P3 P4 P0 P2 )
Process
P0
P1
P2
P3
P4
Allocation
0 1 0
3 0 2
3 0 2
2 1 1
0 0 2
7
3
9
2
4
Max
5
2
0
2
3
3
2
2
2
3
Need
7 4 3
0 2 0
6 0 0
0 1 1
4 3 1
31
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 9
9.1
OBJECTIVE
*Write a C program to simulate disk scheduling algorithms
a) FCFS
b) SCAN
c) C-SCAN
9.2
DESCRIPTION
or
ld
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
PROGRAM
9.3.1
JN
TU
9.3
INPUT
Enter no.of tracks:9
Enter track position:55
OUTPUT
Tracks traversed
55
58
60
70
18
90
58
60
70
18
90
150
32
Downloaded From JNTU World (http://www.alljntuworld.in)
160
184
www.alljntuworld.in
JNTU World
150
160
184
60
10
24
JN
TU
or
ld
9.3.2
INPUT
Enter no.of tracks:9
Enter track position:55
OUTPUT
Tracks traversed
150
58
60
70
18
90
150
33
Downloaded From JNTU World (http://www.alljntuworld.in)
160
184
www.alljntuworld.in
JNTU World
160
184
90
70
60
58
55
18
10
24
94
20
10
2
3
37
JN
TU
or
ld
9.3.3
34
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
}
printf("total header movements%d",sum);
printf("avg is %f",(float)sum/n);
getch();
}
INPUT
Enter the track position : 55
Enter starting position : 100
58
60
70
18
90
150
160
184
OUTPUT
Difference Between tracks
50
10
24
240
37
3
2
10
20
JN
TU
or
ld
Tracks traversed
150
160
184
18
55
58
60
70
90
35
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 10
10.1
OBJECTIVE
Write a C program to simulate page replacement algorithms
a) FIFO
b) LRU
c) LFU
10.2
DESCRIPTION
or
ld
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
a large reference count.
PROGRAM
10.3.1
TU
10.3
JN
36
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
}
OUTPUT
The Page Replacement Process is
7
-1 -1
PF No. 1
7
0
-1
PF No. 2
7
0
1
PF No. 3
2
0
1
PF No. 4
2
0
1
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
0
2
3
0
2
3
0
1
3
PF No. 11
0
1
2
PF No. 12
0
1
2
0
1
2
7
1
2
PF No. 13
7
0
2
PF No. 14
7
0
1
PF No. 15
or
ld
INPUT
Enter the length of reference string 20
Enter the reference string -70120304230321201701
Enter no. of frames -3
JN
TU
10.3.2
37
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
or
ld
min=0;
for(j=1;j<f;j++)
if(count[min] > count[j])
min=j;
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. -- %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
getch();
}
TU
INPUT
Enter the length of 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 the number of frames -- 3
JN
OUTPUT
The Page Replacement process is -7
-1 -1 PF No. -- 1
7
0
-1 PF No. -- 2
7
0
1
PF No. -- 3
2
0
1
PF No. -- 4
2
0
1
2
0
3
PF No. -- 5
2
0
3
4
0
3
PF No. -- 6
4
0
2
PF No. -- 7
4
3
2
PF No. -- 8
0
3
2
PF No. -- 9
0
3
2
0
3
2
1
3
2
PF No. -- 10
1
3
2
1
0
2
PF No. -- 11
1
0
2
1
0
7
PF No. -- 12
1
0
7
38
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
1
0
7
The number of page faults using LRU are 12
10.3.3
or
ld
for(i=0;i<f;i++)
{
cntr[i]=0;
a[i]=-1;
}
Printf(\nThe Page Replacement Process is \n);
for(i=0;i<m;i++)
{
JN
TU
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
min = 0;
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);
}
printf("\n\n Total number of page faults -- %d",pf);
getch();
}
INPUT
Enter number of page references -- 10
Enter the reference string -Enter the available no. of frames -- 3
123452525143
39
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
OUTPUT
The Page Replacement Process is
1
1
1
4
5
5
5
5
5
5
-1
2
2
2
2
2
2
2
2
2
-1
-1
3
3
3
3
3
1
4
3
PF No. 1
PF No. 2
PF No. 3
PF No. 4
PF No. 5
PF No. 6
PF No. 7
PF No. 8
8
JN
TU
or
ld
40
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
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.
Unfortunately, the optimal page-replacement algorithm is difficult to implement, because it requires future
knowledge of the reference string.
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();
printf("Enter maximum limit of the sequence: ");
scanf("%d",&max);
printf("\nEnter the sequence: ");
for(i=0;i<max;i++)
scanf("%d",&seq[i]);
printf("\nEnter no. of frames: ");
scanf("%d",&n);
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)
{
fr[count]=seq[i];
printf("%d\t",fr[count]);
count++;
pf++;
}
i++;
}
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)
JN
TU
or
ld
11.3
41
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
{
TU
or
ld
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++)
{
if(seq[k]==m)
{
pos[j]=k;
break;
}
else
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);
printf("\nPage fault rate %f",pfr);
getch();
JN
}
int findmax(int a[])
{
int max,i,k=0;
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
k=i;
}
}
return k;
}
INPUT
Enter number of page references -- 10
Enter the reference string --
123452525143
42
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
Enter the available no. of frames -- 3
OUTPUT
The Page Replacement Process is
-1
2
2
2
2
2
2
2
2
2
-1
-1
3
3
3
3
3
1
4
3
PF No. 1
PF No. 2
PF No. 3
PF No. 4
PF No. 5
PF No. 6
PF No. 7
PF No. 8
8
JN
TU
or
ld
1
1
1
4
5
5
5
5
5
5
43
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 12
12.1
OBJECTIVE
*Write a C program to simulate producer-consumer problem using semaphores.
12.2
DESCRIPTION
Producer-consumer problem, is a common paradigm for cooperating processes. A producer process produces
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.
or
ld
PROGRAM
#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;
}
} }
JN
TU
12.3
OUTPUT
1. Produce
2. Consume
Enter your choice: 2
Buffer is Empty
1. Produce
2. Consume
Enter your choice: 1
Enter the value: 100
1. Produce
2. Consume
Enter your choice: 2
The consumed value is 100
1. Produce
2. Consume
Enter your choice: 3
3. Exit
3. Exit
3. Exit
3. Exit
44
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 13
13.1
OBJECTIVE
*Write a C program to simulate the concept of Dining-Philosophers problem.
13.2
DESCRIPTION
PROGRAM
JN
TU
13.3
or
ld
45
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
}while(1);
}
}
one()
{
int pos=0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
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]]);
}
or
ld
}
two()
{
TU
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]]);
}
}
}
}
JN
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
OUTPUT
1.One can eat at a time
Enter your choice: 1
3.Exit
46
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
3.Exit
combination 3
P 5 and P 0 are granted to eat
P 3 is waiting
3.Exit
JN
TU
or
ld
combination 2
P 3 and P 0 are granted to eat
P 5 is waiting
47
Downloaded From JNTU World (http://www.alljntuworld.in)
JNTU World
or
ld
www.alljntuworld.in
JN
TU
48
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 1
PRE-LAB QUESTIONS
1.
2.
3.
4.
5.
1.2
POST-LAB QUESTIONS
1.
2.
3.
4.
5.
1.3
ASSIGNMENT QUESTIONS
1.
2.
or
ld
1.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
system processes and user processes to be 5 msec and 2 msec respectively.
Write a C program to simulate pre-emptive SJF CPU scheduling algorithm.
2.1
PRE-LAB QUESTIONS
1.
2.
3.
2.2
EXPERIMENT 2
POST-LAB QUESTIONS
What are the parameters to be considered for designing a multilevel feedback queue scheduler?
Differentiate multi-level queue and multi-level feedback queue CPU scheduling algorithms?
What are the advantages of multi-level queue and multi-level feedback queue CPU scheduling algorithms?
TU
1.
2.
3.
2.3
ASSIGNMENT QUESTIONS
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.
JN
1.
3.1
PRE-LAB QUESTIONS
1.
2.
3.
3.2
EXPERIMENT 3
Define file?
What are the different kinds of files?
What is the purpose of file allocation strategies?
POST-LAB QUESTIONS
1.
2.
3.
4.
Identify ideal scenarios where sequential, indexed and linked file allocation strategies are most appropriate?
What are the disadvantages of sequential file allocation strategy?
What is an index block?
What is the file allocation strategy used in UNIX?
49
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
3.3
JNTU World
ASSIGNMENT QUESTIONS
1.
EXPERIMENT 4
PRE-LAB QUESTIONS
1.
2.
3.
4.
5.
4.2
POST-LAB QUESTIONS
1.
2.
4.3
ASSIGNMENT QUESTIONS
1.
or
ld
4.1
Write a C program to simulate MFT memory management scheme with unequal sized partitions.
EXPERIMENT 5
PRE-LAB QUESTIONS
1.
2.
3.
5.2
POST-LAB QUESTIONS
1.
2.
3.
4.
5.
Which of the dynamic contiguous memory allocation strategies suffer with external fragmentation?
What are the possible solutions for the problem of external fragmentation?
What is 50-percent rule?
What is compaction?
Which of the memory allocation techniques first-fit, best-fit, worst-fit is efficient? Why?
ASSIGNMENT QUESTIONS
TU
5.3
5.1
1.
6.1
EXPERIMENT 6
PRE-LAB QUESTIONS
1.
2.
JN
3.
6.2
POST-LAB QUESTIONS
1.
2.
3.
4.
6.3
ASSIGNMENT QUESTIONS
1.
2.
50
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 7
PRE-LAB QUESTIONS
1.
2.
3.
7.2
POST-LAB QUESTIONS
1.
2.
3.
7.3
Define directory?
Describe the general directory structure?
List the different types of directory structures?
ASSI7GNMENT QUESTIONS
1.
2.
or
ld
7.1
EXPERIMENT 8
PRE-LAB QUESTIONS
1.
2.
3.
8.2
POST-LAB QUESTIONS
1.
2.
3.
8.3
How can be the resource allocation graph used to identify a deadlock situation?
How is Bankers algorithm useful over resource allocation graph technique?
Differentiate between deadlock avoidance and deadlock prevention?
8.1
ASSIGNMENT QUESTIONS
Write a C program to implement deadlock detection technique for the following scenarios?
a. Single instance of each resource type
b. Multiple instances of each resource type
TU
1.
9.1
PRE-LAB QUESTIONS
1.
2.
3.
9.2
POST-LAB QUESTIONS
JN
1.
2.
9.3
ASSIGNMENT QUESTIONS
1.
10.1
EXPERIMENT 10
PRE-LAB QUESTIONS
1.
2.
3.
4.
5.
10.2
EXPERIMENT 9
POST-LAB QUESTIONS
51
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
1.
2.
10.3
Which page replacement algorithm suffers with the problem of Beladys anomaly?
Define the concept of thrashing? What is the scenario that leads to the situation of thrashing?
ASSIGNMENT QUESTIONS
1.
EXPERIMENT 11
PRE-LAB QUESTIONS
1.
11.2
What are the benefits of optimal page replacement algorithm over other page replacement algorithms?
POST-LAB QUESTIONS
1.
or
ld
11.1
EXPERIMENT 12
PRE-LAB QUESTIONS
1.
2.
3.
12.2
POST-LAB QUESTIONS
1.
2.
12.3
Discuss the consequences of considering bounded and unbounded buffers in producer-consumer problem?
Can producer and consumer processes access the shared memory concurrently? If not which technique
provides such a benefit?
12.1
ASSIGNMENT QUESTIONS
1.
EXPERIMENT 13
13.1
PRE-LAB QUESTIONS
Differentiate between a monitor, semaphore and a binary semaphore?
Define clearly the dining-philosophers problem?
TU
1.
2.
13.2
POST-LAB QUESTIONS
1.
ASSIGNMENT QUESTIONS
1.
JN
13.3
Identify the scenarios in the dining-philosophers problem that leads to the deadlock situations?
52
Downloaded From JNTU World (http://www.alljntuworld.in)