OS Practical Solutions
OS Practical Solutions
Sr. Initial of
Title Date Remarks
No faculty
To Study and hands on upon various UNIX, VI
1
Editor and Shell’s Commands
Implementation of Shell’s Scripts using basic
2
UNIX commands and control statements
Implementation of Shell’s Scripts demonstrating
3
Loops
Implementation of Shell’s Scripts demonstrating
4 the use of String operations & Command line
Arguments
Implementation of FCFS (First Come First Serve)
5
CPU Scheduling algorithm
Implementation of SJF (Shortest Job First) CPU
6
Scheduling algorithm
Implementation of RR(Round Robin) CPU
7
Scheduling algorithm
Implementation of Priority CPU Scheduling
8
algorithm
Implementation of Fist- Fit Memory Allocation
9
algorithm
Implementation of Best-Fit Memory Allocation
10
algorithm
Implementation of FIFO Page Replacement
11
algorithm
Implementation of LRU Page Replacement
12
algorithm by Stack method
Implementation of Optical Page Replacement
13
algorithm
PRACTICAL – 1
AIM: To Study and hands on upon various UNIX, VI Editor and Shell’s
Commands.
1) cal:
a) Print the calendar of any year in the range 1 to 9999
b) The system not stores this calendar in memory but generate through the calculation when
we invoke command Options
c) If we want to see the calendar of any year then
$ Cal 2006
d) If we want to see the calendar for particular month then
$ Cal 3 2006
2) who:-
a) List all the users who are currently logged in, with their terminal and their log in time.
Option:
b) $ who am i:
It displays our login name, terminal name and log in time.
3) ls:
It list all the filenames in alphabetical order which are present in current directory.
Options:
ls –a : display hidden files also
ls [aeiou]* : display all files which has first letter any from the bracket.
ls –l : It list the files with 7 columns having information about
it’s permission ,number of links, owner name, group name,size of file in bytes,
date and time when file was last modified & filename.
4) cat:
a) It is used to create a new file. Press ctrl + d to indicate EOF.
b) $ cat > test
Crete a filename with test
c) $ cat test
Display the content of test file
c) $ cat file1 file2 > file3
This would create a new file and content of file1 & file2 are copied into it.
5) wc:
a) It counts the number of lines , words and character from the specified file or files.
b) Wc –l filename : display the number of lines
c) Wc-w filename: display the number of words
d) Wc-c filename: display the number of character.
6) pipe :
a) We can join the commands using a pipe, means it sends the output
of one command as input of another.
b) $ ls | wc-l
Here the output of ls becomes the input to wc which promptly counts the number of
lines it receives as input and display this count on the Screen.
7) echo:
It is used to display output. If we use “ “ in echo then the shell treats
the content within “ “ as a single string to be displayed.
8) exit:
It will stop the execution of shell script and exit form the program.
18) grep:
a) grep stands for “globally search a regular expression and print it.” This command search
for the specified input fully for a match with The supplied pattern and display it.
b) $ grep xy abc
This would search the word xy in a file called abc.
19) sort:
a) It is used to sort the content of a file.
b) $ sort abc
It shows the content of file abc in sorted format
c) $ sort file1 file2 file3
This will sort the contents of several files at once.
20) cut:
a) It cuts or picks up a given number of character or fields from the
specified file.
b) suppose we have a large database of student information from that we
Want only specific field say name (second field) and division (fifth field) then we can
write the cut command as
$ cut –c 2 ,7 empinfo
If we want to view the field 2 through 7 then
$ cut –f1 -d “ “ empinfo
21) passwd:
a) You can change your password whenever you are logged in by using the passwd
command as below.
$ passwd
This command asks you to enter old password to prove that you are the authorized
person and then after it ask for the new password.
22) pwd:
Pwd stands for ‘present working directory’. When you write pwd on prompt it will display
current working directory.
25) rm:
This command removes the given file or files supplied to it.
a) $ rm -i file1
where –i is a switch , removes file interactively; means you are asked
for confirmation before deleting the file.
b) $ rm -r dir1
This command recursively removes all content of dir1 and also dir1
itself
26) cp:
This command is used to copy a file.
a) $ cp file1 file2
This will copy file1 in to file2. if file2 does not exit then it will be created.
27) mv:
This command is used to rename the file
a) $ mv file1 file2
This command renames the file1 to file2
28) cmp:
The cmp utilities compares two files of any type and writes the result to the
Standard o/p
29) diff
To insert new text esc + i ( You have to press 'escape' key then 'i')
To save file esc + : + w (Press 'escape' key then 'colon' and finally
'w')
To save file with file name (save as) esc + : + w "filename"
To quit the vi editor esc + : + q
To quit without saving esc + : + q!
o save and quit vi editor esc + : + wq
To search for specified word in forward esc + /word (Press 'escape' key, type /word-to-find, for
direction e.g. to find word 'shri', type as/shri)
To continue with search n
To search for specified word in backward esc + ?word (Press 'escape' key, type word-to-find)
direction
To copy the line where cursor is located esc + yy
To paste the text just deleted or copied at the esc + p
cursor
To delete entire line where cursor is located esc + dd
To delete word from cursor position esc + dw
To Find all occurrence of given word and esc + :$s/word-to-find/word-to-replace/g
Replace them globally without confirmation
For. e.g. :$s/mumbai/pune/g
AIM: Implementation of Shell’s Scripts using basic UNIX commands and control statements
1. Write shell script to take cost price and selling price of an item is input through the keyboard,
and determine whether the seller has made profit or incurred loss.
Program:-
Program:-
Output:-
12 is a largest number.
3. Write a script to enter any year through the keyboard and to determine whether the year is leap
year or not.
Program:-
Output:-
echo “Menu:1. Present working directory.\n2. Calendar.\n3. List of user who have currently logged
in.\n4. Exit”
read choice
case $choice in
1) pwd ;;
2) cal ;;
3) who –Hu ;;
4) exit ;;
*) echo “Invalid Choice.”
esac
Output:-
Manu:
April 2014
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
5. Write a script to accept number and perform addition, subtraction, multiplication & division.
Program:-
Output:-
Menu:
1.Addition
2.Substaction
3.Multiplication
4. Division
Enter a & b :
10
20
Program:-
Output:-
Enter string:
abc
It is a directory.
7. Write a script to accept file name & display last modification time if file exists otherwise display
appropriate massage.
Program:-
Program:-
fi
Output:-
9. Write a script to display the date, time & a welcome massage (like Good Morning etc.) the time
should be displayed with ‘a.m’ or ‘p.m’ & not in 24 hours notation.
Program:-
Output:-
Current Time:
07:36:32
Good Morning
10. Write a script to display the directory in the descending order of the size of each file.
Program:-
Output:-
11. Write a script to make following file and directory management operation.
1) Display current directory
2) List directory
3) Make directory
4) Change directory
5) Copy directory
6) Rename a file
7) Delete a file
8) Edit a file
Program:-
echo “Manu:\n1. Display current directory\n2. List directory\n3. Make directory\n4. Change directory\n5.
Copy directory\n6. Rename a file\n7. Delete a file and Edit a file”
echo “Enter your choice: ”
read choice
if [ $choice –eq 1 ];
then
echo “Current directory is ”
pwd
elif [ $choice –eq 2 ];
then
echo “List of directory:”
ls
elif [ $choice –eq 3 ];
then
echo “Enter new directory name to create:”
read dir
mkdir $dir
echo “$dir directory is created successfully.”
elif [ $choice –eq 4 ];
then
echo “Enter directory name to change: “
read cdir
cd $cdir
elif [ $choice –eq 5 ];
then
echo “Enter file name to copy:”
read file
echo “Enter directory name to which file to be copied:”
read dir1
cp $file $dir1
elif [ $choice –eq 6 ];
then
echo “Enter file name to rename: ”
read rname
echo “Enter new name:”
read nname
mv $rname $nname
elif [ $choice –eq 7 ]
then
echo “Enter file name to delete:”
read dfile
rm $dfile
elif [ $choice –eq 8 ];
then
echo “Enter file name to edit:”
read efile
echo “Enter new data to write:”
read newdata
echo $newdata > $efile
fi
Output:-
Menu:
12. Write a script which reads a text file & output the following count of character , words & lines
Program :-
Enter the file name: abc.sh Total character: 243 Total words: 67 Total line: 22
PRACTICAL – 3
1. Write a script for accept a five-digit number through keyword, then reverse this five-digit
number.
Program:-
Output:-
Enter five-digit number:
12345
Program:-
Output:-
Program:
$ cat for1.sh
i=0
for day in Mon Tue Wed Thu Fri
do
echo "Weekday $((i=i+1)) : $day"
done
Output:
$ ./for1.sh
Weekday 1 : Mon
Weekday 2 : Tue
Weekday 3 : Wed
Weekday 4 : Thu
Weekday 5 : Fri
4. Write a script to fetch the data from file & display data.
Program:-
String Length
$ cat len.sh
#! /bin/bash
var="Welcome to the geekstuff"
echo ${#var}
Output:
x$./len.sh
24
Substring of String
Program:
$ cat substr.sh
#! /bin/bash
Output:
$ ./substr.sh
geekstuff
geek
String Concatenate
Program:
a='hello'
b='world'
c=$a$b
echo $c
Output
> helloworld
String Compare
Program:
Sourcesystem="ABC"
if [ 'XYZ' -eq "$Sourcesystem" ]; then
echo "Sourcesystem Matched"
else
echo "Sourcesystem is NOT Matched $Sourcesystem"
fi;
Output:
Command-line Arguments:
Program:
$ vi myscript
#!/bin/bash
Output:
Algorithm
1. Define an array of structure process with members pid, btime, wtime & ttime.
2. Get length of the ready queue, i.e., number of process (say n)
3. Obtain btime for each process.
4. The wtime for first process is 0.
5. Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
6. Compute average waiting time awat and average turnaround time atur
7. Display the btime, ttime and wtime for each process.
8. Display GANTT chart for the above scheduling
9. Display awat time and atur
10. Stop
Result
Thus waiting time & turnaround time for processes based on FCFS
scheduling was computed and the average waiting time was determined.
Program
#include <stdio.h>
struct process
{
int pid;
int
btime;
int
wtime;
int
ttime;
} p[10];
main()
{
int i,j,k,n,ttur,twat;
float awat,atur;
printf("Enter no. of process : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) :
",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n");
printf("|");
for(i=0; i<n;
i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime;
j++)
printf("
");
printf("|");
}
printf("\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n");
printf("0");
for(i=0; i<n;
i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
Output
$ gcc fcfs.c
$./a.out
Enter no. of process : 4
Burst time for process P1 (in ms) : 10
Burst time for process P2 (in ms) : 4
Burst time for process P3 (in ms) : 11
Burst time for process P4 (in ms) : 6
FCFS
Scheduling
--------------------------
-- Process B-Time T-Time
W-Time
----------------------------
P1 10 10
0
P2 4 14
10
P3 11 25
14
P4 6 31
25
----------------------------
GANTT Chart
----------------------------------------
| P1 | P2 | P3 | P4 |
----------------------------------------
0 10 14 25 31
1. Define an array of structure process with members pid, btime, wtime & ttime.
2. Get length of the ready queue, i.e., number of process (say n)
3. Obtain btime for each process.
4. Sort the processes according to their btime in ascending order.
a. If two process have same btime, then FCFS is used to resolve the tie.
5. The wtime for first process is 0.
6. Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
7. Compute average waiting time awat and average turn around time atur.
8. Display btime, ttime and wtime for each process.
9. Display GANTT chart for the above scheduling
10. Display awat and atur
11. Stop
Result
Thus waiting time & turnaround time for processes based on SJF scheduling
was computed and the average waiting time was determined.
Program
#include <stdio.h>
struct process
{
int pid; int
btime; int
wtime; int
ttime;
} p[10], temp;
main()
{
int i,j,k,n,ttur,twat;
float awat,atur;
}
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n|");
for(i=0; i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" "); printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n0");
for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
Output
$ gcc sjf.c
$./a.out
Enter no. of process : 5
Burst time for process P1 (in ms) : 10
Burst time for process P2 (in ms) : 6
Burst time for process P3 (in ms) : 5
Burst time for process P4 (in ms) : 6
Burst time for process P5 (in ms) : 9
SJF Scheduling
----------------------------
Process B-Time T-Time W-Time
----------------------------
P3 5 5 0
P2 6 11 5
P4 6 17 11
P5 9 26 17
P1 10 36 26
----------------------------
GANTT Chart
-----------------------------------------------
| P3 | P2 | P4 | P5 | P1 |
-----------------------------------------------
0 5 11 17 26 36
Result
Thus waiting time and turnaround time for processes based on Round robin
scheduling was computed and the average waiting time was determined.
Program
#include <stdio.h>
main()
{
int i,x=-1,k[10],m=0,n,t,s=0;
int a[50],temp,b[50],p[10],bur[10],bur1[10];
int wat[10],tur[10],ttur=0,twat=0,j=0;
float awat,atur;
printf("\nGANTT Chart\n");
for(i=0; i<m; i++)
printf("--------");
printf("\n");
a[0] = 0;
while(j < m)
{
if(x == n-1)
x = 0;
else
x++;
if(bur[x] >= t)
{
bur[x] -= t;
a[j+1] = a[j] + t;
if(b[x] == 1)
{
p[s] = x;
k[s] = a[j+1];
s++;
}
j++;
b[x] -= 1;
printf(" P%d |", x+1);
}
else if(bur[x] != 0)
{
a[j+1] = a[j] + bur[x];
bur[x] = 0;
if(b[x] == 1)
{
p[s] = x;
k[s] = a[j+1];
s++;
}
j++;
b[x] -= 1;
printf(" P%d |",x+1);
}
}
printf("\n");
for(i=0;i<m;i++)
printf("--------");
printf("\n");
temp = k[i];
k[i] =k[j];
k[j] = temp;
}
}
}
printf("\n\n");
for(i=0; i<30; i++)
printf("-");
printf("\nProcess\tBurst\tTrnd\tWait\n");
for(i=0; i<30; i++)
printf("-");
for (i=0; i<n; i++)
printf("\nP%-4d\t%4d\t%4d\t%4d", p[i]+1, bur1[i],
tur[i],wat[i]);
printf("\n");
for(i=0; i<30; i++)
printf("-");
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n\nAverage waiting time : %.2f ms", awat);
printf("\nAverage turn around time : %.2f ms\n", atur);
}
Output
$ gcc rr.c
$ ./a.out
Enter no. of process : 5
Burst time for process P1 : 10
Burst time for process P2 : 29
Burst time for process P3 : 3
Burst time for process P4 : 7
Burst time for process P5 : 12
Enter the time slice (in ms) : 10
GANTT Chart
--------------------------------------------------------------
P1 | P2 | P3 | P4 | P5 | P2 | P5 | P2 |
--------------------------------------------------------------
0 10 20 23 30 40 50 52 61
------------------------------
Process Burst Trnd Wait
------------------------------
P1 10 10 0
P2 29 61 32
P3 3 23 20
P4 7 30 23
P5 12 52 40
------------------------------
1. Define an array of structure process with members pid, btime, pri, wtime & ttime.
2. Get length of the ready queue, i.e., number of process (say n)
3. Obtain btime and pri for each process.
4. Sort the processes according to their pri in ascending order.
a. If two process have same pri, then FCFS is used to resolve the tie.
5. The wtime for first process is 0.
6. Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
7. Compute average waiting time awat and average turn around time atur
8. Display the btime, pri, ttime and wtime for each process.
9. Display GANTT chart for the above scheduling
10. Display awat and atur
11. Stop
Result
Thus waiting time & turnaround time for processes based on Priority scheduling
was computed and the average waiting time was determined.
Program
#include <stdio.h>
struct process
{
int pid; int
btime; int
pri; int
wtime; int
ttime;
} p[10], temp;
main()
{
int i,j,k,n,ttur,twat;
float awat,atur;
}
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n|");
for(i=0; i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" "); printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n0");
for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
Output
$ gcc pri.c
$ ./a.out
Enter no. of process : 5
Burst time for process P1 (in ms) : 10
Priority for process P1 : 3
Burst time for process P2 (in ms) : 7
Priority for process P2 : 1
Burst time for process P3 (in ms) : 6
Priority for process P3 : 3
Burst time for process P4 (in ms) : 13
Priority for process P4 : 4
Burst time for process P5 (in ms) : 5
Priority for process P5 : 2
Priority Scheduling
--------------------------------------
Process B-Time Priority T-Time W-Time
--------------------------------------
P2 7 1 7 0
P5 5 2 12 7
P1 10 3 22 12
P3 6 3 28 22
P4 13 4 41 28
--------------------------------------
GANTT Chart
----------------------------------------------------
| P2 | P5 | P1 | P3 | P4 |
----------------------------------------------------
0 7 12 22 28 41
1. Declare structures hole and process to hold information about set of holes and
processes respectively.
2. Get number of holes, say nh.
3. Get the size of each hole
4. Get number of processes, say np.
5. Get the memory requirements for each process.
6. Allocate processes to holes, by examining each hole as follows:
a. If hole size > process size then
i. Mark process as allocated to that hole. ii. Decrement
hole size by process size.
b. Otherwise check the next from the set of hole
7. Print the list of process and their allocated holes or unallocated status.
8. Print the list of holes, their actual and current availability.
9. Stop
Result
Thus processes were allocated memory using first fit method.
Program
#include <stdio.h>
struct process
{
int size;
int flag;
int holeid;
} p[10];
struct hole
{
int size;
int actual;
} h[10];
main()
{
int i, np, nh, j;
printf("\n\tFirst fit\n");
printf("\nProcess\tPSize\tHole");
for(i=0; i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", i, h[i].actual, h[i].size);
printf("\n");
}
Output
$ gcc ffit.c
$ ./a.out
Enter the number of Holes : 5
Enter size for hole H0 : 100
Enter size for hole H1 : 500
Enter size for hole H2 : 200
Enter size for hole H3 : 300
Enter size for hole H4 : 600
First fit
Process PSize Hole
P0 212 H1
P1 417 H4
P2 112 H1
P3 426 Not allocated
1.Declare structures hole and process to hold information about set of holes and
processes respectively.
2. Get number of holes, say nh.
3. Get the size of each hole
4. Get number of processes, say np.
5. Get the memory requirements for each process.
6. Allocate processes to holes, by examining each hole as follows:
a. Sort the holes according to their sizes in ascending order b. If
hole size > process size then
i. Mark process as allocated to that hole. ii. Decrement
hole size by process size.
c. Otherwise check the next from the set of sorted hole
7. Print the list of process and their allocated holes or unallocated status.
8. Print the list of holes, their actual and current availability.
9. Stop
Result
Thus processes were allocated memory using best fit method.
Program
#include <stdio.h>
struct process
{
int size; int
flag; int
holeid;
} p[10];
struct hole
{
int hid; int
size; int
actual;
} h[10];
main()
{
int i, np, nh, j;
void bsort(struct hole[], int);
printf("\n\tBest fit\n");
printf("\nProcess\tPSize\tHole");
for(i=0; i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", h[i].hid, h[i].actual,
h[i].size);
printf("\n");
}
void bsort(struct hole bh[], int n)
{
struct hole temp;
int i,j;
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(bh[i].size > bh[j].size)
{
temp = bh[i]; bh[i] =
bh[j]; bh[j] = temp;
}
}
}
}
Output
$ gcc bfit.c
$ ./a.out
Enter the number of Holes : 5
Enter size for hole H0 : 100
Enter size for hole H1 : 500
Enter size for hole H2 : 200
Enter size for hole H3 : 300
Enter size for hole H4 : 600
Best fit
Result
Thus page replacement was implemented using FIFO algorithm.
Program
#include <stdio.h>
main()
{
int i,j,l,rs[50],frame[10],nf,k,avail,count=0;
Output
$ gcc fifopr.c
$ ./a.out
Enter length of ref. string : 20
Enter reference string :
1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6
Enter number of frames : 5
Result
Thus page replacement was implemented using LRU algorithm.
Program
#include <stdio.h>
main()
{
int i,j,len,rs[50],frame[10],nf,k,avail,count=0;
int access[10], freq=0, dm;
Output
$ gcc lrupr.c
$ ./a.out
Length of Reference string : 20
Enter reference string :
1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6
Enter no. of frames : 5
#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
lg[i]=0;
for(i=0;i<frsize;i++)
{
for(k=j+1;k<12;k++)
{
if(fr[i]==p[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<frsize;i++)
{
if(lg[i]==0)
{
index=i;
found=1;
break;
}
}
if(found==0)
{
max=lg[0];
index=0;
for(i=1;i<frsize;i++)
{
if(max<lg[i])
{
max=lg[i];
index=i;
}
}
}
fr[index]=p[j];
pf++;
}
display();
}
printf("\n no of page faults:%d",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}
Output:
OUTPUT :
2 -1 -1
2 3 -1
2 3 -1
231
235
235
435
435
435
235
235
235
no of page faults : 3