Operating System Lab Manual
Operating System Lab Manual
Series
Lab Manual
Operating
System Concepts
A simplified practical work book of Operating System Concepts course for
Computer Science, Information Technology and Software Engineering
students
Shahid Abid
2 Lab Manual: Operating Systems Concepts
PREFACE
This lab manual has been prepared to facilitate the students of computer science in studying,
analyzing and simulating various functions of operating system concepts. The students will have to
work with CLI and GUI of various OS, study how to monitor OS, create process and simulate
several algorithms using high level/visual programming language. The lab sessions are designed to
improve the abilities of the students by giving hands on experience. After completing the laboratory
exercises, the students will be familiar with the practical aspects of the various concepts explained in
the course, as well as with a simple OS development.
PREPARED BY
Lab manual is prepared by Mr. Shahid Abid under the supervision of the Director BIIT, Dr. Jamil
Sawar.
GENERAL INSTRUCTIONS
a. Students are required to maintain the lab manual with them till the end of the semester.
b. All readings, answers to questions and illustrations must be solved on the place provided. If more
space is required then additional sheets may be attached. You may add screen shots to the report
by using the ‘Print Screen’ command on your keyboard to get a snapshot of the output.
c. It is the responsibility of the student to have the lab manual graded as soon as he/she has
completed a task.
d. Loss of manual will result in resubmission of the complete manual.
e. Students are required to go through the experiment before attending a lab session.
f. Students must bring the manual during lab session.
g. Keep the manual neat, clean and presentable.
h. Plagiarism is strictly forbidden. No credit will be given if a lab session is plagiarised and no
resubmission will be entertained.
i. Marks will be deducted for late submission.
j. You need to submit the report even if you have demonstrated the exercises to the lab instructor or
shown them the lab report during the lab session.
VERSION HISTORY
Date Updated by Details
September Mr. Shahid Abid Version 1.0. Initial draft prepared and experiments
2019 outlined.
2
3 Lab Manual: Operating Systems Concepts
MARKS
Max. Marks
Lab# Lab Title Sign
Marks Obtained
3
4 Lab Manual: Operating Systems Concepts
Objectives
• Introduction to CLI commands and GUI commands/elements.
• Write a simple C++ program, compile and execute the program.
Time Required : 1.5 hrs
Programming Language : C++
Software Required : Turbo C++ in DOSBox, VC++
Hardware Required :
Computer with administrative rights
A brief introduction to MS Windows command line interpreter (CLI) and graphical user
interface (GUI) will be shown to students.
DOS Files:
There are three types of DOS files i.e. BAT, COM or EXE. These files contains DOS
commands/programs.
DOS Commands:
There are two types of DOS commands
1. Internal commands
2. External commands
2. DELETE Command:
Purpose: Delete or erase one or more specified files from the disk.
Syntax: DEL <Filename.ext>
DEL [drive][path] filename [/p]
• If you want to delete all backup files from current drive then
DEL *.bak
• If you want to delete all the files form current location then
DEL *.*
3. DIR Command:
Purpose: Display a list of current directory files and subdirectories.
Syntax:
DIR [drive:] [path] [filename] [/p] [/w]
MS Windows - GUI
Processes
5
6 Lab Manual: Operating Systems Concepts
Performance
It contains a lot of information about CPU, Memory, Disk, Network, Virtual Memory, Cache,
CPU cores, Threads, Handles etc.
CPU
It contains information about CPU speed, Cache, CPU cores, Threads, Handles etc.
6
7 Lab Manual: Operating Systems Concepts
7
8 Lab Manual: Operating Systems Concepts
Objectives
Introduction to CLI commands and GUI commands/elements.
Write, compile, and execute a simple C++ program in Linux environment using gcc.
Time Required : 1.5 hrs
Programming Language : C++
Software Required : Linux/Ubuntu in VM, gcc compiler
Hardware Required :
Computer with administrative rights
Structure of the lab
A brief introduction to Unix/Linux command line interpreter (CLI) and graphical user
interface (GUI) will be given to students.
Introduction
Fedora is available to you as virtual machine. Fedora is an open-source
Linux-based operating system alternative to Windows. We will learn the
basic desktop environment and common shell commands. Please login
using the credentials provided by the instructor.
Play with the fedora desktop environment.
Linux Basics
Linux System
Linux System can be split into two parts:
CLI (Shell)
GUI (X-Windows Server with Gnome, KDE etc. window manager)
We will work in Kernel / User modes using root / test users.
Formally, a Shell is interface between a user and a Linux operating system, i.e.
user interacts with the Linux operating system through the shell.
Kernel is the core of Linux Operating System, which is operational as long as the
computer system is running. The kernel is part of the Linux Operating system which
consists of routines which interact with underlying hardware, and routines which
include system calls handling, process management, scheduling, signals, the file
system, and I/O to storage devices.
root@ubuntu:/home/sha# cd /
root@ubuntu:/# ls
8
9 Lab Manual: Operating Systems Concepts
Basic Commands
In this section, we learn some basic Linux commands e.g., ls, cd, mkdir etc.
Directory Commands
Command Description
ls List the file in the directory, just like dir command in DOS.
Options
Command Description
mkdir directory-name Creates a new directory.
Directory-name specifies the name of the new
directory. If the name doesn’t begin with a slash,
the new directory is created as a subdirectory of
the current working directory. If the name begins
with a slash, the name defines the path from the
9
10 Lab Manual: Operating Systems Concepts
$cd /
Try to use the following command first because this will
bring you back to your home directory
$ cd
mkdir books
This command will create a new directory under the home directory.
For Example:
$cd dir-name
To change to any sub-directory under the current directory.
$cd books
(When Enter is pressed, the prompt becomes)
/books$
Do you see any difference between the two prompts?
Now you are in books directory, a step down to home. How could you go up?
$ cd .. (there’s space between cd and ..) Now you will again be in your parent directory. And
the prompt becomes: $
In-Lab Exercises
1. Suppose you need to list the contents of a directory in long listing format. What
parameters should you use with ls to perform this task? Hint: remember that
the man command gives you an online reference manual for any Linux
command.
3. Write a program that odd numbers from 1 to 10 and print their sum, Compile and run
it using gcc.
4. Learn the usage of find command. Hint: remember that the man command
gives you an on line reference manual for any Linux command and also you
can get help from the Internet.
Important note: Don't copy paste exact information .write in your own word
after understanding.
Important: The lab instructor will grade you at the end of the lab.
11
12 Lab Manual: Operating Systems Concepts
System Monitoring
In this section, we learn about basic tools (CLI and GUI) to monitor system resources.
1. To identify the available CPU, memory, and disk resources, we can used the following
commands:
• cat /proc/cpuinfo (read the CPU information)
• cat /proc/meminfo (read the memory (RAM) information)
• df -h ( find out secondary storage (hard-disk) information)
Make sure you are able to identify the total RAM size, disk space, and CPU resource
available.
2. top is a command line program provides a real-time view of the processes running in
the system. It provides system summary and also the list of tasks managing by Linux
kernel. The program is useful to identify the processes running with CPU and memory
utilization. Lets play with the top program: provide
• Launch a terminal and execute top command.
• You can press q to exit from top program.
12
13 Lab Manual: Operating Systems Concepts
/*
The code is taken from http://www.daniweb.com/software-
development/c/code/216411/reading-a-file-line-by-line */
#include <stdio.h>
int main ( void )
{
static const char filename[] = "file.txt"; FILE
*file = fopen ( filename, "r" );
if ( file != NULL )
{
char line [ 128 ]; /* or other suitable maximum line size */
13
14 Lab Manual: Operating Systems Concepts
In-Lab Exercises
• Create a simple shell script to print "Hello Word!" and a C program to print "Hello Word!".
You need to profile the execution time and system calls used by both programs. Hint! time
command may help to identify the program execution time.
• Write a simple shell script to backup a file. Your program should be able to remove the
original file and print appropriate messages.
• Write a report in your own words after reading the article about interpreting the ‘top’
command output. Write briefly.
14
15 Lab Manual: Operating Systems Concepts
A brief introduction to Unix/Linux command line interpreter (CLI) and graphical user
interface (GUI) will be given to students.
Process Creation
A process is a program in execution. The process which creates another process is called
parent process. The process which is created is called child process. We can identify
process by their unique key called process identifier or pid (integer number). In Linux we
can use fork() system call to create processes. By this system call new process is created
containing copy of parent process. Both process (parent and child) continue executing
instructions after fork(). The return number of fork() for new (child) process will be 0 (zero),
whereas for parent process value will be nonzero positive process identifier. If fork() fails, it
return a negative number. In this section, we create a simple program using fork() to create
child process using C language.
Create a new file named fork example.c and type in the following code:
#include<stdio.h>
#include<unistd.h>
void main()
{
pid_t pid;
pid = fork(); /* fork a child process */
if( pid <0 ) /* error occurred */
{
printf("Fork Failed");
}
Lab Task:
16
17 Lab Manual: Operating Systems Concepts
A brief introduction to Unix/Linux command line interpreter (CLI) and graphical user
interface (GUI) will be given to students.
The fork system call creates a new process. The execve system call overwrites a process
with a new program. Like peanut butter and chocolate in the ever-delicious Reese's Peanut
Butter Cups, these two system calls go together to spawn a new running program. A
process forks itself and the child process execs a new program, which overlays the one in
the current process.
Because the child process is initially running the same code that the parent was and has
the same open files and the same data in memory, this scheme provides a way for the child
to tweak the environment, if necessary, before calling the execve system call. A common
thing to do is for the child to change its standard input or standard output (file descriptors 0
and 1, respectively) and then exec the new program. That program will then read from and
write to the new sources.
Example
This is a program that forks itself and has the child exec the ls program, running the "ls -aF
/" command. Five seconds later, the parent prints a message saying, I'm still here!. We use
the sleep library function to put the process to sleep for five seconds.
/* forkexec: create a new process. */ /* The child runs "ls -aF /". The parent wakes up after 5
seconds */ /* Paul Krzyzanowski */ #include <stdlib.h> /* needed to define exit() */ #include
<unistd.h> /* needed for fork() and getpid() */ #include <stdio.h> /* needed for printf() */ int
main(int argc, char **argv) { void runit(void); int pid; /* process ID */ switch (pid = fork()) {
case 0: /* a fork returns 0 to the child */ runit(); break; default: /* a fork returns a pid to the
parent */ sleep(5); /* sleep for 5 seconds */ printf("I'm still here!\n"); break; case -1: /*
something went wrong */ perror("fork"); exit(1); } exit(0); } void runit(void) { printf("About to
run ls\n"); execlp("ls", "ls", "-aF", "/", (char*)0); perror("execlp"); /* if we get here, execlp
failed */ exit(1); }
int
main(int argc, char **argv) {
void runit(void);
int pid; /* process ID */
void
runit(void) {
printf("About to run ls\n");
execlp("ls", "ls", "-aF", "/", (char*)0);
perror("execlp"); /* if we get here, execlp failed */
exit(1);
}
./forkexec
18
19 Lab Manual: Operating Systems Concepts
ALGORITHM:
1. Start.
2. Declare the array size.
3. Read the number of processes to be inserted.
4. Read the Burst times of processes,
5. Sort the array according to the arrival time of process in ascending
order.
6. Calculate the waiting time of each process,
Waiting time [i+1] = Burst time[i] + Waiting time [i]
7. Calculate the turnaround time of each process,
Turnaround time [i+1] = Turnaround time [i] + Burst time [i+1]
8. Calculate the average waiting time and average turnaround time.
9. Display the values
10. Stop.
PROGRAM:
#include<iostream>
using namespace std;
int main()
{
` char pn[10][10];
int arr[10], bur[10], star[10], finish[10], tat[10], wt[10], i, n;
int totwt=0, tottat=0;
19
20 Lab Manual: Operating Systems Concepts
}
else
{
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
}
cout<<"\nPNameArrtimeBurtime Start TAT Finish";
for(i=0;i<n;i++)
{
cout<<pn[i]<<arr[i]<<bur[i]<<star[i]<<tat[i]<<finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
cout"\nAverage Waiting time”<<totwt/n;
cout"\nAverage Turn Around Time:"<<tottat/n);
system(“pause”);
}
20
21 Lab Manual: Operating Systems Concepts
Algorithm:
1. Start.
2. Declare the array size.
3. Read the number of processes to be inserted .
4. Read the Burst times of processes.
5. Sort the Burst times in ascending order and process with shortest burst
time is first executed. And also sort the process according to arrival time
of that process.
6. Calculate the waiting time of each process,
Wt[i+1]= bt[i]+wt[i]
7. Calculate the turnaround time of each process,
tt[i+1]=tt[i]+bt[i+1]\
8. Calculate the average waiting time and average turnaround time.
9. Display the values.
10. Stop.
PROGRAM:-
#include<iostream>
Int main()
{
Int I, j, burstt[10], arrivalt[10], t, n, wt[10], tt[10], w1=0, t1=0;
Float awt, atat;
for(i=0;i<n;i++)
{
Cin>>burst[i];
Cin>>arrivalt[i];
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
if(arrivalt[i]>arrivalt[j])
{
if(burstt[i]>burstt[j])
{
21
22 Lab Manual: Operating Systems Concepts
t=burstt[i];
burstt[i]=burstt[j];
burstt[j]=t;
t=arrivalt[i];
arrivalt[i]=arrivalt[j];
arrivalt[j]=t;
}
}
}
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=burstt[0];
wt[i+1]=burstt[i]+wt[i];
tt[i+1]=tt[i]+burstt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
cout<<"\nbursttime\t waiting time\t turnaround time\n";
for(i=0;i<n;i++)
{
cout<<burstt[i]<<”\t”<<wt[i]<<”\t”<<tt[i]);
}
system(“pause”);
}
22
23 Lab Manual: Operating Systems Concepts
PROGRAM:-
#include<iostream.h>
#include<stdio.h>
int main()
{
int n=3;
float total, wait[3]={0};
float p[3], twaiting=0, waiting=0;
int proc;
int stack[3];
float brust[3], arrival[3], sbrust,temp[3], top=3;
for(int i=0;i<n;i++)
{
p[i]=i;
stack[i]=i;
cout<<"enter arival time :";
cin>>arrival[i];
cout<<endl<<"enter brust time:";
cin>>brust[i];
temp[i]=arrival[i];
sbrust=brust[i]+sbrust;
}
for(i=0;i<sbrust;i++)
23
24 Lab Manual: Operating Systems Concepts
{
proc=stack[0];
if(temp[proc]==i)
twaiting=0;
else
twaiting=i-(temp[proc]);
temp[proc]=i+1;
wait[proc]=wait[proc]+twaiting;
waiting=waiting+(twaiting);
brust[proc]=brust[proc]-1;
if(brust[proc]==0)
{
for(int x=0;x<top-1;x++)
stack[x]=stack[x+1];
top=top-1;
}
for(int z=0;z<top-1;z++)
{
if((brust[stack[0]]>brust[stack[z+1]]) && (arrival[stack[z+1]] <= i+1))
{
int t=stack[0];
stack[0]=stack[z+1];
stack[z+1]=t;
}
}
}
cout<<"waiting:"<<waiting;
return 0;
}
24
25 Lab Manual: Operating Systems Concepts
Objective:-
Write a c++ program to implements the round robin scheduling algorithm?
Algorithm:
1. Start
2. Declare the array size
3. Read the number of processes to be inserted
4. Read the burst times of the processes
5. Read the Time Quantum
6. If the burst time of a process is greater than time Quantum then subtract time
quantum form the burst time, Else assign the burst time to time quantum.
7. Calculate the average waiting time and turn around time of the processes.
8. Display the values
9. Stop
PROGRAM:-
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
int main()
{
int n=3;
float total, wait[3]={0};
float p[3], twaiting=0, waiting=0;
int proc;
int stack[3];
float brust[3], arrival[3], sbrust, temp[3], top=3;
for(int j=0;j<n;j++)
{
p[j]=j;
stack[j]=j;
cout<<"enter arival time :";
cin>>arrival[j];
cout<<endl<<"enter brust time:";
25
26 Lab Manual: Operating Systems Concepts
cin>>brust[j];
temp[j]=arrival[j];
sbrust=brust[j]+sbrust;
}
int cont;
cout<<"enter contum time:";
cin>>cont;
int i=0;
while(1)
{
for(int m=0;m<cont;m++)
{
if(i==sbrust)
{
break;
}
proc=stack[0];
cout<<endl<<proc;
if(temp[proc]==i)
twaiting=0;
else
{
twaiting=i-(temp[proc]);
}
temp[proc]=i+1;
wait[proc]=wait[proc]+twaiting;
waiting=waiting+(twaiting);
brust[proc]=brust[proc]-1;
if(brust[proc]==0)
{
for(int x=0;x<top-1;x++)
{
stack[x]=stack[x+1];
}
top=top-1;
m=-1;
}
i=i+1;
}
if(i==sbrust)
{
break;
}
int tp=stack[0];
for(int x=0;x<top-1;x++)
{
stack[x]=stack[x+1];
26
27 Lab Manual: Operating Systems Concepts
}
stack[top-1]=tp;
m=-1;
}
cout<<"waiting:"<<waiting;
return 0;
}
27
28 Lab Manual: Operating Systems Concepts
temp[i]=arrival[i];
28
29 Lab Manual: Operating Systems Concepts
sbrust=brust[i]+sbrust;
}
for(i=0;i<sbrust;i++)
{ //section 1
proc=stack[0];
if(temp[proc]==i)
{
twaiting=0;
}
else
{
twaiting=i-(temp[proc]);
}
temp[proc]=i+1;
wait[proc]=wait[proc]+twaiting;
waiting=waiting+(twaiting);
brust[proc]=brust[proc]-1;
if(brust[proc]==0)
{
for(int x=0;x<top-1;x++)
{
stack[x]=stack[x+1];
}
top=top-1;
for(int z=0;z<top-1;z++)
{
if((prority[stack[0]]>prority[stack[z+1]]) && (arrival[stack[z+1]] <= i+1))
{
int t=stack[0];
stack[0]=stack[z+1];
stack[z+1]=t;
}
}
}
}
29
30 Lab Manual: Operating Systems Concepts
30
31 Lab Manual: Operating Systems Concepts
sbrust=brust[i]+sbrust;
}
for(i=0;i<sbrust;i++)
{ //section 1
proc=stack[0];
if(temp[proc]==i)
{
twaiting=0;
}
else
{
twaiting=i-(temp[proc]);
}
temp[proc]=i+1;
wait[proc]=wait[proc]+twaiting;
waiting=waiting+(twaiting);
brust[proc]=brust[proc]-1;
if(brust[proc]==0)
{
for(int x=0;x<top-1;x++)
{
stack[x]=stack[x+1];
}
top=top-1;
}
for(int z=0;z<top-1;z++)
{
if((prority[stack[0]]>prority[stack[z+1]]) && (arrival[stack[z+1]] <= i+1))
{
int t=stack[0];
stack[0]=stack[z+1];
stack[z+1]=t;
}
}
}
cout<<"Average waiting time is:"<<waiting/n;
float tu=(sbrust+waiting)/n;
cout<<endl<<"Average turnaround time is:"<<tu;
return 0;
}
31
32 Lab Manual: Operating Systems Concepts
Objective:
Write a program to implement Dynamic allocation of memories in MVT.
Algorithm:
Step 1: Start the process.
Step 2: Declare variables.
Step 3: Enter total memory size.
Step 4: Allocate memory for OS.
Step 5: Allocate total memory to the pages.
Step 6: Display the wastage of memory.
Step 7: Stop the process.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int m, i, p[15];
char ch;
clrscr();
printf("Enter memory to be allocated: ");
scanf("%d",&m);
printf("Enter process size : ");
scanf("%d", &p[0]);
i=0;
do
{
m=m-p[i];
printf("\nRemaining memory is %d\n",m);
abc:printf("Do you want to continue: ");
fflush(stdin);
scanf("%c",&ch);
i++;
if(ch=='y')
{
printf("Enter the process size: ");
32
33 Lab Manual: Operating Systems Concepts
scanf("%d",&p[i]);
}
else
{
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();
}
33
34 Lab Manual: Operating Systems Concepts
Objective:
Write a program to implement Dynamic allocation of memories in MVT.
Algorithm:
Step 1: Start the process.
Step 2: Declare variables.
Step 3: Enter total memory size.
Step 4: Allocate memory for operating system.
Step 5: Allocate total memory to the pages.
Step 6: Display the wastage of memory.
Step 7: Stop the process.
PROGRAM:
34
35 Lab Manual: Operating Systems Concepts
35
36 Lab Manual: Operating Systems Concepts
Objective:
Write a program to implement Bankers Algorithm for Deadlock Avoidance.
Algorithm:
1. Start the program.
2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether its possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety.
9. Or not if we allow the request.
10. Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
Int alloc[10][10], max[10][10];
int avail[10], work[10], total[10];
int i, j, k, n, need[10][10];
int m;
int count=0,c=0;
char finish[10];
clrscr();
printf("Enter the no. of processes and resources:");
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
finish[i]='n';
printf("Enter the claim matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);
printf("Enter the allocation matrix:\n");
36
37 Lab Manual: Operating Systems Concepts
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&alloc[i][j]);
printf("Resource vector:");
for(i=0;i<m;i++)
scanf("%d",&total[i]);
for(i=0;i<m;i++)
avail[i]=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
avail[j]+=alloc[i][j];
for(i=0;i<m;i++)
work[i]=avail[i];
for(j=0;j<m;j++)
work[j]=total[j]-work[j];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j]=max[i][j]-alloc[i][j];
A:for(i=0;i<n;i++)
{
c=0;
for(j=0;j<m;j++)
if((need[i][j]<=work[j])&&(finish[i]=='n'))
c++;
if(c==m)
{
printf("All the resources can be allocated to Process %d",i+1);
printf("\n\nAvailable resources are:");
for(k=0;k<m;k++)
{
work[k]+=alloc[i][k];
printf("%4d",work[k]);
}
printf("\n");
finish[i]='y';
printf("\nProcess %d executed?:%c \n",i+1,finish[i]);
count++;
}
}
if(count!=n)
goto A;
else
printf("\n System is in safe mode");
printf("\n The given state is safe state");
getch();
}
Objective:
Write a program to implement Bankers Algorithm for Deadlock Prevention.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char job[10][10];
int time[10], avail,tem[10], temp[10];
int safe[10];
int ind=i,j,q,n,t;
clrscr();
printf("Enter no of jobs: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter name and time: ");
scanf("%s%d",&job[i],&time[i]);
}
for(i=0;i<n;i++)
{
temp[i]=time[i];
tem[i]=i;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(temp[i]>temp[j])
{
t=temp[i];
temp[i]=temp[j];
temp[j]=t;
t=tem[i];
tem[i]=tem[j];
tem[j]=t;
}
}
38
39 Lab Manual: Operating Systems Concepts
for(i=0;i<n;i++)
{
q=tem[i];
if(time[q]<=avail)
{
safe[ind]=tem[i];
avail=avail-tem[q];
//printf("%s",job[safe[ind]]);
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();
}
39
40 Lab Manual: Operating Systems Concepts
Objective:
Write a program to implement FIFO (First In First Out) page replacement algorithm.
Algorithm:
Step 1: Create a queue to hold all pages in memory.
Step 2: When the page is required replace the page at the head of the queue.
Step 3: Now the new page is inserted at the tail of the queue.
PROGRAM:
#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)
40
41 Lab Manual: Operating Systems Concepts
{
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");
}
41
42 Lab Manual: Operating Systems Concepts
Objective:
Write a program to implement page replacement algorithm LRU.
Algorithm:
Step 1: Create a queue to hold all pages in memory.
Step 2: When the page is required replace the page at the head of the queue.
Step 3: Now the new page is inserted at the tail of the queue.
Step 4: Create a stack.
Step 5: When the page fault occurs replace page present at the bottom of the
stack.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void display();
void main()
{
int i, j, page[20], fs[10], n, m;
int index, k, l, flag1=0, flag2=0, pf=0;
int fr[10];
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;
42
43 Lab Manual: Operating Systems Concepts
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++)
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();
}
43
44 Lab Manual: Operating Systems Concepts
Objective:
Write a program to implement page replacement algorithms Optimal.
Algorithm:
Here we select the page that will not be used for the longest period of time.
Step 1: Create an array.
Step 2: When the page fault occurs replace page that will not be used for the longest
period of time.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fr[3], n, m;
void display();
void main()
{
int i, j, page[20], fs[10];
int max, found=0, lg[3], index, k, l, flag1=0, flag2=0, pf=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;
pf=m;
for(j=0;j<n;j++)
{
flag1=0;
flag2=0;
for(i=0;i<m;i++)
{
44
45 Lab Manual: Operating Systems Concepts
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[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++)
45
46 Lab Manual: Operating Systems Concepts
{
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");
}
46
47 Lab Manual: Operating Systems Concepts
Objective:
Write a program to implement the memory management policy-Paging.
Algorithm:
Step 1: Read all the necessary input from the keyboard.
Step 1: Pages- Logical memory is broken into fixed-sized blocks.
Step 3: Frames- Physical memory is broken into fixed-sized blocks.
Step 4: Calculate the physical address using the following;
Physical Address = (Frame Number * Frame size) + offset
Step 5: Display the physical address.
Step 6: Stop the process.
PROGRAM:
47