OS_LAB MANUAL_C216_KCS451 (2) (2)
OS_LAB MANUAL_C216_KCS451 (2) (2)
LABORATORY MANUAL
4. To Execute UNIX system calls for Input Output System Calls. C216.1
15. Calculation of external and internal fragmentation, free space list of C216.1
blocks from system, List process file from the system.
16. Implementation of Compaction for the continually changing C216.1
memory layout and calculate
17. Implementation of resource allocation graph (RAG). C216.1
20. Implement the solutions for Readers-Writers problem using inter- C216.3
process communication technique -Semaphore.
21. Simulate FIFO page replacement algorithms. C216.5
INTRODUCTION
An operating system acts as an intermediary between the user of a computer and computer
hardware. The purpose of an operating system is to provide an environment in which a user can
execute programs in a convenient and efficient manner.
An operating system is software that manages the computer hardware. The hardware must
provide appropriate mechanisms to ensure the correct operation of the computer system and to
prevent user programs from interfering with the proper operation of the system.
An operating system is a program that controls the execution of application programs and
acts as an interface between the user of a computer and the computer hardware.
Every computer must have an operating system to run other programs. The operating system
coordinates the use of the hardware among the various system programs and application
programs for a various users. It simply provides an environment within which other programs can
do useful work.
The operating system is a set of special programs that run on a computer system that allows it to
work properly. It performs basic tasks such as recognizing input from the keyboard, keeping
track of files and directories on the disk, sending output to the display screen and controlling
peripheral devices.
OS is designed to serve two basic purposes:
1. It controls the allocation and use of the computing System’s resources among the various
user and tasks.
2. It provides an interface between the computer hardware and the programmer that
simplifies and makes feasible for coding, creation, debugging of application programs.
The Operating system must support the following tasks. The task are:
1. Provides the facilities to create, modification of programs and data files using an editor.
2. Access to the compiler for translating the user program from high level language to
machine language.
3. Provide a loader program to move the compiled program code to the computer’s memory
for execution.
4. Provide routines that handle the details of I/O programming.
Name
Roll No.
Section- Batch
Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Information Technology
INDEX
Experiment Experiment Date of Date of Faculty
No. Name Conduction Submission Signature
Students are provided with the details of the experiment (Aim, pre-experimental questions,
procedure etc.) to be conducted in next lab and are expected to come prepared for each lab
class.
Faculty ensures that students have completed the required pre-experiment questions and they
complete the in-lab programming assignment(s) before the end of class. Given that the lab
programs are meant to be formative in nature, students can ask faculty for help before and
during the lab class.
Students’ performance will be assessed in each lab based on the following Lab Assessment
Components:
Assessment Criteria-1: Performance (Max. marks = 5)
Assessment Criteria-2: VIVA (Max. marks = 5)
Assessment Criteria-3: Record (Max. marks = 5)
In each lab class, students will be awarded marks out of 5 under each component head,
making it total out of 15 mark
EXPERIMENT - 1
Aim: Study of hardware and software requirements of different operating systems (UNIX,
LINUX, WINDOWS XP, WINDOWS 7/8
Description: The OBJECTIVE of this practical is to obtain general overview of various popular OS
Unix was first developed in 1969 at Bell Labs (Thompson & Ritchie) by Ritchie & Thompson.
They identified the (new) features of Unix: A hierarchical file system incorporating demountable
volumes.
Compatible file, device and inter-process IO (naming schemes, access control)
Ability to initiate asynchronous processes (i.e., address-spaces = heavyweight)System command
language selectable on a per-user basis
Completely novel at the time: prior to this, everything was "inside" the OS. In Unix separation
between essential things (kernel) and everything else
Among other things: allows user wider choice without increasing size of core OS allows easy
replacement of functionality — resulted in over 100 subsystems including a dozen languages
Highly portable due to use of high-level language Features which were not included: real time,
multiprocessor support
Linux systems include user interfaces and applications in addition to the kernel
• Borrows from the UNIX layered system approach
• System contains kernel threads to perform services– Implemented as daemons, which sleep until
awakened by a kernel component
• Multiuser system– Restricts access to important operations to users with superuser (also called
root) privileges
User Interface
Can be accessed via the command-line via shells such as bash, csh and esh
• Most Linux GUIs are layered– X Window System
• Lowest level
• Provides to higher GUI layers mechanisms to create and manipulate graphical components–
Window manager
• Builds on mechanisms in the X Window System interface to control the placement, appearance,
size and other window attributes– Desktop environment (e.g., KDE, GNOME)
• Provide user applications and service
Linux increasingly conforms to popular standards such as POSIX
• The Single UNIX Specification (SUS)– Suite of standards that define user and application
programming interfaces for UNIX operating systems, shells and utilities– Version 3 of the SUS
combines several standards (including POSIX, ISO standards and previous versions of the SUS)
• Linux Standards Base (LSB)– Project that aims to standardize Linux so that applications written
for one LSB-compliant distribution will compile and behave exactly the same on any other LSB-
compliant distribution
Hardware Platforms
• Supports a large number of platforms, including – x86 (including Intel IA-32), HP/Compaq Alpha
AXP, Sun SPARC, Sun UltraSPARC, Motorola 68000, PowerPC, PowerPC64, ARM, Hitachi
SuperH, IBM S/390 and zSeries, MIPS, HP PA-RISC, Intel IA-64,AMD x86-64,H8/300,V850 and
CRIS
• Architecture-specific code– Performs operations implemented differently across platforms
• Porting– Modifying the kernel to support a new platform
• Source tree– Loosely organizes kernel into separate components by directory
• User-mode Linux (UML)– Important tool for kernel development
Loadable Kernel Modules
• Loadable kernel modules– Contains object code that, when loaded, is dynamically linked to a
running kernel– Enables code to be loaded on demand
• Reduces the kernel’s memory footprint– Modules written for versions of the kernel other than the
current one may not work properly–Kmod: a kernel subsystem that manages modules without user
intervention
COMMAND :
1. Date Command :
This command is used to display the current data and time.
Syntax: $date
2.Calender Command :
This command is used to display the calendar of the year or the particular month of calendar year.
Syntax :
a. $cal<year>
b. $cal<month><year>
Here the first syntax gives the entire calendar for given year & the second Syntax gives the
calendar of reserved month of that year.
3. Echo Command :
This command is used to print the arguments on the screen .
3. SMSO – background become white and foreground become black color. 4.rmso – background
become black and foreground becomes white color. 5.Cols – Display the number of columns in
our terminals.
10.LIST Command :
It is used to list all the contents in the current working directory.
Syntax :$ls–options <arguments>
If the command does not contain any argument means it is working in the Current directory.
Options :
a– used to list all the files including the hidden files. c– list all the files column wise.
d- list all the directories.
m- list the files separated by commas.
p- list files include „/‟ to all the directories. r- list the files in reverse alphabetical order.
f- list the files based on the list modification date. x-list in column wise sorted order.
DIRECTORY RELATED COMMANDS :
1. Present Working Directory Command :
To print the complete path of the current working directory.
Syntax : $pwd
2. MKDIR Command :
To create or make a new directory in a current directory .
Syntax :$mkdir<directory name>
3. CD Command :
To change or move the directory to mentioned directory .
Syntax :$cd <directory name>
4. RMDIR Command :
To remove a directory in the current directory & not the current directory itself
Syntax :$rmdir<directory name>
FILE RELATED COMMANDS : 1.CREATE A FILE :
To create a new file in the current directory we use CAT command.
Syntax :
$cat > filename.
The > symbol is redirectory we use cat command.
2. DISPLAY A FILE :
To display the content of file mentioned we use CAT command without „>‟ operator.
Syntax :
$cat <filename.
Options –s = to neglect the warning /error message.
3. COPYING CONTENTS :
To copy the content of one file with another. If file does not exist, a new file is created
and if the file exists with some data then it is overwritten.
Syntax :
$ cat<filename source>>><destination filename>
Options : -
-n content of file with numbers included with blank lines.
Syntax :
$cat –n <filename>
4. SORTING A FILE :
To sort the contents in alphabetical order or in reverse order.
Syntax :
$sort <filename >
6. MOVE Command :
To completely move the contents from source file to destination file and to remove the source
file.
Syntax :
$ mv<source filename><destination filename>
7. REMOVE Command :
To permanently remove the file we use this command .
Syntax :
$rm<filename>
8. WORD Command :
To list the content count of no of lines , words, characters .
Syntax :
$wc<filename>
Options :
-c – to display no of characters.-l – to display only the lines.
-w – to display the no of words.
9. LINE PRINTER :
To print the line through the printer, we use lp command.
Syntax :
$lp<filename>
10. PAGE Command :
This command is used to display the contents of the file page wise & next page can be
viewed by pressing the enter key.
Syntax :
$pg<filename>
11. FILTERS AND PIPES
HEAD :It is used to display the top ten lines of file. Syntax: $head<filename>
TAIL :This command is used to display the last ten lines of file.
Syntax: $tail<filename>
PAGE : This command shows the page by page a screen full of information is displayed after
which the page command displays a prompt and passes for the user to strike the enter key to
continue scrolling.
Syntax:
MORE :It also displays the file page by page .To continue scrolling with more command ,press
the space bar key.
Syntax: $more<filename>
GREP :This command is used to search and print the specified patterns from the file.
Syntax: $grep [option] pattern <filename>
SORT :This command is used to sort the datas in some order.
Syntax: $sort<filename>
PIPE :It is a mechanism by which the output of one command can be channeled into the input of
another command.
$who | wc-l
Syntax:
TR :Thetr filter is used to translate one set of characters from the standard inputs to another.
Syntax: $tr ―[a-z]‖ ―[A-Z]‖
Post-Experiment Questions:
Experiment 2
Description:
Fork System Call: Fork system call is used for creating a new process, which is called child
process, which runs concurrently with the process that makes the fork() call (parent process).
After a new child process is created, both processes will execute the next instruction following
the fork() system call. A child process uses the same pc(program counter), same CPU registers,
same open files which use in the parent process.
It takes no parameters and returns an integer value. Below are different values returned by fork().
Negative Value: creation of a child process was unsuccessful. Zero: Returned to the newly
created child process. Positive value: Returned to parent or caller. The value contains process ID
of newly created child process.
Program:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void main ()
{
int pid;
printf ("I'm the original process with Process id: %d and my parent id is: %d.\n",
getpid (), getppid ());
pid = fork (); /* Duplicate. Child and parent continue from here */
if (pid != 0) /* pid is non-zero, so I must be the parent */
{
printf ("I'm the parent process with process id %d and my parent id is %d.\n",
getpid (), getppid ());
printf ("My child's process id is %d\n", pid);
}
else /* pid is zero, so I must be the child */
{
printf ("I'm the child process with process id %d and my parent id is %d.\n",
getpid (), getppid ());
}
printf("Process with process id %d terminates.\n",getpid ());/*Both procs execute this */
}
Output:
I'm the original process with Process id: 3514 and my parent id is: 1923.
I'm the parent process with process id 3514 and my parent id is 1923.
My child's process id is 3515
Process with process id 3514 terminates.
I'm the child process with process id 3515 and my parent id is 3514.
Process with process id 3515 terminates.
Post-Experiment Questions:
1. Explain c program to implement the Unix or Linux command to implement ls -l
>output.txt?
2. Explain the mount and unmount system calls?
Experiment 3
Description:
The file structure related system calls available in the UNIX system let you create, open, and
close files, read and write files, randomly access files, alias and remove files, get information
about files, check the accessibility of files, change protections, owner, and group of files, and
control devices. These operations either use a character string that defines the absolute or relative
path name of a file, or a small integer called a file descriptor that identifies the I/O channel.
There are four system calls for file management:
1. open ()
2. read ()
3. write ()
4. close ()
open ()
open() system call is used to know the file descriptor of user-created files. Since read and write
use file descriptor as their 1st parameter so to know the file descriptor open() system call is used.
Syntax:
fd = open (file_name, mode, permission);
Example:
fd = open ("file", O_CREAT | O_RDWR, 0777);
Here,
mode is used to define the file opening modes such as create, read, write modes.
Syntax:
length = write(file_descriptor , buffer, len);
Example:
n = write(fd, "Hello world!", 12);
Here,
file descriptor is the file descriptor of the file.
buffer is the name of the buffer to be stored.
len is the length of the data to be written.
Return value: If successful write() returns the number of bytes actually written.
close ()
close() system call is used to close the opened file, it tells the operating system that you are done
with the file and close the file.
Syntax:
int close(int fd);
Here,
fd is the file descriptor of the file to be closed.
Return value: If file closed successfully it returns 0, else it returns -1.
SOLUTION
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
int main()
{
int n,fd;
char buff[50]; // declaring buffer
//message printing on the display
printf("Enter text to write in the file:\n");
//read from keyboard, specifying 0 as fd for std input device
//Here, n stores the number of characters
n= read(0, buff, 50);
// creating a new file using open.
fd=open("file",O_CREAT | O_RDWR, 0777);
Post-Experiment Questions:
1. What is system call and how it is handled by operating system?
2. What are 5 different categories of system calls?
Experiment 4
Aim: To Execute UNIX system call for Input Output System Calls.
Description:
Algorithm:
1. Start the program.
2. open a file for O_RDWR for R/W,O_CREATE for creating a file , O_TRUNC for truncate a
file
3. Using getchar(), read the character and stored in the string[] array
4. The string [] array is write into a file close it.
5. Then the first is opened for read only mode and read the characters and displayed It and close
the file
6.Stop the program
Program:
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
main( )
{
int fd[2];
char buf1[25]= "just a test\n";
char buf2[50];
fd[0]=open("file1",O_RDWR);
fd[1]=open("file2",O_RDWR);
write(fd[0], buf1, strlen(buf1));
printf("\n Enter the text now….");
scanf("\n %s",buf1);
printf("\n Cat file1 is \n hai");
write(fd[0], buf1, strlen(buf1));
lseek(fd[0], SEEK_SET, 0);
read(fd[0], buf2, sizeof(buf1));
write(fd[1], buf2, sizeof(buf2));
close(fd[0]);
close(fd[1]);
printf("\n");
return 0;
}
OUTPUT:
Post-Experiment Questions:
1. What is file descriptor?
2. Explain some Input/output system calls?
Experiment 5
Program:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("nEnter Burst Time:n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("nProcesst Burst Time tWaitingTimetTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}
Output:
Post-Experiment Questions:
1. What is scheduling?
2. Explain short, medium and long term scheduler?
Experiment 6
Description: Priority scheduling is one of the most common scheduling algorithms in batch
systems. Each process is assigned a priority. Process with the highest priority is to be executed
first and so on. Processes with the same priority are executed on first come first served basis.
Priority can be decided based on memory requirements, time requirements or any other resource
requirement.
Program:
void main()
{
int i,j,n,t,turn[20],burst[20],p[20],wt[20],c[20];
float await,aturn,twait=0,tturn=0;
printf(―\nEnter the value of n:‖);
scanf(―%d‖,&n);
printf(―\n Enter the process no burst and arrivaltime‖);
for(i=0;i<n;i++)
{
scanf(―%d‖,&c[i]);
scanf(―%d‖,&burst[i]);
scanf(―%d‖,&p[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(p[i]>p[j])
{
t=p[i];
p[i]=p[j];
p[j]=t;
t=burst[i];
burst[i]=burst[j];
burst[j]=t;
t=c[i];
c[i]=c[j];
c[j]=t;
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
wt[i]=0;
turn[i]=burst[i];
}
}
else
{
turn[i]=turn[i-1]+burst[i];
wt[i]=turn[i]-burst[i];
twait=twait+wt[i];
tturn=tturn+turn[i];
}await=twait/n;
aturn=tturn/n;
printf(―pno\tbtime\tatime\twtime\tttime‖);
for(i=0;i<n;i++)
{
printf(―\n%d\t%d\t%d\t%d\t%d\n‖,c[i],burst[i],p[i],wt[i],turn[i]);
}
printf(―\n The average waiting time is:%f‖,await);
printf(―\n The average turn around time is:%f‖,aturn);
}
Output:
Enter the process burst no and priority
1 15 2
251
3 10 3
p no btime priority wtime ttime
2 5 1 0 5
1 15 2 5 20
3 10 3 20 30
The average waiting time is :8.333333
The average turn around time is:18.333334
Post-Experiment Questions:
1. Why CPU scheduling is necessary?
2. What is context switching?
Experiment 7
Program:
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
Output:
Post-Experiment Questions:
1. Explain about FCFS scheduling algorithm?
2. What is average waiting time?
Experiment 8
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.
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]);
}
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];
wtavg = wtavg + wt[i]; tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\t SYSTEM/USER PROCESS \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],su[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
getch();
}
OUTPUT
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
RESULT
PROCESS SYSTEM/USER PROCESS BURST TIME WAITING TIME TURNAROUND
TIME 1 0 2 0 2 3 0 1 2 3 2 1 5 3 8 0 1 3 8 11
Average Waiting Time is --- 3.250000
Average Turnaround Time is --- 6.000000
Post-Experiment Questions:
1. Describe Multi-Level Queue scheduling algorithm?
2. Define turnaround time?
Experiment 9
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.
Program:
#include < stdio.h>
#include<conio.h>
void main()
{
int f[50], i, st, len, j, c, k, count = 0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
x: count=0;
printf(―Enter starting block and length of files: ‖);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(‖ The file is allocated to disk\n");
}
else
printf(‖ The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
getch();
}
Output:
Files Allocated are:
Enter starting block and length of files: 14 3
14 1
15 1
16 1
Post-Experiment Questions:
1. Describe contiguous file allocation scheme?
2. Write advantages & disadvantages of contiguous file allocation scheme?
Experiment 10
Description: This method solves all problems of contiguous allocation. In linked allocation
scheme each file is a linked list of disk blocks scattered on the disk. The first word of each block
is used as a pointer to the next one and the rest of block is used for data.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d ------- >%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
Output:
Enter how many blocks already allocated: 3
Post-Experiment Questions:
1. Describe linked list allocation scheme?
2. Write advantages & disadvantages of linked list allocation scheme?
Experiment-11
Description: Indexed allocation method eliminates the disadvantages of linked list allocation by
bringing all the pointers together into one location, called the index block.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d ------- >%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
Output:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk : 4
1234
Allocated File Indexed
5 ------ >1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk : 2
78
Allocated File Indexed
6 ------ >7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0
Post-Experiment Questions:
1. Describe indexed allocation scheme?
2. Write advantages & disadvantages of indexed allocation scheme?
Experiment-12
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 Worst-fit chooses the
largest available block.
Program:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
flag[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();
}
Output:
Input
Enter the number of blocks: 3
Enter the number of files: 2
Output
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
Post-Experiment Questions:
Experiment-13
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 Best-fit strategy chooses
the block that is closest in size to the request.
Program:
#include<stdio.h>
#include<process.h>
void main()
{
int a[20],p[20],i,j,n,m;
printf("Enter no of Blocks.\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst Block size:",i);
scanf("%d",&a[i]);
}
printf("Enter no of Process.\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dstProcess:",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
printf("The Process %d allocated to %d\n",j,a[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
{
if(p[j]!=10000)
{
printf("The Process %d is not allocated\n", j);
}
}
}
Output:
Enter no of Blocks.
5
Enter the 0st Block size:500
Enter the 1st Block size:400
Enter the 2st Block size:300
Enter the 3st Block size:200
Enter the 4st Block size:100
Enter no of Process. 5
Enter the size of 0st Process:100
Enter the size of 1st Process:350
Enter the size of 2st Process:400
Enter the size of 3st Process:150
Enter the size of 4st Process:200
Post-Experiment Questions:
1. What type of fragmentation best fit strategy suffers?
2. Write advantages & disadvantages of best fit scheme?
Experiment-14
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 First-fit chooses the first
available block that is large enough.
Program:
#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;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
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();
}
Output:
Input
Enter the number of blocks: 3
Enter the number of files: 2
Output
File No File Size Block No Block Size Fragment
1 1 3 7 6
2 4 1 5 1
Post-Experiment Questions:
1. What is fragmentation? Explain its types.
2. Write advantages & disadvantages of first fit scheme?
Experiment-15
Aim: Calculation of external and internal fragmentation, free space list of blocks from system,
List process file from the system.
Description:
IMPLEMENTATION DETAILS:
INPUT/s:
(i) Free space list of blocks from system (as in program 4).
(ii) List processes and files from the system (as in program 4).
STEPS TO PERFORM:-
(i) Completing experiment contiguous allocation techniques, we end up getting list of allotted files,
remaining part of allotted block and blocks which cannot be allotted
(ii) After implementing each allocation algorithm, list the amount of free space blocks left out after
performing allocation.
(iii) When a block which is not at all allocated to any process or file, it adds to external
fragmentation.
(iv) When a file or process is allocated the free block and still some part of it is left unused, we
count such unused portion into internal fragmentation.
OUTPUT/s:
Processes and files allocated to free blocks. From the list of unused blocks, we determine the count
of total internal and external fragmentation.
Experiment-16
Aim: Implementation of Compaction for the continually changing memory layout and calculate
total movement of data.
Description:
Compaction is a process in which the free space is collected in a large memory chunk to make
some space available for processes.
In memory management, swapping creates multiple fragments in the memory because of the
processes moving in and out.
Compaction refers to combining all the empty spaces together and processes.
Compaction helps to solve the problem of fragmentation, but it requires too much of CPU time.
It moves all the occupied areas of store to one end and leaves one large free space for incoming
jobs, instead of numerous small ones.
In compaction, the system also maintains relocation information and it must be performed on
each new allocation of job to the memory or completion of job from memory.
ALGORITHM
Compaction is a method to overcome the external fragmentation problem.
All free blocks are brought together as one large block of free space.
Compaction requires dynamic relocation. Certainly, compaction has a cost and selection of an
optimal compaction strategy is difficult.
One method for compaction is swapping out those processes that are to be moved within the
memory, and swapping them into different memory locations.
Solution:
#include<stdio.h>
#include<conio.h>
void create(int,int);
void del(int);
void compaction();
void display();
int fname[10],fsize[10],fstart[10],freest[10],freesize[10],m=0,n=0,start;
int main()
{
int name,size,ch,i;
int *ptr;
// clrscr();
ptr=(int *) malloc(sizeof(int)*100);
start=freest[0]=(int)ptr;
freesize[0]=500;
printf("\n\n");
printf(" Free start address Free Size \n\n");
for(i=0;i<=m;i++)
printf(" %d %d\n",freest[i],freesize[i]);
printf("\n\n");
while(1)
{
printf("1.Create.\n");
printf("2.Delete.\n");
printf("3.Compaction.\n");
printf("4.Exit.\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the name of file: ");
scanf("%d",&name);
printf("\nEnter the size of the file: ");
scanf("%d",&size);
create(name,size);
break;
case 2:
printf("\nEnter the file name which u want to delete: ");
scanf("%d",&name);
del(name);
break;
case 3:
compaction();
printf("\nAfter compaction the tables will be:\n");
display();
break;
case 4:
exit(1);
default:
printf("\nYou have entered a wrong choice.\n");
}
}
for(i=0;i<=m;i++)
if( freesize[i] >= size)
a=i,flag=0;
if(!flag)
{
for(j=0;j<n;j++);
n++;
fname[j]=name;
fsize[j]=size;
fstart[j]=freest[a];
freest[a]=freest[a]+size;
freesize[a]=freesize[a]-size;
flag=1;
compaction();
display();
for(i=0;i<=m;i++)
if( freesize[i] >= size)
a=i,flag=0;
if(!flag)
{
for(j=0;j<n;j++);
n++;
fname[j]=name;
fsize[j]=size;
fstart[j]=freest[a];
freest[a]+=size;
freesize[a]-=size;
printf("\n The memory map will now be: \n\n");
display();
}
else
printf("\nNo enough space.\n");
}
}
fsize[k]=fsize[k+1];
fstart[k]=fstart[k+1];
}
n--;
}
if(flag)
{
printf("\n\n After deletion of this process the memory map will be : \n\n");
display();
}
}
void compaction()
{
int i,j,size1=0,f_size=0;
if(fstart[0]!=start)
{
fstart[0]=start;
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];
}
else
{
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];
}
f_size=freesize[0];
for(j=0;j<=m;j++)
size1+=freesize[j];
freest[0]=freest[0]-(size1-f_size);
freesize[0]=size1;
m=0;
}
void display()
{
int i;
printf("\n\n");
printf("\n\n*** FREE SPACE TABLE ***\n\n");
printf("FREE START ADDRESS FREE SIZE \n\n");
for(i=0;i<=m;i++)
printf(" %d %d\n",freest[i],freesize[i]);
}
OUTPUT:
Post-Experiment Questions:
1. Which problem is solved by using the technique of compaction?
2. Write disadvantages of compaction?
Experiment-17
Description
A resource allocation graph tracks which resource is held by which process and which process is
waiting for a resource of a particular type. It is very powerful and simple tool to illustrate how
interacting processes can deadlock. If a process is using a resource, an arrow is drawn from the
resource node to the process node. If a process is requesting a resource, an arrow is drawn from
the process node to the resource node.
If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only
one instance, then the processes will deadlock. For example, if process 1 holds resource A,
process 2 holds resource B and process 1 is waiting for B and process 2 is waiting for A, then
process 1 and process 2 will be deadlocked.
ALGORITHM
(processes) in the graph. An entry arr[i] represents the linked list of vertices (resources
requested by process) adjacent to the ith vertex.
Questions:
Experiment 18
Description: The banker‟s algorithm is a resource allocation and deadlock avoidance algorithm
that tests for safety by simulating the allocation for predetermined maximum possible amounts of
all resources, then makes an ―s-state‖ check to test for possible activities, before deciding
whether allocation should be allowed to continue.
Following Data structures are used to implement the Banker‟s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available:
It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max:
It is a 2-d array of size „n*m’ that defines the maximum demand of each process in a system.
Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation:
It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently
allocated to each process.
Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj
Need:
It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
Need [ i, j ] = k means process Pi currently need ‘k’ instances of resource type Rj for its
execution.
Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
Allocationi specifies the resources currently allocated to process Pi and Needi specifies the
additional resources that process Pi may still request to complete its task.
Safety Algorithm
The algorithm for finding out whether or not a system is in a safe state can be described as
follows:
1) Let Work and Finish be vectors of length „m‟ and „n‟ respectively. Initialize:
Work = Available Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work if no such i exists goto step (4)
3) Work = Work + Allocation[i] Finish[i] = true goto step (2)
4) if Finish [i] = true for all i then the system is in a safe state
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k
instances of resource type Rj. When a request for resources is made by process Pi, the following
actions are taken:
1) If Requesti <= Needi Goto step (2) ; otherwise, raise an error condition, since the process has
exceeded its maximum clAim.
2) If Requesti <= Available Goto step (3); otherwise, Pi must wait, since the resources are not
available.
3) Have the system pretend to have allocated the requested resources to process Pi by modifying
the state as follows:
Available = Available – Requesti Allocationi = Allocationi + Requesti Needi = Needi– Requesti
Program:
#include <stdio.h>
int main()
{
int count = 0, m, n, process, temp, resource;
int allocation_table[5] = {0, 0, 0, 0, 0};
int available[5], current[5][5], maximum_clAim[5][5];
int maximum_resources[5], running[5], safe_state = 0;
printf("\nEnter The Total Number Of Processes:\t");
scanf("%d", &process);
for(m = 0; m < process; m++)
{
running[m] = 1;
count++;
}
printf("\nEnter The Total Number Of Resources To Allocate:\t");
scanf("%d", &resource);
printf("\nEnter The ClAim Vector:\t");
for(m = 0; m < resource; m++)
{
scanf("%d", &maximum_resources[m]);
}
printf("\nEnter Allocated Resource Table:\n");
for(m = 0; m < process; m++)
{
for(n = 0; n < resource; n++)
{
scanf("%d", ¤t[m][n]);
}
}
printf("\nEnter The Maximum ClAim Table:\n");
for(m = 0; m < process; m++)
{
for(n = 0; n < resource; n++)
{
scanf("%d", &maximum_clAim[m][n]);
}
}
printf("\nThe ClAim Vector \n");
for(m = 0; m < resource; m++)
{
printf("\t%d ", maximum_resources[m]);
}
printf("\n The Allocated Resource Table\n");
for(m = 0; m < process; m++)
{
for(n = 0; n < resource; n++)
{
printf("\t%d", current[m][n]);
}
printf("\n");
}
printf("\nThe Maximum ClAim Table \n");
for(m = 0; m < process; m++)
{
for(n = 0; n < resource; n++)
{
printf("\t%d", maximum_clAim[m][n]);
}
printf("\n");
}
for(m = 0; m < process; m++)
{
for(n = 0; n < resource; n++)
{
allocation_table[n] = allocation_table[n] + current[m][n];
}
}
printf("\nAllocated Resources \n");
for(m = 0; m < resource; m++)
{
printf("\t%d", allocation_table[m]);
}
for(m = 0; m < resource; m++)
{
available[m] = maximum_resources[m] - allocation_table[m];
}
printf("\nAvailable Resources:");
for(m = 0; m < resource; m++)
{
printf("\t%d", available[m]);
}
printf("\n");
while(count != 0)
{
safe_state = 0;
for(m = 0; m < process; m++)
{
if(running[m])
{
temp = 1;
for(n = 0; n < resource; n++)
{
if(maximum_clAim[m][n] - current[m][n] > available[n])
{
temp = 0;
break;
}
}
if(temp)
{
printf("\nProcess %d Is In Execution \n", m + 1);
running[m] = 0;
count--;
safe_state = 1;
for(n = 0; n < resource; n++)
{
available[n] = available[n] + current[m][n];
}
break;
}
}
}
if(!safe_state)
{
printf("\nThe Processes Are In An Unsafe State \n");
break;
}
else
{
printf("\nThe Process Is In A Safe State \n");
printf("\nAvailable Vector\n");
for(m = 0; m < resource; m++)
{
printf("\t%d", available[m]);
}
printf("\n");
}
}
return 0;
}
OUTPUT:
Post-Experiment Questions:
1. The Banker’s algorithm is used for .
2. is the situation in which a process is waiting on another process, which is also
waiting on another process ... which is waiting on the first process. None of the processes
involved in this circular wait are making progress.
3. What is safe state?
4. What are the conditions that cause deadlock?
Experiment-19
Aim: Implement the solution for Bounded Buffer (producer-consumer) problem using inter
process communication.
Problem : To make sure that the producer won‟t try to add data into the buffer if it‟s full and
that the consumer won‟t try to remove data from an empty buffer.
Solution : The producer is to either go to sleep or discard data if the buffer is full. The next time
the consumer removes an item from the buffer, it notifies the producer, who starts to fill the
buffer again. In the same way, the consumer can go to sleep if it finds the buffer to be empty. The
next time the producer puts data into the buffer, it wakes up the sleeping consumer. An
inadequate solution could result in a deadlock where both processes are waiting to be
awakened.
In the post Producer-Consumer solution, we have discussed above solution by using inter-thread
communication(wait(), notify(), sleep()). In this post, we will use Semaphores to implement the
same.
The below solution consists of four classes:
1. Q : the queue that you‟re trying to synchronize
2. Producer : the threaded object that is producing queue entries
3. Consumer : the threaded object that is consuming queue entries
4. PC : the driver class that creates the single Q, Producer, and Consumer.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
int buf[256];
int in = 0;
int out = 0;
sem_t full;
sem_t empty;
sem_t mutex;
int buf_size;
int counter = 0;
void *producer(void *arg)
{
int i, item, *index;
index = (int *)arg;
for (i = 0;; i++)
{
item = 1000 + i;
sem_wait(&empty);
sem_wait(&mutex);
buf[in] = item;
in = (in + 1) % (*index);
counter++;
printf("\n%d [P%d] ", item, *index);
sem_post(&mutex);
sem_post(&full);
/* if (i % 5 == 0)
sleep(1); */
}
}
void *consumer(void *arg)
{
int i, item, *index;
index = (int *)arg;
for (i = 0;;i++)
{
sem_wait(&full);
sem_wait(&mutex);
item = buf[out];
out = (out + 1) % (*index);
counter--;
printf("\n%d [C%d] ", item, *index);
sem_post(&mutex);
sem_post(&empty);
/* if (i % 5 == 0)
sleep(1); */
}
}
int main()
{
int produce, consume;
int i;
printf("\nThe Buffer Size:");
scanf("%d", &buf_size);
printf("\nThe Producer:");
scanf("%d", &produce);
printf("\nThe Consumer:");
scanf("%d", &consume);
pthread_t prod, cons;
void* exit_status;
sem_init(&full, 0, 0);
sem_init(&empty, 0, buf_size);
sem_init(&mutex, 0, 1);
for (i = 0; i < produce; i++)
{
pthread_create(&prod, NULL, producer, &i);
}
for (i = 0; i < consume; i++)
{
pthread_create(&cons, NULL, consumer, &i);
}
pthread_join(prod, &exit_status);
pthread_join(cons, &exit_status);
// pthread_exit(NULL);
return 0;
}
Output:
Producer produced item: 0
Consumer consumed item: 0
Producer produced item: 1
Consumer consumed item: 1
Producer produced item: 2
Consumer consumed item: 2
Producer produced item: 3
Consumer consumed item: 3
Producer produced item: 4
Consumer consumed item: 4
Experiment-20
Aim: Implement the solutions for Readers-Writers problem using inter-process communication
technique -Semaphore.
Description: Consider a situation where we have a file shared between many people.
If one of the people tries editing the file, no other person should be reading or writing at
the same time, otherwise changes will not be visible to him/her.
However if some person is reading the file, then others may read it at the same time.
2. If allowed i.e. wait() gives a true value, it enters and performs the write. If not allowed, it
keeps on waiting.
3. It exits the critical section.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdbool.h>
struct semaphore
{
int mutex;
int rcount;
int rwait;
bool wrt;
};
void addR(struct semaphore *s)
{
if (s->mutex == 0 && s->rcount == 0)
{
printf("\nSorry, File open in Write mode.\nNew Reader added to queue.\n");
s->rwait++;
}
Else {
printf("\nReader Process added.\n");
s->rcount++;
s->mutex--;
}
return ;
}
void addW(struct semaphore *s)
{
if(s->mutex==1)
{
s->mutex--;
s->wrt=1;
printf("\nWriter Process added.\n");
}
else if(s->wrt) printf("\nSorry, Writer already operational.\n");
else printf("\nSorry, File open in Read mode.\n");
return ;
}
void remR(struct semaphore *s)
{
if(s->rcount == 0) printf("\nNo readers to remove.\n");
else
{
printf("\nReader Removed.\n");
s->rcount--;
s->mutex++;
}
return ;
}
void remW(struct semaphore *s)
{
if(s->wrt==0) printf("\nNo Writer to Remove");
else
printf("\nWriter Removed\n");
s->mutex++;
s->wrt=0;
if(s->rwait!=0)
{
s->mutex-=s->rwait;
s->rcount=s->rwait;
s->rwait=0;
printf("%d waiting Readers Added.",s->rcount);
}
}
}
int main()
{
struct semaphore S1={1,0,0};
while(1)
{
//system("cls");
printf("Options :-\n1.Add Reader.\n2.Add Writer.\n3.Remove Reader.\n4.Remove
Writer.\n5.Exit.\n\n\tChoice : ");
int ch;
scanf("%d‖, &ch);
switch(ch)
{
case 1: addR(&S1); break;
case 2: addW(&S1); break;
case 3: remR(&S1); break;
case 4: remW(&S1); break;
case 5: printf("\n\tGoodBye!"); getch(); return 0;
default: printf("\nInvalid Entry!"); continue;
}
printf("\n\n<<<<<< Current Status >>>>>>\n\n\tMutex\t\t:\t%d\n\tActive
Readers\t:\t%d\n\tWaiting Readers\t:\t%d\n\tWriter Active\t:\t%s\n\n", S1.mutex, S1.rcount,
S1.rwait, (S1.mutex==0 && S1.rcount==0) ? "YES" : "NO");
system("pause");
}}
OUTPUT
File open in Write mode
New reader added to queue
Post-Experiment Questions:
EXPERIMENT-21
Descriptions:
THEORY:
FIFO algorithm:
The simpler page replacement algorithm is a FIFO algorithm. A FIFO replacement algorithm
associates with each page the time when that page was brought into memory. When a page must
be replace, the oldest page is chosen. We can create a FIFO queue to hold all pages in memory.
We replace the page at the head of the queue when a page is brought into memory; we insert it at
the tail of the queue.
ALGORITHM:
1. Start
2. Read the number of frames
3. Read the number of pages
4. Read the page numbers
5. Initialize the values in frames to -1
6. Allocate the pages in to frames in First in first out order.
7. Display the number of page faults.
8. stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
clrscr();
}}
printf("\n\n\t\t No.of pages faults...%d",pf);
getch();
}
OUTPUT:
Post-Experiment Questions:
1. Define FIFO?
2. Which of the following statement is not true? a) Multiprogramming implies multitasking b)
Multi-user does not imply multiprocessing c) Multitasking does not imply multiprocessing d)
Multithreading implies multi-user
3. Define page?
4.Define Frame?
EXPERIMENT-22
Descriptions:
In the Least Recently Used (LRU) page replacement policy, the page that is used least
recently will be replaced.
Implementation:
o Add a register to every page frame - contain the last time that the page in that
frame was accessed
o Use a "logical clock" that advance by 1 tick each time a memory reference is
made.
The following figure shows the behaviour of the program in paging using the LRU page
replacement policy:
o We can see notably that the bad replacement decisions made by FIFO is not
present in LRU.
o There are total of 9 page read operations to satisfy the total of 18 page requests -
that is almost a 20% improvement over FIFO in such a short experiment
o (I only want to make the point here that page replacement policy can affect the
system performance. I do not want to get into the question of "how much better is
LRU than FIFO").
In fact, it has been shown empirically that LRU is the preferred page replacement
policy.
ALGORITHM :
1. Start
2. Read the number of frames
3. Read the number of pages
4. Read the page numbers
5. Initialize the values in frames to -1
6. Allocate the pages in to frames by selecting the page that has not been used for the longest
period of time.
7. Display the number of page faults.
8. stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames. .. ");
scanf("%d",&nof);
printf(" Enter no.of reference string..");
scanf("%d",&nor);
printf("\n Enter reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM "); 41
printf("\n\t The given reference string:");
printf("\n… ........................................... ");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference NO %d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
getch();
}
int lruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++) 42
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}
OUTPUT:
LRU PAGE REPLACEMENT ALGORITHM
Enter no.of Frames... 3
Enter no.of reference string 6
Enter reference string..
654231
LRU PAGE REPLACEMENT ALGORITHM
The given reference string:
…………………. 6 5 4 2 3 1
Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
No.of page faults...6
Post-Experiment Questions:
1. In which of the following page replacement policies, Bolady’s anomaly occurs?
(A)FIFO (B)LRU (C)LFU (D)SRU
2. Explain the difference between FIFO and LRU?
3. The operating system manages . 1 Memory 2 Processor 3 Disk and I/O devices 4 All
of the above
EXPERIMENT- 23
Descriptions: In operating systems, whenever a new page is referred and not present in
memory, page fault occurs and Operating System replaces one of the existing pages with
newly needed page. Different page replacement algorithms suggest different ways to decide
which page to replace. The target for all algorithms is to reduce number of page faults.
In this algorithm, OS replaces the page that will not be used for the longest period of time in
future.
ALGORITHM:
1. Start
2. Read the number of frames
3. Read the number of pages
4. Read the page numbers
5. Initialize the values in frames to -1
6. Allocate the pages in to frames by selecting the page that will not be used for the longest
period of time.
7. Display the number of page faults.
8. stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
{
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");
printf("\n. .............................. ");
printf("\nEnter the no.of frames");
scanf("%d‖, &nof);
printf("Enter the no.of reference string");
scanf("%d",&nor);
printf("Enter the reference string");
for (i=0; i<nor;i++)
scanf("%d",&ref[i]);
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\n. ............................. ");
printf("\nThe given string");
printf("\n. .................. \n");
for (i=0; i<nor;i++)
printf("%4d‖, ref[i]);
for (i=0; i<nof;i++)
{
frm[i]=-1;
optcal[i]=0;
}
for (i=0; i<10; i++)
recent[i]=0;
printf("\n");
for (i=0; i<nor;i++)
{
flag=0;
printf("\n\tref no %d ->\t",ref[i]);
for (j=0; j<nof; j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
frm[victim]=ref[i];
for (j=0; j<nof; j++)
printf("%4d‖, frm[j]);
}
}
printf("\n Number of page faults: %d",pf);
getch();
}
int optvictim(int index)
{
int i,j,temp,notfound;
for (i=0; i<nof;i++)
{
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
{
notfound=0;
optcal[i]=j;
break;
}
if(notfound==1)
return i;
}
temp=optcal[0];
for (i=1; i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for (i=0; i<nof; i++)
if(frm[temp]==frm[i])
return i;
return 0;
}
OUTPUT:
OPTIMAL PAGE REPLACEMENT ALGORITHM
Enter no. of Frames. .. 3
Enter no. of reference string.6
Enter reference string.
654231
Post-Experiment Questions:
APPENDIX
AKTU SYLLABUS