OS Lab Manual
OS Lab Manual
OS Lab Manual
Experiments
S.N. Page Name of Experiments Signature Remarks
No
7. Implement the first fit; best fit and worst fit file
allocation strategy.
26-29
Aim:-
To execute the following system calls in UNIX operating system.
COMMAND
1.) fork()
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.
1
PROGRAM
#include <stdio.h>
#include<stdio.h>
#include <sys/types.h>
#include<sys/types.h>
int main()
#include<unistd.h>
{
void main()
fork();
{
fork();
// make two process which run same
fork();
// program after this instruction
printf("hello\n");
fork();
return 0;
}
printf("Hello world!\n");
Output
hello
}
hello
Output:-
hello
hello
Hello world!
hello
Hello world!
hello
hello
hello
2.exec()
exec command in Linux is used to execute a command from the bash itself. This command does not create
a new process it just replaces the bash with the command to be executed. If the exec command is
successful, it does not return to the calling process.
Syntax:-
exec [-cl] [-a name] [command [arguments]] [redirection ...]
Options:
c: It is used to execute the command with empty environment.
a name: Used to pass a name as the zeroth argument of the command.
l: Used to pass dash as the zeroth argument of the command.
Note: exec command does not create a new process. When we run the exec command from the terminal,
the ongoing terminal process is replaced by the command that is provided as the argument for the exec
command.
Command
echo 'this is the input text.'>in.txt
exec wc -w < in.txt
Output:-
5
---------------File cretead--------------------
In.txt
this is the input text
2
3. Mkdir()
We have used ‘$ cd’ (below) to get into the newly created directory and again on giving ‘$ pwd’
command,we are displayed with the new ‘newfolder’ directory.
Command
$ mkdir newfolder
$ cd newfolder
$ pwd
Output: /home/kali/newfolder
4. cat()
Cat is short for concatenate. This command displays the contents of one or more files without having to o
pen the file for editing.
Syntax
cat [options] filename(s)
For example, to display the contents of a file with each line numbered, use the –n option:
cat –n filename
filename(s) – Specify the name of the file (or files) that you want to display
Examples:-
1. Create a New file
cat >test1.txt
cat test1.txt
3
5.open -
How it works in OS
Syntax
$ open foldername
Command:-
$open newfolder
6. Date Command :
This command is used to display the current data and time.
Command
$date
$date +%ch
4
Options : -
7. History
One of the extensively used command in UNIX world is the history command. The bash shell stores a
history of commands entered, which can be used to repeat commands by using the history command. By
default, it’ll show the previous 1000 commands that were used.
COMMAND
$ history
Output
1 shutdown
2 shutdown -c
3 shutdown -h now
4 help
5 sudo
6 sudo apt
7 read a b
........
5
8.clear
The ‘$ clear’ command is used to clean up the terminal so that you can type with more accuracy
COMMAND
$ clear
Output
9. pwd
The ‘$pwd’ command stands for ‘print working directory’ and as the name says,it displays the directory in
which we are currently (directory is same as folder for Windows OS users).
In the output we are sike directory(folder for Windows OS that are moving to Linux),which is present
inside the home directory .
Command
$ pwd
Output: /home/kali
10. ls
The ‘ls’ command simply displays the contents of a directory.
$ ls
Output 1:
Desktop Documents Downloads in.txt Music newfolder Pictures Public sikendar spaceAPI Templates
test1.txt Videos
11. cd
The ‘$ cd’ command stands for ‘change directory’ and it changes your current directory to the ‘newfolder’
directory. You can understand this a double-clicking a folder and then you do some stuff in that folder.
$ cd newfolder
6
Exp.No. 2a
Aim:
Algorithm
Step 1 : Read the input variables and assign the value
Step 2 : Perform the various arithmetic operations
Step 3 : Print the result after the arithmetic
operations.
Step 4 : Stop
Command
echo Enter the first number
read a
echo Enter the second number
read b
echo `expr $a + $b`
echo `expr $a - $b`
echo `expr $a \* $b`
echo `expr $a / $b`
echo `expr $a % $b`
Output
Enter the first number
10
Enter the second number
2
12
8
20
5
0
Result:-
Thus the shell program to perform arithmetic operation is executed and output is verified successfully.
7
2. b) To check the given number is even or odd
ALGORITHM
Step 1 : Start the progam.
STEP 2: Read the value of n.
STEP 3: Calculate “r=expr $n%2”.
STEP 4: If the value of r equals 0 then print the number is even
STEP 5: If the value of r not equal to 0 then print the number is odd.
STEP 6 : Stop
Command
SAMPLE OUTPUT 1
SAMPLE OUTPUT 2
Enter the Number
88
88 is Even number
Result:-
Thus the shell program to perform find the given number is odd or even is executed and output is
verified successfully.
8
ExNo:3 a FIRST COME FIRST SERVE
AIM:
To write a C program to implement the CPU scheduling algorithm for FIRST COME FIRST SERVE.
PROBLEM DESCRIPTION:
Cpu scheduler will decide which process should be given the CPU for its execution. . For this it use
different algorithm to choose among the process. one among that algorithm is sjf algorithm.
In this algorithm the process which arrive first is given the cpu after finishing its request only it will allow
cpu to execute other process.
ALGORITHM:
Step1: Start
Step2: Declare the array size
Step3: Read the number of processes to be inserted
Step4: Read the Burst time of processes.
Step5: Calculate the waiting time of each process w[i+1]=bt[i]+wt[i]
Step6: Calculate the turnaround time of each process tt[i+1]=tt[i]+bt[i+1]
Step7 : Calculate
• Average waiting time = Total waiting time/No of process
• Average turnaround time = Total turn around time/no of process
Step 8 : Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,bt[20],wt[20],tat[20],n;
float wtavg,tatavg;
printf("\n Enter the number of process --");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst time for process %d --",i);
scanf("%d",&bt[i]);
}
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];
}
9
printf("\t PROCESS \t BURST TIME \t WAITING TIME \t TURNAROUND TIME \n");
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:-
RESULT:
Thus the C program to implement CPU scheduling algorithm for first come first serve was executed
successfully.
10
SHORTEST JOB FIRST
AIM:
To write a C program to implement the CPU scheduling algorithm for Shortest job first.
PROBLEM DESCRIPTION:
In this algorithm the process which has less service time given the cpu after finishing its request only it
will allow cpu to execute next other process.
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.
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<stdio.h>
#include<conio.h>
main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
//clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
11
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++)
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();
}
OUTPUT
Result
Thus the C program to implement the CPU scheduling algorithm for shortest job first was executed
successfully.
12
PRIORITY SCHEDULING
AIM:
To write a C program to implement CPU scheduling algorithm for priority scheduling.
PROBLEM DESCRIPTION:
In this algorithm the process which arrive first is given the cpu after finishing its request only it will
allow cpu to execute other process.
ALGORITHM:
1. Start
2. Declare the array size
3. Read the number of processes to be inserted
4. Read the Priorities of processes
5. sort the priorities and Burst times in ascending order
calculate the waiting time of each process wt[i+1]=bt[i]+wt[i]
6. calculate the turnaround time of each process tt[i+1]=tt[i]+bt[i+1]
Calculate the average waiting time and average turnaround time.
7. Display the values
8. Stop
PROGRAM
#include<stdio.h>
main()
{
int p[20],bt[20],pri[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 & 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;
13
}
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\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]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
getch();
}
OUTPUT
RESULT:
Thus C program to implement CPU scheduling algorithm for round robin scheduling was executed
successfully.
14
ROUND ROBIN
AIM :
To write a C program to simulate the CPU scheduling algorithm for round robin.
PROBLEM DESCRIPTION:
In this algorithm we are assigning some time slice. The process is allocated according to the time slice ,if
the process service time is less than the time slice then process itself will release the CPU voluntarily.
The scheduler will then proceed to the next process in the ready queue. If the CPU burst of the currently
running process is longer than time quantum ,the timer will go off and will cause an interrupt to the
operating system .A context switch will be executed and the process will be put at the tail of the ready
queue.
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<stdio.h>
main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
//clrscr();
printf("Enter the no of processes -- ");
scanf("%d",&n);
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++)
15
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];
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();
}
OUTPUT
RESULT:
Thus the C program to simulate CPU scheduling algorithm for round robin was executed successfully
16
Ex. No 4
Aim
17
PROGRAM
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
/*
This program provides a possible solution for first readers writers problem using mutex and semaphore.
I have used 10 readers and 5 producers to demonstrate the solution. You can always play with these
values.
*/
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;
}
void *reader(void *rno)
{
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1) {
sem_wait(&wrt); // If this id the first reader, then it will block the writer
}
pthread_mutex_unlock(&mutex);
// Reading Section
printf("Reader %d: read cnt as %d\n",*((int *)rno),cnt);
int main()
{
18
pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);
int a[10] = {1,2,3,4,5,6,7,8,9,10}; //Just used for numbering the producer and consumer
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;
}
OUTPUT
19
Ex. No. 5
Aim
Algorithm
Step 1: Start
Step 2 : Import the libraries for threads and semaphore for synchronization.
Step 5 : initialise the counter i and status message variable as int and a pointer msg, and intialise the
semaphore array.
Step 6 : create the philosopher threads using pthread_create and pass a pointer to the dine function as
the subroutine and a pointer to the counter variable i.
Step 7: All the philosophers start by thinking. We pass chopstick(n) (left) to pthread_mutex_lock to wait
and acquire lock on it.
Step 8: Then the thread waits on the right((n+1) % NUM_CHOPSTICKS) chopstick to acquire a lock on it
(pick it up).
Step 9: When the philosopher successfully acquires lock on both the chopsticks, the philosopher starts
dining (sleep(3)).
Step 10: Once the philosopher finishes eating, we call pthread_mutex_unlock on the left chopstick
(signal) thereby freeing the lock. Then proceed to do the same on the right chopstick.
Step 11: We loop through all philosopher threads and call pthread_join to wait for the threads to finish
executing before exiting the main thread.
20
Step 12: We loop thorough the chopstick array and call pthread_mutex_destroy to destroy the
semaphore array.
Program
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM_PHILOSOPHERS 5
#define NUM_CHOPSTICKS 5
int main()
{
int i, status_message;
void *msg;
// Wait for all philosophers threads to complete executing (finish dining) before closing the program
for (i = 1; i <= NUM_PHILOSOPHERS; i++)
21
{
status_message = pthread_join(philosopher[i], &msg);
if (status_message != 0)
{
printf("\n Thread join failed \n");
exit(1);
}
}
22
OUTPUT
Philosopher 2 is thinking
Philosopher 2 is eating
Philosopher 3 is thinking
Philosopher 5 is thinking
Philosopher 5 is eating
Philosopher 1 is thinking
Philosopher 4 is thinking
Philosopher 4 is eating
Philosopher 2 Finished eating
Philosopher 5 Finished eating
Philosopher 1 is eating
Philosopher 4 Finished eating
Philosopher 3 is eating
Philosopher 1 Finished eating
Philosopher 3 Finished eating
23
Ex. No. 6
Aim
Banker‘s Algorithm:
When a new process enters a system, it must declare the maximum number of
instances of each resource type it needed. This number may exceed the total number of
resources in the system. When the user request a set of resources, the system must
determine whether the allocation of each resources will leave the system in safe state. If
it will the resources are allocation; otherwise the process must wait until some other
process release the resources.
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. Stop the process.
SOURCE CODE
#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=1,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]);
}
printf("Enter the available resources:");
scanf("%d",&avail);
for(i=0;i<n;i++)
{
temp[i]=time[i];
tem[i]=i;
24
}
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;
}
}
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();
}
Output
Enter no of jobs:4
Enter name and time: A 1
Enter name and time: B 4
Enter name and time: C 2
Enter name and time: D 3
Enter the available resources: 20
Safe sequence is: A 1, C 2, D 3, B 4.
25
Ex. No. 7
7) Implement the first fit, best fit and worst fit file allocation strategy
AIM:
ALGORITHM:
PROGRAM
#include<stdio.h>
main()
{
int p[10],np,b[10],nb,ch,c[10],d[10],alloc[10],flag[10],i,j;
printf("\nEnter the no of process:");
scanf("%d",&np);
printf("\nEnter the no of blocks:");
scanf("%d",&nb);
printf("\nEnter the size of each process:");
for(i=0;i<np;i++)
{
printf("\nProcess %d:",i);
scanf("%d",&p[i]);
}
printf("\nEnter the block sizes:");
for(j=0;j<nb;j++)
{
printf("\nBlock %d:",j);
scanf("%d",&b[j]);c[j]=b[j];d[j]=b[j];
}
if(np<=nb)
{
printf("\n1.First fit 2.Best fit 3.Worst fit");
do
{
printf("\nEnter your choice:");
scanf("%d",&ch);
26
switch(ch)
{
case 1: printf("\nFirst Fit\n");
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=b[j])
{
alloc[j]=p[i];printf("\n\nAlloc[%d]",alloc[j]);
printf("\n\nProcess %d of size %d isallocated in block:%d of size:%d",i,p[i],j,b[j]);
flag[i]=0,b[j]=0;break;
}
else
flag[i]=1;
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size %d is notallocated",i,p[i]);
}
break;
case 2: printf("\nBest Fit\n");
for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(c[i]>c[j])
{
int temp=c[i];
c[i]=c[j];
c[j]=temp;
}}}
printf("\nAfter sorting block sizes:");
for(i=0;i<nb;i++)
printf("\nBlock %d:%d",i,c[i]);
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=c[j])
{
alloc[j]=p[i];
printf("\n\nAlloc[%d]",alloc[j]);
printf("\n\nProcess %d of size %d is allocated in block %d of size%d",i,p[i],j,c[j]);
flag[i]=0,c[j]=0;break;
}
27
else
flag[i]=1;
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size %d is not allocated",i,p[i]);
}
break;
case 3: printf("\nWorst Fit\n");
for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(d[i]<d[j])
{
int temp=d[i];
d[i]=d[j];
d[j]=temp;
} } }
printf("\nAfter sorting block sizes:");
for(i=0;i<nb;i++)
printf("\nBlock %d:%d",i,d[i]);
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=d[j])
{
alloc[j]=p[i];
printf("\n\nAlloc[%d]",alloc[j]);
printf("\n\nProcess %d of size %d is allocated in block %d of size%d",i,p[i],j,d[j]);
flag[i]=0,d[j]=0;break;
}
else
flag[i]=1;
}}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size%d is not allocated",i,p[i]);
}
break;
default: printf("Invalid Choice…!");break;
}
}while(ch<=3);
}
28
}
OUTPUT
Enter the no of process:3
Enter the no of blocks:3
Enter the size of each process:
Process 0:100
Process 1:150
Process 2:200
Enter the block sizes:
Block 0:300
Block 1:350
Block 2:200
1.First fit 2.Best fit 3.Worst fit
Enter your choice:1
Alloc[100]
Process 0 of size 100 is allocated in block 0 of size 300
Alloc[150]
Process 1 of size 150 is allocated in block 1 of size 350
Alloc[200]
Process 2 of size 200 is allocated in block 2 of size 200
Enter your choice:2
Best Fit
After sorting block sizes are:
Block 0:200
Block 1:300
Block 2:350
Alloc[100]
Process 0 of size 100 is allocated in block:0 of size:200
Alloc[150]
Process 1 of size 150 is allocated in block:1 of size:300
Alloc[200]
Process 2 of size 200 is allocated in block:2 of size:350
enter your choice:3
Worst Fit
After sorting block sizes are:
Block 0:350
Block 1:300
Block 2:200
Alloc[100]
Process 0 of size 100 is allocated in block 0 of size 350
Alloc[150]
Process 1 of size 150 is allocated in block 1 of size 300
Alloc[200]
Process 2 of size 200 is allocated in block 2 of size 200
Enter your choice:6
Invalid Choice…!
29
8) Write a C program to simulate page replacement algorithms
AIM:
To write a C program to implement FIFO page replacement algorithm.
DESCRIPTION :
The FIFO Page 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 . There is not strictly necessary to record the time when a page is brought in.
By creating a FIFO queue to hold all pages in memory and by replacing the page at
the head of the queue. When a page is brought into memory, insert it at the tail of
the queue.
ALGORITHM:
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Format queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int i, j, k, f, pf=0, count=0, rs[25], m[10], n;
//clrscr();
printf("\n Enter the length of reference string -- ");
scanf("%d",&n);
printf("\n Enter the reference string -- ");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("\n Enter no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
printf("\n The Page Replacement Process is -- \n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
30
break;
}
if(k==f)
{
m[count++]=rs[i];
pf++;
}
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\n The number of Page Faults using FIFO are %d",pf);
getch();
}
OUTPUT
31
EX.NO:8. B) IMPLEMENTATION OF LRU PAGE REPLACEMENT
AIM:
To write C program a program to implement LRU page replacement algorithm.
DESCRIPTION:
The Least Recently Used replacement policy chooses to replace the page which has
not been referenced for the longest time. This policy assumes the recent past will
approximate the immediate future. The operating system keeps track of when each
page was referenced by recording the time of reference or by maintaining a stack of
references.
ALGORITHM:
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
//clrscr();
printf("Enter the length of reference string -- ");
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]); flag[i]=0;
}
printf("Enter the number of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is -- \n");
for(i=0;i<n;i++){
for(j=0;j<f;j++){
if(m[j]==rs[i])
{
flag[i]=1;
32
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
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();
}
OUTPUT
33
4 3 2 PF No. -- 8
0 3 2 PF No. -- 9
032
032
1 3 2 PF No. -- 10
132
1 0 2 PF No. -- 11
102
1 0 7 PF No. -- 12
107
107
The number of page faults using LRU are 12
RESULT:
Thus a UNIX C program to implement LRU page replacement is executed
successfully.
AIM:
To write a program in C to implement LFU page replacement algorithm.
ALGORITHM
Step1: Start the program
Step2: Declare the required variables and initialize it.
Step3; Get the frame size and reference string from the user
Step4: Keep track of entered data elements
Step5: Accommodate a new element look for the element that is not to be used in
frequently replace.
Step6: Count the number of page fault and display the value
Step7: Terminate the program
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
//clrscr();
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0;
34
a[i]=-1;
}
printf("\nThe Page Replacement Process is – \n");
for(i=0;i<m;i++)
{
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();
}
OUTPUT
Enter number of page references -- 10
Enter the reference string -- 1 2 3 4 5 2 5 2 5 1 4 3
Enter the available no. of frames 3
The Page Replacement Process is –
1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
523
523
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8
Total number of page faults -- 8
RESULT:
Thus the C programs to implement LFU page replacement algorithm was
executed successfully.
35
9) Write a C program to simulate disk scheduling algorithm
a) FIFO b)SCAN c)CSCAN
Aim
To write a c program to simulate FIFO disk scheduling algorithm
Algorithm
1. Let Request array represents an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‘head’ is the position of disk head.
2. 2. Let us one by one take the tracks in default order and calculate the absolute distance of
3. the track from the head.
4. 3. Increment the total seek count with this distance.
5. 4. Currently serviced track position now becomes the new head position.
6. 5. Go to step 2 until all tracks in request array have not been serviced.
Source Code
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
OUTPUT
36
SCAN
Aim
To write a C program to simulate SCAN disk scheduling algorithm.
Algorithm
1. Let Request array represents an array storing indexes of tracks that have been requested
in ascending order of their time of arrival. ‘head’ is the position of disk head.
2. Let direction represents whether the head is moving towards left or right.
3. In the direction in which head is moving service all tracks one by one.
4. Calculate the absolute distance of the track from the head.
5. Increment the total seek count with this distance.
6. Currently serviced track position now becomes the new head position.
37
7. Go to step 3 until we reach at one of the ends of the disk.
8. If we reach at the end of the disk reverse the direction and go to step 2 until all tracks in
request array have not been serviced.
Program
#include<conio.h>
#include<stdio.h>
int main()
{
int i,j,sum=0,n;
int d[20];
int disk; //loc of head
int temp,max;
int dloc; //loc of disk in array
//clrscr();
printf("enter number of location\t");
scanf("%d",&n);
printf("enter position of head\t");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&d[i]);
}
d[n]=disk;
n=n+1;
for(i=0;i<n;i++) // sorting disk locations
{
for(j=i;j<n;j++)
{
if(d[i]>d[j])
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
max=d[n];
for(i=0;i<n;i++) // to find loc of disc in array
{
if(disk==d[i]) { dloc=i; break; }
}
for(i=dloc;i>=0;i--)
{
printf("%d -->",d[i]);
}
printf("0 -->");
for(i=dloc+1;i<n;i++)
38
{
printf("%d-->",d[i]);
}
sum=disk+max;
printf("\nmovement of total cylinders %d",sum);
getch();
return 0;
}
Output
Enter no of location 8
Enter position of head 53
Enter elements of disk queue
98
183
37
122
14
124
65
67
53->37->14->0->65->67->98->122->124->183->
Movement of total cylinders 236.
CSCAN
Aim
To write a C program to simulate disk scheduling algorithm using CSCAN
Algorithm:
1. Let Request array represents an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‘head’ is the position of disk head.
2. The head services only in the right direction from 0 to the size of the disk.
3. While moving in the left direction do not service any of the tracks.
4. When we reach the beginning(left end) reverse the direction.
5. While moving in the right direction it services all tracks one by one.
6. While moving in the right direction calculate the absolute distance of the track from the head.
7. Increment the total seek count with this distance.
8. Currently serviced track position now becomes the new head position.
9. Go to step 6 until we reach the right end of the disk.
10. If we reach the right end of the disk reverse the direction and go to step 3 until all tracks in the
request array have not been serviced.
Source Code
#include<stdio.h>
int main()
{
39
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
40
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
Output
41