0% found this document useful (0 votes)
22 views

Os Lab Expt.s

The document discusses various basic and advanced UNIX commands. It provides examples of commands like date, pwd, ls, cat, grep, sort, echo, read and expr. It also discusses if/else and switch case conditional statements in shell scripting. Several sample programs are provided to demonstrate the use of these commands and conditional statements.

Uploaded by

rkumar25022000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Os Lab Expt.s

The document discusses various basic and advanced UNIX commands. It provides examples of commands like date, pwd, ls, cat, grep, sort, echo, read and expr. It also discusses if/else and switch case conditional statements in shell scripting. Several sample programs are provided to demonstrate the use of these commands and conditional statements.

Uploaded by

rkumar25022000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Experiment No - 1

AIM: To Study the Basic UNIX Commands.

1. date : This command is used to display the system time.

ex: $date

2: pwd: It is used to display the present working directory.

ex: $pwd

3. man: It returns the manual page of any command.

ex $man pwd

4: who: It print information about users who are currently logged in.
ex: $who

5. whoami: It prints the user name associated with the current


effective user id.

ex: $whoami

6. cal: To Dispaly the calender of the any year.

ex: $cal

ex1: $cal 2000

ex2: $ cal 4 2009

ex3: $cal -3

ex4: $cal 1752 (see month of september, 11 days are missing)

7: clear: It is used to clear the screen.


ex: $clear

8: mkdir: This command is used to create a new directory or sub


directory.

ex: $mkdir <directory name>

9: cd: This command is used to change directory.

ex: $cd <directory name>

10: cat: This command is used to create a new file and to append or
add the contents and display the contents.

1.Create a New File


$cat>file.txt
[ type something ]

then press ctrl+d (to save the file)

2. $cat file.txt (to display the contents)

3. $cat>>file.txt (to append )

11. cp: This command is used to copy content of the existing file to
new file or existing file and copy the file one location to another
location.(i.e. Folder to Folder)

ex1: cp file.txt f1.txt

ex2: cp file.txt /home/student/skp(directory name)

ex3: cp /home/student/18cse100/filename /home/student/skp

12. ls: To display the all files and derectory present in the present
working dierctory.

ex:$ls
1. ls -l: to display long listing files and directory

ex: $ls -l

13. mv: This command is used for rename the file and directory.

ex1: $mv file.txt f2.txt

ex2: $mv skp giet

14. cd .. :This command is used to come out from present working


directory.

ex: $cd ..

15. rm: It is used to delete an existing file.

ex: $rm file1.txt

16. rmdir: This command is used to delete an empty directory.


(Note: Before deleting a directory, make sure that it should empty and
not in use)

ex: $rmdir skp

Experiment No - 2
AIM: To study about Advanced UNIX Commands.
1. touch: The touch command is used to create, change and modify
timestamps of a file.

ex1: $touch file1.txt file2.txt file3txt file4.txt

ex2: $ls -l

ex3: $cat>>file2.txt

ex4: $ls -l

2. chmod: The chmod command is used to change the access mode of a


file.

Syntax:$chmod[reference][operator][mode] file

The references are used to distinguish the users to whom the


permissions apply. The references are represented by one or more of
the following letters:

u=rwx,g=rx,o=r myfile

Reference Class Description


u owner file's owner

g group users who are members of


the file's group

o others users who are neither the


file's owner nor members of
the file's group.

There are three basic modes which correspond to the basic permissions:
r:Permission to read the file.
w:Permission to write the file.
x:Permission to execute the file

The permissions for the user, group, and others, in that order. Each
digit is a combination of the numbers 4, 2, 1, and 0 :

4 stands for "read",


2 stands for "write",
1 stands for "execute", and
0 stands for "no permission."

So 7 is the combination of permissions 4+2+1 (read, write, and


execute), 5 is 4+0+1 (read, no write, and execute),and 4 is 4+0+0
(read, no write, and no execute).

U G O
-rw-rw-r-- 1 master master 21 Aug 3 09:32 file2.txt
ex1: $chmod 777 file2.txt

ex: $ll (after execute this command see the file, check the status of
the file)

ex2: $chmod 642 file2.txt

ex: $ll

ex3: $chmod 555 file2.txt

ex: $ll

ex4: $chmod 222 file2.txt

ex: $ll

ex5: $cat file2.txt

ex6: $chmod 666 file2.txt

ex: $ll

ex7: $cat file2.txt

3. wc: This command is used to count the no of words, lines and


characters in a existing file.

ex: $wc file2.txt

ex1: $ll | cat>file100.txt

ex2: $ cat file100.txt

ex3: wc file100.txt

4. head: This command is used to display the first part (by default 10
lines) of the file.

ex: $head file100.txt

5. tail:This command is used to display the last part (by default 10


lines) of the file.

ex: $ tail file100.txt

6. pipe(|): This command is used to concanate two command. i.e. Output


of first command is input of the second command.

ex1: $ll | cat>file23.txt


ex2: $cat file2.txt | wc

7. grep: The grep command which stands for “global regular


expression print,― processes text line by line and prints any lines
which match a specified pattern. The grep command is used to search
text or searches the given file for lines containing a match to the
given strings or words.

ex1: $grep <pattern> file2.txt


[ pattern means any word or string ]
ex2: $grep (character) file2.txt

ex3: $grep (word) file2.txt

ex4: $grep string file2.txt

8. sort: Sort command is used for printing lines of input text files
and concatenation of all files in sorted order. Sort command takes
blank space as field separator and entire Input file as sort key.

ex: cat>file200.txt
srfhrf
hf
bg
drf
f
b
cv
df
h
bcvf
6
8
5
9
1
3
2
4
7

pres ctrl + d

ex: $sort file200.txt

Experiment No - 3
AIM: To study about echo, read and expr UNIX commands and Compilation
of Shell Programs and practice sample shell programs.

1. echo:echo command in linux is used to display line of text/string


that are passed as an argument . This is a built in command that is
mostly used in shell scripts and batch files to output status text to
the screen or a file.

Syntax: echo [string]

ex1: $echo "Welcome to GIET"

ex2: $echo Welcome to OS Lab

2. read: The command read in the Linux is used to read the input from
the keyboard.

ex1: $read a
input some value 234
ex2: $read b
input some text or string GIET
ex3: $echo $a

ex4: $echo $b

3. expr: The expr command in Unix evaluates a given expression and


displays its corresponding output. It is used for:

-->Basic operations like addition, subtraction, multiplication,


division, and modulus on integers.

-->Evaluating regular expressions, string operations like substring,


length of strings etc.

ex1: For Addition


$expr 5 + 3

ex2: For Substraction


$expr 15 - 8

ex3: For Multiplication


$expr 10 \* 7

ex4: For Division


$expr 24 / 6

Shell Scripting or Shell Programs


gedit add.sh

#Program For Addition of two number


echo "Enter two numbers"

read x

read y

sum=`expr $x + $y`

echo "Sum = $sum"

Note: expr is an external program used by Bourne shell. It uses expr


external program with the help of backtick. The backtick(`) is
actually called command substitution.

To run or compile the shell program


sh add.sh(file name)

ex6:To find the length of the string

echo "Enter the String"


read x

len=`expr length $x`

echo "Length of the String is $len"

ex7: To find the index/position of character in a string

a=giet

b=`expr index $a e`

echo "Index Position of a String is: $b"

ex8: To find substring of string:

a=hello

b=`expr substr $a 2 3`

[Note:where 2 is position and 3 is length, command is to get substring


from position 2 of length 3 characters]

echo "Substring of a String is: $b"

Questions:
1. Write a shell program to input two numbers and display the result
of all arithmetic operators.

2. Write a shell program to swap between two numbers using and without
using third variable.
x=`expr $x + $y`
y=`expr $x - $y`
x=`expr $x - $y`

3. Write a shell program to input radius and find the area of circle
and perimeter of a circle.
r=5
pi=3.14
AC=`echo $pi \* $r \* $r |bc`
3.14 * 5 * 5

4. Write a shell program to input principle amount, no of terms, rate


of interest. Calculate simple interest.

5. Write a shell program to input basic salary. find the TA=10% of BS,
DA=15% of BS, HRA=20% of BS.Find total salary.

6. Write a shell program to create a new directory(UR name). In that


directory create a new file(newfile.txt).Copy the contents of the file
to new file(new file name). Display the no. of lines, words &
characters in given and also display the contents of the file. at last
display the list of files present in the present working directory.

pi=3.14
area=`echo $pi \* $r \* $r |bc`

x=`expr $x + $y`
y=`expr $x - $y`
x=`expr $x - $y`

echo "Enter the basic salary"


read BS
TA=`echo $BS \* 0.1 | bc`
DA=`echo $BS \* 1.5 | bc`
HRA=`echo $BS \* 0.2 | bc`

TS=`echo $BS + $TA + $DA + $HRA |bc`

echo "Total Salary := $TS"

Experiment No - 4
AIM: Shell Scripting or Programs using if..else, nested if..else and
switch case.
Operators
-eq (==)
-lt (<)
-le (<=)
-gt (>)
-ge (>=)
-ne (!=)
-a (and)
-o (or)

Syntax:if..else
if [ condition ]
then
statement
else
statement
fi

syntax:nested if else ladder

if [ condition ]
then
statement
elif [ condition ]
then
statement
elif [ condition ]
then
statement
else
statement
fi
Q1. Write a shell program to check the given number is even or odd.
if [ `expr $n % 2` -eq 0 ]

Q2. Write a shell program to read a number and check whether the
number is +ve or -ve.

Q3. Write a shell program to find the greatest among three numbers.

Q4. Write a shell program to read a character and check the character
is vowel or consonant using CASE.

Q5.Write a program to input a digit within (0 to 6), display week day


as per the given input. Ex: 0 – sunday, 1 – monday, 2-tuesday etc.
Q6. Write a program to input a day "sunday/monday/tuesday....". Then
display a suitable message, for example: sunday - "it is holiday",
monday - "it is working day
, tuesday - "it is busy day" etc

Q7. write a program to input an arithmetic operator(+, -, *, /, %).


input two numbers and display the result according to the given
operator.
syntax for switch case:
case $variable-name in
pattern1)
command1
....
commandN
;;
pattern2)
command1
....
commandN
;;
*) default
esac

echo "Enter any character: "


read ch
case $ch in
"a") echo "It is a vowel.";;
"e") echo "It is a vowel.";;
"i") echo "It is a vowel.";;
"o") echo "It is a vowel.";;
"u") echo "It is a vowel.";;
*) echo "It is not a vowel."
esac

echo "Enter a character"


read a
case $a in
a) echo "It is a vowel";;
e) echo "It is a vowel";;
i) echo "It is a vowel";;
o) echo "It is a vowel";;
u) echo "It is a vowel";;
*) echo "Not a vowel";;
esac

Experiment No - 5
AIM: Shell Scripting on while loop.

while loop syntax:

while [ condition ]
do
commands
statements
done

Q1. Write a shell program to print the first 10 natural numbers.


i=i+1 (Incriment in C programming)
i=`expr $i + 1` (Incriment Shell Program)

Q2. Write a shell program to find the sum of odd and even numbers upto
20.

echo "Enter the Range"


read n
EvenSum=0
OddSum=0
i=1
while [ $i -le $n ]
do
if [ `expr $i % 2` -eq 0 ]
then
EvenSum=`expr $EvenSum + $i`
else
OddSum=`expr $OddSum + $i`
fi
i=`expr $i + 1`
done
echo "Sum of Even Numbers is $EvenSum"
echo "Sum of Odd Numbers is $OddSum"

Q3. Write a shell program to find the factorial of a given number.

echo "enter the number"


read n
fact=1
while [ $n -gt 0 ]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done
echo "factorial of number is $fact"

Q4. Write a shell program to print the fibonacci series upto nth term.
echo "Enter the number"
read n
i=1
while [ $i -le $n ]
do
c=0
j=1
while [ $j -le $i ]
do
if [ `expr $i % $j` -eq 0 ] then
c=`expr $c + 1`
fi
j=`expr $j + 1`
done
if [ $c -eq 2 ] then
echo "$i"
fi
i=`expr $i + 1`
done

Q5. Write a shell program to display the prime numbers between 1 to


50.

Q6. Write a shell program to find the sum of digits of a given number.

Q7. Write a shell program to print Floyd's Triangle.


1
2 3
4 5 6
7 8 9 10

Experiment No - 6
AIM:- To Study about fork() system call.

Fork system call use for creates a new process, which is called child
process, which runs concurrently with process (which process called
system call fork) and this process is called parent process. After a
new child process 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 (-ve): creation of a child process was unsuccessful.


Zero (0): Returned to the newly created child process.
Positive value (+ve): Returned to parent or caller. The value contains
process ID of newly created child process.

Ex1: Calculate number of times hello is printed.


#include <stdio.h>
#include <sys/types.h> /* needed to use
pid_t, etc.*/
#include <unistd.h> /*LINUX constants and functions (fork() ,etc.) */
int main()
{
fork();

printf("Hello world!\n");
return 0;
}

Ex:2
----
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}
Number of times hello printed is equal to number of process created.
Total Number of Processes = 2 power n, where n is number of fork
system calls. So here n = 3, 2 power 3 =8
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
if (fork() == 0)
printf("Hello from Child!\n");

else
printf("Hello from Parent!\n");
return 0;
}

Here, two outputs are possible because the parent process and child
process are running concurrently. So we don't know if OS first give
control to which process a parent process or a child process.
Important: Parent process and child process are running the same
program, but it does not mean they are identical. OS allocate
different data and state for these two processes and also control the
flow of these processes can be different.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
int x = 1;

if (fork() == 0)
printf("Child has x = %d\n", ++x);
else
printf("Parent has x = %d\n", --x);
return 0;
}
===========================================
#include <stdio.h>
#include <unistd.h>
int main()
{
int n=0;
if(fork()==0)
{
printf("Child! Seq=%d\n", ++n);
}
else
{
printf("Parent! Seq=%d\n", ++n);
}
printf("Both! n=%d\n", ++n);
return 0;}
==============================================
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int pid;
pid = fork();
if (pid < 0)
{
printf("Fork failed\n");
exit(-1);
}
else if (pid == 0)
{
printf("Child Process,return from fork=%d \n",
pid);
}
else
{
printf("Parent Process return from fork=%d \n",
pid);

}
}

Experiment No - 7
AIM:Simulation of FCFS and SJF CPU Scheduling Algorithm.
FCFS C Program

No of Process : 3
Process BursTime WaitingTime TurnAtime
P[1] 24 0 24
P[2] 3 24 27
P[3] 3 27 30

#include<stdio.h>
void main()
{
int n,bt[5],wt[5],tat[5],tot_tat=0;
int avg_wt=0,avg_tat=0,i,j,tot_wt=0;
printf("Enter the no of process:\n");
scanf("%d",&n);
printf("Enter the Burst Time:\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
{
wt[i]=wt[i]+bt[j];
}
}
printf("\nProcess\tBurstTime\tWaitingTime
\tTurnAtime\n");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
tot_wt=tot_wt+wt[i];
tot_tat=tot_tat+tat[i];

printf("\nP[%d]\t%d\t\t%d\t\t%d",i+1,bt[i],
wt[i],tat[i]);
}
avg_wt=tot_wt/i;
avg_tat=tot_tat/i;

printf("\nAverage Waiting Time %d",avg_wt);


printf("\nAverage Turn_A_Time %d",avg_tat);
}

SJF C Program
#include<stdio.h>
void main()
{
int n,bt[5],wt[5],tat[5],tot_tat=0;
int avg_wt=0,avg_tat=0,i,j,tot_wt=0;
int p[5],t,pos;
printf("Enter the no of process:\n");
scanf("%d",&n);
printf("Enter the Burst Time:\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++){
pos=i;
for(j=i+1;j<n;j++){
if(bt[j]<bt[pos]){
pos=j;}
}
t=bt[i];
bt[i]=bt[pos];
bt[pos]=t;

t=p[i];
p[i]=p[pos];
p[pos]=t;
}

wt[0]=0;
for(i=0;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
{
wt[i]=wt[i]+bt[j];
}
}
printf("\nProcess\tBurstTime\tWaitingTime
\tTurnAtime\n");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
tot_wt=tot_wt+wt[i];
tot_tat=tot_tat+tat[i];

printf("\nP[%d]\t%d\t\t%d\t\t%d",p[i],bt[i],
wt[i],tat[i]);
}
avg_wt=tot_wt/i;
avg_tat=tot_tat/i;
printf("\nAverage Waiting Time %d",avg_wt);
printf("\nAverage Turn_A_Time %d",avg_tat);
}

Experiment No - 8
AIM:Simulation of Round Robin and Priority CPU Scheduling Algorithm.

Priority.c
----------
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],p[20];
int i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
}

for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process is zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround


Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t
%d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}

Round Robin
------------
#include<stdio.h>
int main()
{

int c,j,n,time,r,flag=0,tq;
int wt=0,tat=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
r=n;
printf("Enter Arrival Time and Burst Time");
for(c=0;c<n;c++)
{
scanf("%d",&at[c]);
scanf("%d",&bt[c]);
rt[c]=bt[c];
}
printf("Enter Time Quantum:");
scanf("%d",&tq);
printf("\n\nProcess\t|Turnaround Time|
Waiting Time\n\n");
for(time=0,c=0;r!=0;)
{
if(rt[c]<=tq && rt[c]>0)
{
time+=rt[c];
rt[c]=0;
flag=1;
}
else if(rt[c]>0)
{
rt[c]-=tq;
time+=tq;
}
if(rt[c]==0 && flag==1)
{
r--;
printf("P[%d]\t|\t%d\t|\t%d\n",c+1,
time-at[c],time-at[c]-bt[c]);
wt+=time-at[c]-bt[c];
tat+=time-at[c];
flag=0;
}
if(c==n-1)
c=0;
else if(at[c+1]<=time)
c++;
else
c=0;
}
printf("Average Waiting Time=%f",
wait_time*1.0/n);
printf("Average Turnaround Time=%f",
turnaround_time*1.0/n);
return 0;
}
Experiment No - 8
AIM:Simulation of Banker's Algorithm.

#include <stdio.h>
void main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10]; int
completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;

printf("Enter the no of processes : ");


scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;

printf("\n\nEnter the no of resources : ");


scanf("%d", &r);

printf("\n\nEnter the Max Matrix for each process : ");


for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}

printf("\n\nEnter the allocation for each process : ");


for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}

printf("\n\nEnter the Available Resources : ");


for(i = 0; i < r; i++)
scanf("%d", &avail[i]);

for(i = 0; i < p; i++)

for(j = 0; j < r; j++)


need[i][j] = Max[i][j] - alloc[i][j];

do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}

if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);

if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");}
OUTPUT
Enter the no of processes : 5
Enter the no of resources : 3
Enter the Max Matrix for each process :
For process 1 : 7 5 3
For process 2 : 3 2 2
For process 3 : 7 0 2
For process 4 : 2 2 2
For process 5 : 4 3 3
Enter the allocation for each process :
For process 1 : 0 1 0
For process 2 : 2 0 0
For process 3 : 3 0 2
For process 4 : 2 1 1
For process 5 : 0 0 2
Enter the Available Resources : 3 3 2
Max matrix: Allocation matrix:
7 5 3 0 1 0
3 2 2 2 0 0
7 0 2 3 0 2
2 2 2 2 1 1
4 3 3 0 0 2

Process 2 runs to completion!


Max matrix: Allocation matrix:
7 5 3 0 1 0
0 0 0 0 0 0
7 0 2 3 0 2
2 2 2 2 1 1
4 3 3 0 0 2

Process 3 runs to completion!


Max matrix: Allocation matrix:
7 5 3 0 1 0
0 0 0 0 0 0
0 0 0 0 0 0
2 2 2 2 1 1
4 3 3 0 0 2

Process 4 runs to completion!


Max matrix: Allocation matrix:
7 5 3 0 1 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
4 3 3 0 0 2

Process 1 runs to completion!


Max matrix: Allocation matrix:
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
4 3 3 0 0 2

Process 5 runs to completion!


The system is in a safe state!!

Experiment No - 10
AIM: Implimentation of FIFO and LRU Page replacement Algorithm.
FIFO
ALGORITHM
step 1. Start the process

step 2. Declare the size with respect to page length

step 3. Check the need of replacement from the page to memory

step 4. Check the need of replacement from old page to new page in
memory

step 5. Forma queue to hold all pages

step 6. Insert the page require memory into the queue

step 7. Check for bad replacement and page fault

step 8. Get the number of processes to be inserted

step 9. Display the values

step 10. Stop the process

#include<stdio.h>
void main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
}

OUTPUT:

ENTER THE NUMBER OF PAGES: 20


ENTER THE PAGE NUMBER :7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
ENTER THE NUMBER OF FRAMES :3
ref string page frames
7 7 -1 -1
0 7 0 -1
1 7 0 1
2 2 0 1
0
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
3 4 2 3
0 0 2 3
3
2
1 0 1 3
2 0 1 2
0
1
7 7 1 2
0 7 0 2
1 7 0 1

Page Fault Is 15

LRU
#include<stdio.h>
void main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
printf("\nThe no of page faults is %d",c);
}

OUTPUT:

Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2

The no of page faults is 10

You might also like