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

OS LAB R20 (2)

The Operating System Lab Manual outlines the curriculum for a second-year computer science course at Dadi Institute of Engineering & Technology for the academic year 2022-23. It includes the institute's vision and mission, course objectives, outcomes, and a comprehensive list of experiments focused on Unix/Linux commands, C programming, and various operating system concepts. The manual aims to equip students with practical skills in operating systems and shell scripting, emphasizing both theoretical understanding and hands-on experience.

Uploaded by

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

OS LAB R20 (2)

The Operating System Lab Manual outlines the curriculum for a second-year computer science course at Dadi Institute of Engineering & Technology for the academic year 2022-23. It includes the institute's vision and mission, course objectives, outcomes, and a comprehensive list of experiments focused on Unix/Linux commands, C programming, and various operating system concepts. The manual aims to equip students with practical skills in operating systems and shell scripting, emphasizing both theoretical understanding and hands-on experience.

Uploaded by

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

OPERATING SYSTEM LAB MANUAL

Regulation : R20
Year/Semester : II/I
Academic Year : 2022-23
Branch : CSE

DADI INSTITUTE OF ENGINEERING & TECHNOLOGY


(Approved by A.I.C.T.E., New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NAAC Accredited Institute
Recognized under Section 2(f) & 12(B) of UGC Act 1956
An ISO 9001:2015, ISO 14001:2015 & ISO 45001:2018 Certified Institute.
NH-16, Anakapalle – 531002, Visakhapatnam, A.P.
Mobile: +91 9963981111, Website: www.diet.edu.in, E-mail: [email protected]
Institute Vision & Mission
Vision
To evolve into a premier technical institution ensuring academic excellence and promoting
innovational research.
Mission
 To impart high quality technical and professional education, to uplift the living standards
of the youth by focusing on employability, higher education and research
 To bridge the gap between industry and academia by introducing add on courses based on
industrial and academic needs
 To develop responsible citizens through disciplined career and acceptance of ethical
values
 To be a student centric Institute imbibing experiential, innovative and lifelong learning
skills addressing societal problems

Department Vision &; Mission


Vision
To generate competent software professionals to become part of the industry and research
organizations.
Mission
 To impart high quality professional training with an emphasis on basic principles of
computer science and engineering.
 To impart moral and ethical values, and interpersonal skills to the students.
 To empower the students with the required skills by adopting bridge courses to meet
industry requirements.
 To promote research based projects & activities in the emerging areas of technology
convergence.
Course Objectives:
To understand the design aspects of operating system
To study the process management concepts & Techniques
To study the storage management concepts
To familiarize students with the Linux environment
To learn the fundamentals of shell scripting/programming

Course Outcomes:
To use Unix utilities and perform basic shell control of the utilities
To use the Unix file system and file access control
To use of an operating system to develop software
Students will be able to use Linux environment efficiently
Solve problems using bash for shell scripting
OPERATING SYSTEM LAB

LIST OF EXPERIMENTS

1) a) Study of Unix/Linux general purpose utility command list: man,who,cat, cd, cp, ps, ls,
mv, rm, mkdir, rmdir, echo, more, date, time, kill, history, chmod, chown, finger, pwd, cal, logout,
shutdown.
b) Study of vi editor
c) Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system
d) Study of Unix/Linux file system (tree structure)
e) Study of .bashrc, /etc/bashrc and Environment variables.
2) Write a C program that makes a copy of a file using standard I/O, and system calls
3) Write a C program to emulate the UNIX ls –l command.
4) Write a C program that illustrates how to execute two commands concurrently with a command
pipe. Ex: - ls –l | sort
5) Simulate the following CPU scheduling algorithms: (a) Round Robin (b) SJF (c) FCFS (d)
Priority
6) Multiprogramming-Memory management-Implementation of fork (), wait (), exec() and exit (),
System calls
7) Simulate the following: a) Multiprogramming with a fixed number of tasks (MFT) b)
Multiprogramming with a variable number of tasks (MVT)
8) Simulate Bankers Algorithm for Dead Lock Avoidance
9) Simulate Bankers Algorithm for Dead Lock Prevention.
10) Simulate the following page replacement algorithms: a) FIFO b) LRU c) LFU
11) Simulate the following File allocation strategies (a) Sequenced (b) Indexed (c) Linked
12) Write a C program that illustrates two processes communicating using shared memory
13) Write a C program to simulate producer and consumer problem usingsemaphores
14) Write C program to create a thread using pthreads library and let it run its function.
15) Write a C program to illustrate concurrent execution of threads using pthreads library

ADDITIONAL EXPERIMENTS

1) Simulate Scan Disk Scheduling algorithm


2) Simulate C-Scan Disk Scheduling algorithm
1) a) Study of Unix/Linux general purpose utility command list: man, who, cat, cd, cp, ps, ls,
mv, rm, mkdir, rmdir, echo, more, date, time, kill, history, chmod, chown, finger, pwd, Cal,
logout, shutdown.
1) The man command:
man - an interface to the on-line reference manuals
man is the system's manual pager. Each page argument given to man is normally the name
of a program, utility or function.
Example: $man pwd
Output:
PWD
NAM
E
pwd – print working directory name
SYNOPSIS
pwd
DESCRIPTION
pwd prints the pathname of the working (current) directory
2) The who command:
who - show who is logged on
Print information about users who are currently logged in.
Example:
$who
root console july 2 10:30
aaa tty01 july 2 10:40
bbb tty02 july 2 10:55
$
3)The cat command:
cat - concatenate files and print on the standard output
Concatenate FILE(s), or standard input, to standard output.
Example:
$cat > newfile
Hi
Hell
Welcome
$
The input operation is terminated by using <ctrl+d> command
4)The mkdir command:
mkdir - make directories
It creates the directories if they do not already exist.
Example:
$mkdir user1
$
5)The cd command:
cd - change directories
It is a command-line OS shell command used to change the current working directory
Example:
$cd user1
user1]$
6)The cp command:
cp - copy files and directories
This command is used to create a duplicate of a file, a set of files or a directory The cp
command copies both text and binary files
Syntax:
$cp source-file/directory destination-file/directory Examples:
i) $cp file1 file2
ii) $cp dir1/file1 dir1/file2
7)The ps command:
ps - report a snapshot of the current processes.
The ps command is used to display the attributes of processes that are running currently.
Example:
$ps
PID TTY TIME CMD
476 tty03 00:00:01 login
659 tty03 00:00:01 sh
684 tty03 00:00:00 ps
$
8)The ls command:
ls - list directory contents
This command lists the contents in a directory. Syntax:
$ls
Example:
$ls
9)The mv command:
mv - move (rename) files
This command is used to move either an individual file, a list of files or a directory
Syntax:
$mv source-file/directory destination-file/directory Example:
$mv dir1/file1 dir2
The mv command is also used to rename a file or directory
Syntax:
$mv old-file-name new-file-name
Example:
$mv file1 file7
10) The rm command:
rm - remove files or directories
This command is used remove a file or a directory. Syntax:
$rm filename
Example:
$rm file1
11) The rmdir command:
rmdir - remove empty directories
This command is used to delete a directory
Syntax:
$rmdir directory-name
Example:
$rmdir user1
12) The echo command:
echo - display a line of text
This command is used to display messages. It
takes zero, one or more arguments.
Example:
$echo “Hello World”
Hello World
$
13) The more command:
more - file perusal filter for crt viewing
This command is used to display a file.
- It allows us to set the output page size and pauses at the end of each page to allow us to
read the file.
Syntax:
$more filename
Example:
$more story1
14) The date command:
date - print or set the system date and time
Using this command, the user can display the current date along with the time nearest to the
second
Example:
$date
Sat Jan 10 11:58:00 IST 2004
$
15) The time command:
time - time a simple command or give resource usage This
command is used to know the resource usage
It runs a program or command with given arguments, generates a timing statistics about
the program run and directs this statistics report to the standard output.
Example:
$time
real 0m0.000s
user 0m0.000s
sys 0m0.000s
$
16) The kill command:
kill - terminate a process
To kill a background process a kill command is used.
This command is given with the PID of the process to be killed as its argument Example:
$kill 555
Here, 555 is the PID of the process
17) The history command:
history – Used to display the history of previous executed commands With no
options, display the command history list with line numbers Example:
$history 1
pwd
2 date
3 cal
4 history
18) The chmod command:
chmod - change file mode bits
The chmod command is used to change permissions of a file after its creation Only the
Owner or Super User can change file permissions
Syntax:
$chmod assignment-expression filename
Example:
$chmod u+x sample
$ls -l sample
- r w x r - - r - - sample
19) The chown command:
chown - change file owner and group
Only the owner can change the major attributes of a file
Sometimes it is necessary to change the ownership of a file.
Example:
$ls -l sample
-rwxr--r-x 1 dhoni July 19 11:55 sample
$chown virat sample
$ls -l sample
-rwxr--r-x 1 virat July 19 11:55 sample
20) The finger command:
finger - user information lookup program
The finger displays information about the system users.
Example:
$finger
Login Name TTY Idle When Where
root super user tty01 1:24 Mon 14:00
501 ABD tty02 0:20 Mon 15:40 abd.cric.com
502 Federer tty03 2:12 Mon 15:40 fedrr.batm.com

21) The pwd command:


pwd – print name of current/working directory.
This command is used to determine the location of the current directory in the directory
structure
Syntax:
$pwd Example:
$pwd
22) The cal command:
cal - displays a calendar
This command is used to print the calendar of a specific month or a specific year Example:
$cal 03 2018
23) The logout command:
logout - Exit a login shell.
Example:
$logout
24) The shutdown command:
shutdown - bring the system down
shutdown arranges for the system to be brought down in a safe way. Example:
$shutdown
b) Study of vi editor Vi
Editor:
- An editor allows the users to see a portion of a file on the screen and to modify
characters and lines by simply typing at the cursor position.
- The vi editor represents,
- Vi stands for visual
- It is a full screen editor and allows the user to view and edit the entire
document at the same time.
- Vi is case sensitive.
- It has powerful undo features.
Modes of Vi editor:
i) Command mode:
- In this mode all the keys pressed by the user are interpreted to be editor
commands.
- No text is displayed on the screen even if corresponding keys is pressed on the
keyboard.
ii) Insert mode:
- This mode permits to insert a new text, editing and replacement of existing text.
- When vi editor is in insert mode the letters typed at the keyboard are echoed on the
screen.
iii) Escape mode:
- Commands typed at the command line.
Starting with vi editor:
Syntax:
$vi filename
Editing the file:
- Open the file using $ vi filename
- To add text at the end of the file, position the cursor at the last character of the file.
- Switch from command mode to text input mode by pressing ‘a’.
- Here ‘a’ stands for append.
- Inserting text in the middle of the file is possible by pressing ‘i’.
- The editor accepts and inserts the typed character until Esc key is pressed. Saving
text:
:w – save the file and remains in edit mode
:wq – save the file and quits from edit mode
:q – quit without changes from edit mode
Quitting vi:
Press zz or ‘:wq’ in command mode.
b) Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system
Types of Shells:
- The Shells are 4 types
1. The Bourne Shell (sh)
2. The C Shell (csh)
3. The Korn Shell (ksh)
4. The Bourne-Again Shell (bash)
1)The Bourne Shell (sh):
- This is the most common shell available on Unix Systems.
- The first major shell developed by Stephen Bourne at AT&T Bell labs. This shell is
widely used.
- This shell is distributed as the standard shell on almost all Unix Systems.
2)The C Shell (csh):
- It is developed by Bill Joy at UCB as part of the BSD release.
- Its syntax and usage is very similar to the C programming language. This shell is not
available on all machines.
- Shell scripts written in the C shell are not compatible with the Bourne Shell.
- A version of it called tcsh is available free of cost under Linux.
3)The Korn Shell (ksh):
- This shell was developed by David Korn at AT&T Bell labs.
- Basically it is built on the Bourne Shell. It also incorporates certain features of the C shell.
- At present it is one of the widely used shells.
- It can run Bourne shell scripts without any modification.
- One of its versions, the Public Domain Korn Shell (pdksh), comes with Linux free of cost.
4)The Bourne-Again Shell:
- It was developed by B Fox and C Ramey at Free Software Foundation.
- Certain Linux operating system variants come with this shell as its default shell.
- This is clearly a free ware shell.
c) Study of Unix/Linux file system (tree structure)

- It is also called as UNIX File System, because it is used to organize UNIX files.
- The different directories in the Directory Hierarchy are,
d) Study of .bashrc, /etc/bashrc and Environment variables.
.bashrc
.bashrc file is automatically executed when new terminal(shell) is opened. Purpose of
bashrc file:
You can export environment variables(So there is no need to export
environment variable every time)
You can define aliases. You can provide the path for cross compiler. You can add your own
script which can start automatically whenever
new shell is opened.
You can change the history length
/etc/bashrc
Like .bash_profile you will also commonly see a .bashrc file in your home
directory. This file is meant for setting command aliases and functions used by bash
shell users.
Just like the /etc/profile is the system wide version of .bash_profile. The
/etc/bashrc for Red Hat and /etc/bash.bashrc in Ubuntu is the system wide version of
.bashrc.
Interestingly enough in the Red Hat implementation the /etc/bashrc also executes the shell
scripts within /etc/profile.d but only if the users shell is a Interactive Shell (aka Login
Shell)
Environment Variables:
variable purpose
PATH The PATH variable holds a list of directories in a certain order. In this
list colon {:) separate different directories
HOME When a user logs in, he or she will be automatically placed in the home
directory. This directory is decided by the system administrator at the
time of opening an account for a user.This directory is stored in the file
/etc/passwd
IFS This variable holds tokens used by the shell commands to parse a
string
into substrings such as a word or a record into its individual fields. The

default tokens are the three white space tokens Space, Tab, New line
MAIL This variable holds the absolute pathname of the file where user’s
mail
is kept. Usually the name of this file is the user’s login name
SHELL This variable contains the name of the users shell program in the form
of
absolute pathname. System administrator sets the default shell If
required, user can change it
TERM This variable holds the information regarding the type of the terminal
being used. If TERM is not set properly, utilities like vi editor will
not
work
2) Write a C program that makes a copy of a file using standard I/O, and system

calls. #include <stdio.h>

#include <unistd.h>
#include <fcntl.h>
void typefile (char *filename)
{
int fd, nread;
char buf[1024];
fd = open (filename, O_RDONLY);
if (fd == -1)
{
perror (filename);
return;
}
while ((nread = read (fd, buf, sizeof (buf))) > 0)
write (1, buf, nread);
close (fd);
}
int main (int argc, char **argv)
{
int argno;
for (argno = 1; argno<argc; argno++)
typefile (argv[argno]);
}

Output:
$cc ioscalls.c
$cat>ff
Hi hello
$./a.out ff
Hi hello
3. Write a C program to emulate the Unix ls-l command.
#include <stdio.h> #include
<unistd.h> #include
<sys/types.h> #include
<sys/wait.h> #include
<stdlib.h>
int main()
{
int pid; //process id
pid = fork(); //create another process if (
pid < 0 )
{ //fail
printf(“\nFork failed\n”); exit
(-1);
}
else if ( pid == 0 )

{ //child
execlp ( “/bin/ls”, “ls”, “-l”, NULL ); //execute ls
}
else
{ //parent
wait (NULL); //wait for child
printf(“\nchild complete\n”);
exit (0);
}
}

OUTPUT:
guest-glcbIs@ubuntu:~$gcc –o lsc.out lsc.c
guest-glcbIs@ubuntu:~$./lsc.out
total 100
-rwxrwx—x 1 guest-glcbls guest-glcbls 140 2012-07-06 14:55 f1
drwxrwxr-x 4 guest-glcbls guest-glcbls 140 2012-07-06 14:40 dir1 child
complete
4) Write a program that illustrates how to execute two commands concurrently with a command
pipe
#include <stdio.h> #include
<unistd.h> #include
<sys/types.h> #include
<stdlib.h>
int main()
{
int pfds[2]; char
buf[30];
if(pipe(pfds)==-1)
{
perror("pipe failed");
exit(1);
}
if(!fork())
{
close(1);
dup(pfds[1];
system (“ls –l”);
}
else
{
printf("parent reading from pipe \n");
while(read(pfds[0],buf,80)) printf("%s \
n" ,buf);
}
}

Output:
[student@gcet ~]$ vi pipes2.c
[student@gcet ~]$ cc pipes2.c
[student@gcet ~]$ ./a.out Parent
reading from pipe Total 24
-rwxrwxr-x l student student 5563Aug 3 10:39 a.out
-rw-rw-r—l
Student student 340 jul 27 10:45 pipe2.c
-rw-rw-r—l student student
Pipes2.c
-rw-rw-r—l student student 401 34127 10:27 pipe2.c
student
) Simulate the following CPU scheduling algorithms:
a.SJF
b. FCFS
c.ROUND ROBIN
d. PRIORITY
a.SJF (Non - Preemptive)
#include<stdio.h>
int main()
{
int i,n,p[10]={1,2,3,4,5,6,7,8,9,10},min,k=1,btime=0;
int bt[10],temp,j,at[10],wt[10],tt[10],ta=0,sum=0; float
wavg=0,tavg=0,tsum=0,wsum=0;
printf(" -------Shortest Job First Scheduling ( NP ) ---------\n");
printf("\nEnter the No. of processes :");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\tEnter the burst time of %d process :",i+1);
scanf(" %d",&bt[i]);
printf("\tEnter the arrival time of %d process :",i+1);
scanf(" %d",&at[i]);
}

/*Sorting According to Arrival Time*/

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(at[i]<at[j])
{
temp=p[j];
p[j]=p[i];
p[i]=temp;
temp=at[j];
at[j]=at[i];
at[i]=temp;
temp=bt[j];
bt[j]=bt[i];
bt[i]=temp;
}
}
}

/*Arranging the table according to Burst time,


Execution time and Arrival Time
Arrival time <= Execution time
*/

for(j=0;j<n;j++)
{
btime=btime+bt[j];
min=bt[k];
for(i=k;i<n;i++)
{
if (btime>=at[i] && bt[i]<min)
{
temp=p[k];
p[k]=p[i];
p[i]=temp;
temp=at[k];
at[k]=at[i];
at[i]=temp;
temp=bt[k];
bt[k]=bt[i];
bt[i]=temp;
}
} k+
+;
}
wt[0]=0;
for(i=1;i<n;i++)
{
sum=sum+bt[i-1];
wt[i]=sum-at[i];
wsum=wsum+wt[i];
}

wavg=(wsum/n);
for(i=0;i<n;i++)
{
ta=ta+bt[i];
tt[i]=ta-at[i];
tsum=tsum+tt[i];
}

tavg=(tsum/n);

printf("**********************
**"); printf("\n RESULT:-");
printf("\nProcess\t Burst\t Arrival\t Waiting\t Turn-around" );
for(i=0;i<n;i++)
{
printf("\n p%d\t %d\t %d\t\t %d\t\t\t%d",p[i],bt[i],at[i],wt[i],tt[i]);
}

printf("\n\nAVERAGE WAITING TIME : %f",wavg);


printf("\nAVERAGE TURN AROUND TIME :
%f",tavg); return 0;
}

OUTPUT:
-------Shortest Job First Scheduling ( NP )-------
Enter the No. of processes :3
Enter the burst time of 1 process :4
Enter the arrival time of 1 process :1
Enter the burst time of 2 process :3
Enter the arrival time of 2 process :0
Enter the burst time of 3 process :5
Enter the arrival time of 3 process :2
******************
****** RESULT:-
Process Burst Arrival Waiting Turn-around
p2 3 0 0 3
p1 4 1 2 6
p3 5 2 5 10

AVERAGE WAITING TIME : 2.333333


AVERAGE TURN AROUND TIME : 6.333333

b) FCFS:
Algorithm for FCFS scheduling:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Set the waiting of the first process as ‘0’ and its burst time as its turnaround time
Step 5: for each process in the Ready Q calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-
1)
(b) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for
process(n)
Step 6: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process Step
7: Stop the process

PROGRAM:

#include<iostream> using
namespace std; class fcfs
{
int a[10],k,b[10],c[10],d[10],n,i,j,sum,total; float
awt,atat;
public:
void getdata()
{
k=total=sum=0;
cout<<"enter the no of processes:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter the burst time for process:"<<i+1<<" ";
cin>>a[i];
d[i]=i+1;
}
b[0]=0;
for(i=0;i<n;i++)
{
k=k+a[i];
b[i+1]=k;
}
for(i=0;i<n;i++)
{
c[i]=a[i]+b[i];
sum=sum+b[i];
total=total+c[i];
}
awt=sum/n;
atat=total/n;
}
void putdata()
{
cout<<"Process\tBurst time\twaiting time\tTurn around time\n";
for(i=0;i<n;i++)
{
cout<<d[i]<<"\t"<<a[i]<<"\t"<<b[i]<<"\t"<<c[i]<<"\n";
}
cout<<"average waitig time:"<<awt;
cout<<"\nTurn around time:"<<atat;
}
};
int main()
{
fcfs obj;
obj.getdata();
obj.putdata();
return 0;
}

OUTPUT:
enter the no of processes:3
enter the burst time for process:1 2
enter the burst time for process:2 5
enter the burst time for process:3 4
Process Burst time waiting time Turn around time
1 2 0 2
2 5 2 7
3 4 7 11
average waiting time:3
Turn around time:6

c) Algorithm for ROUND ROBIN:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Calculate the no. of time slices for each process where
No. of time slice for process(n) = burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst time of process(n-1
) + the time difference in getting the CPU from process(n-1)
(b) Turn around time for process(n) = waiting time of process(n) + burst time of
process(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process Step
8: Stop the process
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0; float
awt=0.0,atat=0.0;
clrscr();
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter burst time for sequences:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum:");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no \t Burst time \t Wait time \t Turn around time\n"); for(i=0;i<n;i++)
printf("%d \t%d \t%d \t%d\n",i+1,bt[i],wt[i],tat[i]);
printf("Avg wait time is %f \n Avg turn around time is %f",awt,atat); getch();
}
OUTPUT:
Enter number of processes:3 Enter
burst time for sequences:2 3
1
Enter time quantum:2
Process_noBurst time Wait time Turn around time
1 2 0 2
2 3 3 6
3 1 4 5
Avg wait time is 2.333333
Avg turn around time is 4.333333

d) Algorithm for PRIORITY:


PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0; float
awt,atat;
printf("\n-----------PRIORITY SCHEDULING ---------\n");
printf("Enter the No of Process: ");
scanf("%d", &n);
for (i=0;i<n;i++)
{
pid[i] = i;
printf("Enter the Burst time of Pid %d : ",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d : ",i);
scanf ("%d",&pr[i]);
}
for (i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if (pr[i] > pr[j] )
{
t = pr[i];
pr[i] = pr[j];
pr[j] = t;
t = bt[i];
bt[i] = bt[j];
bt[j] = t;
t = pid[i]; pid[i]
= pid[j]; pid[j]
= t;
}
}
tat[0] = bt[0];
wt[0] = 0;
for (i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = wt[i] + bt[i];
}
printf("\n \n");
printf("Pid\t Priority\tBurst time\t WaitingTime\tTurnArroundTime\n");
printf("\n \n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
ttat = ttat+tat[i];
twt = twt + wt[i];
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time: %f\n",awt,atat);
}
OUTPUT:
-----------PRIORITY SCHEDULING--------------
Enter the No of Process: 3 Enter
the Burst time of Pid 0 : 1 Enter
the Priority of Pid 0 : 2 Enter the
Burst time of Pid 1 : 1 Enter the
Priority of Pid 1 : 3 Enter the
Burst time of Pid 2 : 4 Enter the
Priority of Pid 2 : 1
Pid Priority Burst time WaitingTime TurnArroundTime

2 1 4 0 4
0 2 1 4 5
1 3 1 5 6

Avg.Waiting Time: 3.000000


Avg.Turn Around Time:5.000000
6) Multiprogramming-Memory management- Implementation of fork (), wait (), exec() and exit (),
System calls

#include<stdio.h>
#include<unistd.h>
main( )
{
int i, pid;
pid=fork( );
if(pid== -1)
{
perror("fork failed");
exit(0);
}
else if(pid==0)
{
printf("\n Child process starts"); for(i=0;
i<5; i++)
{
printf("\n Child process %d is called", i);
}
printf("\n Child process ends");
}
else
{
wait(0);
printf("\n Parent process ends");
}
exit(0);
}
OUTPUT
:
Child process starts Child
process 0 is called Child
process 1 is called Child
process 2 is called Child
process 3 is called Child
process 4 is called Child
process ends Parent
process ends
7) a. Multiprogramming with fixed number of tasks (MFT) :

Algorithm:

Step1: start the process. Step2:


Declare variables. Step3: Enter
total memory size. Step4: Allocate
memory for os.
Step5: allocate total memory to the pages.
Step6: Display the wastage of memory.
Step7: Stop the process

Program:
#include<stdio.h>
#include<conio.h>

main()
{
int i,m,n,tot,s[20];
printf("Enter total memory size:");
scanf("%d",&tot);
printf("Enter no. of pages:");
scanf("%d",&n);
printf("Enter memory for OS:");
scanf("%d",&m); for(i=0;i<n;i++)
{
printf("Enter size of page%d:",i+1);
scanf("%d",&s[i]);
}
tot=tot-m;
for(i=0;i<n;i++)
{
if(tot>=s[i])
{
printf("Allocate page %d\n",i+1);
tot=tot-s[i];
}
else
printf("process p%d is blocked\n",i+1);
}
printf("External Fragmentation is=%d",tot);
}
Output:
Enter total memory size:50
Enter no. of pages:4
Enter memory for OS:10
Enter size of page1:10
Enter size of page2:9
Enter size of page3:9
Enter size of page4:10
Allocate page 1
Allocate page 2
Allocate page 3
Allocate page 4
External Fragmentation is=2
Multiprogramming with a variable number of task (MVT) :

Algorithm:

Step1: start the process.


Step2: Declare variables. Step3:
Enter total memory size. Step4:
Allocate memory for os.
Step5: allocate total memory to the pages.
Step6: Display the wastage of memory.
Step7: Stop the process.

Program:
#include<stdio.h>
#include<conio.h>
main()
{
int ms,i,ps[20],n,size,p[20],s,intr=0;
clrscr();
printf("Enter size of memory:");
scanf("%d",&ms);
printf("Enter memory for OS:");
scanf("%d",&s);
ms-=s;
printf("Enter no.of partitions to be divided:");
scanf("%d",&n);
size=ms/n;
for(i=0;i<n;i++)
{
printf("Enter process and process size");
scanf("%d%d",&p[i],&ps[i]);
if(ps[i]<=size)
{
intr=intr+size-ps[i];
printf("process%d is allocated\n",p[i]);
}
else
printf("process%d is blocked",p[i]);
}
printf("total fragmentation is %d",intr); getch();
}

Output:
Enter size of memory:50
Enter memory for OS:10
Enter no.of partitions to be divided:4
Enter process and process size1 10
process1 is allocated
Enter process and process size2 9
process2 is allocated
Enter process and process size3 9
process3 is allocated
Enter process and process size4 8
process4 is allocated
total fragmentation is 4
8,9) Simulate Banker’s Algorithm for Deadlock avoidance & Dead Lock Prevention

#include<stdio.h>
#include<conio.h>
struct da
{
int max[10],a1[10],need[10],before[10],after[10];
}p[10];
void main()
{
int i,j,k,l,r,n,tot[10],av[10],cn=0,cz=0,temp=0,c=0; clrscr();

printf("\n ENTER THE NO. OF PROCESSES:");


scanf("%d",&n);
printf("\n ENTER THE NO. OF RESOURCES:");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("PROCESS %d \n",i+1);
for(j=0;j<r;j++)
{
printf("MAXIMUM VALUE FOR RESOURCE %d:",j+1);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("ALLOCATED FROM RESOURCE %d:",j+1);
scanf("%d",&p[i].a1[j]);
p[i].need[j]=p[i].max[j]-p[i].a1[j];
}
}
for(i=0;i<r;i++)
{
printf("ENTER TOTAL VALUE OF RESOURCE %d:",i+1);
scanf("%d",&tot[i]);
}
for(i=0;i<r;i++)
{
for(j=0;j<n;j++) temp=temp+p[j].a1[i];
av[i]=tot[i]-temp;
temp=0;
}
printf("\n\t RESOURCES ALLOCATED NEEDED TOTAL AVAIL");
for(i=0;i<n;i++)
{
printf("\n P%d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].max[j]);
printf("\t"); for(j=0;j<r;j+
+) printf("%d",p[i].a1[j]);
printf("\t"); for(j=0;j<r;j+
+)
printf("%d",p[i].need[j]);
printf("\t"); for(j=0;j<r;j+
+)
{
if(i==0)
printf("%d",tot[j]);
}
printf(" "); for(j=0;j<r;j+
+)
{
if(i==0)
printf("%d",av[j]);
}
}
printf("\n\n\t AVAIL BEFORE\T AVAIL AFTER ");
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
if(p[i].need[j] >av[j]) cn+
+; if(p[i].max[j]==0)
cz++;
}
if(cn==0 && cz!=r)
{
for(j=0;j<r;j++)
{
p[i].before[j]=av[j]-p[i].need[j];
p[i].after[j]=p[i].before[j]+p[i].max[j];
av[j]=p[i].after[j];
p[i].max[j]=0;
}
printf("\n P %d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].before[j]);
printf("\t"); for(j=0;j<r;j++)
printf("%d",p[i].after[j]);
cn=0;
cz=0;
c++;
break;
}
else
{
cn=0;cz=0;
}
}
}
if(c==n)
printf("\n THE ABOVE SEQUENCE IS A SAFE SEQUENCE");
else
printf("\n DEADLOCK OCCURED");
}

OUTPUT:
ENTER THE NO. OF PROCESSES:5

ENTER THE NO. OF RESOURCES:3

PROCESS 1

MAXIMUM VALUE FOR

RESOURCE 1:7 MAXIMUM VALUE

FOR RESOURCE 2:5 MAXIMUM

VALUE FOR RESOURCE 3:3

ALLOCATED FROM RESOURCE 1:0

ALLOCATED FROM RESOURCE 2:1

ALLOCATED FROM RESOURCE 3:0

PROCESS 2
MAXIMUM VALUE FOR

RESOURCE 1:3 MAXIMUM VALUE

FOR RESOURCE 2:2 MAXIMUM

VALUE FOR RESOURCE 3:2

ALLOCATED FROM RESOURCE 1:2

ALLOCATED FROM RESOURCE 2:0

ALLOCATED FROM RESOURCE 3:0

PROCESS 3

MAXIMUM VALUE FOR

RESOURCE 1:9 MAXIMUM VALUE

FOR RESOURCE 2:0 MAXIMUM

VALUE FOR RESOURCE 3:2

ALLOCATED FROM RESOURCE 1:3

ALLOCATED FROM RESOURCE 2:0

ALLOCATED FROM RESOURCE 3:2

PROCESS 4

MAXIMUM VALUE FOR

RESOURCE 1:2 MAXIMUM VALUE

FOR RESOURCE 2:2 MAXIMUM

VALUE FOR RESOURCE 3:2

ALLOCATED FROM RESOURCE 1:2

ALLOCATED FROM RESOURCE 2:1

ALLOCATED FROM RESOURCE 3:1

PROCESS 5

MAXIMUM VALUE FOR RESOURCE

1:4 MAXIMUM VALUE FOR

RESOURCE 2:3 MAXIMUM VALUE

FOR RESOURCE 3:3 ALLOCATED

FROM RESOURCE 1:0 ALLOCATED

FROM RESOURCE 2:0 ALLOCATED

FROM RESOURCE 3:2 ENTER TOTAL

VALUE OF RESOURCE 1:10 ENTER


TOTAL VALUE OF RESOURCE 2:5

ENTER TOTAL VALUE OF RESOURCE 3:7

RESOURCES ALLOCATED NEEDED TOTAL AVAIL

P1 753 010 743 1057 332

P2 322 200 122

P3 902 302 600

P4 222 211 011


P5 433 002 431

AVAIL BEFORET AVAIL


AFTER

P 2 210 532

P 4 521 743

P 1 000 753

P 3 153 1055
P 5 624 1057

THE ABOVE SEQUENCE IS A SAFE SEQUENCE


10) Simulate the following page replacement algorithms:
a. FIFO b. LRUc. LFU

a. page replacement algorithm FIFO


PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int i, j, k, f, pf=0, count=0, rs[25], m[10], n;
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])
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);
}
OUTPUT:
The Page Replacement Process is –
7 -1 -1 PF No. 1
7 0 -1 PF No. 2
7 0 1 PF No. 3
2 0 1 PF No. 4
201
2 3 1 PF No. 5
2 3 0 PF No. 6
4 3 0 PF No. 7
4 2 0 PF No. 8
4 2 3 PF No. 9
0 2 3 PF No. 10
023
023
0 1 3 PF No. 11
0 1 2 PF No. 12
012
012
7 1 2 PF No. 13
7 0 2 PF No. 14
7 0 1 PF No. 15
The number of Page Faults using FIFO are 15

page replacement algorithm LRU

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;
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();
}

INPUT
Enter the length of reference string -- 20
Enter the reference string -- 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


OUTPUT
The Page Replacement process is -- 7 -
1 -1 PF No. -- 1
7 0 -1 PF No. -- 2
7 0 1 PF No. -- 3
2 0 1 PF No. – 4
11) Simulate the following File allocation strategies:
a) Sequenced file allocation
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no.of files:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n"); for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
}
OUTPUT
:
Enter no.of files:3
Enter no. of blocks occupied by file1 15
Enter the starting block of file1 11
Enter no. of blocks occupied by file2 8
Enter the starting block of file2 3
Enter no. of blocks occupied by file3 4

Enter the starting block of file33


Filename Start block length
1 11 15
2 3 8
3 3 4
Enter file name:2
File name is:2length is:8blocks occupied: 3 4 5 6 7 8 9 10
Indexed file allocation:

#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{

printf("Enter starting block and size of file%d:",i+1); scanf("%d


%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++) scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
}
OUTPUT
Enter no. of files:3
Enter starting block and size of file1:1 6
Enter blocks occupied by file1:3
enter blocks of file1:1 2 3
Enter starting block and size of file2:2 3
Enter blocks occupied by file2:3
enter blocks of file2:1 2 3
Enter starting block and size of file3:9 5
Enter blocks occupied by file3:1
enter blocks of file3:5

File index length 1


1 3
2 2 3
3 9 1

Enter file name:2


file name is:2
Index is:2
Block occupied are: 1 2 3
{
12)Write a C program to simulate producer-consumer problem using

semaphores. PROGRAM:

#include<std
ioio.h> void
main()

int buffer[10], bufsize, in, out, produce, consume, choice=0; in = 0; out = 0; bufsize

while(choice !=3)
{
printf(“\n1. Produce \t 2. Consume \t3. Exit”); printf(“\
nEnter your choice: ”); scanf(“%d”, &choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce); buffer[in] =
produce;
in = (in+1)%bufsize;
}
reak;
case 2: if(in == out) printf(“\
nBuffer is Empty”); else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume); out = (out+1)%bufsize;
}
break;
}
} }
OUTPUT
1. Produce 2. Consume 3. Exit
Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit
Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3. Exit
Enter your choice: 3
13) Write a C program that illustrates two processes communicating using shared memory.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h> int
main()
{
int i;

void *shared_memory; char


buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT); //creates shared memory
segment with key 2345, having size 1024 bytes.
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0); //process attached to shared memory segment
printf("Process attached at %p\n",shared_memory); //this prints the address where the segment is
attached with this process
printf("Enter some data to write to shared memory\n");
read(0,buff,100); //get some input from user
strcpy(shared_memory,buff); //data written to shared memory
printf("You wrote : %s\n",(char *)shared_memory);
}
OUTPUT:
14) Write a c program to create a thread using pthread library and let it run its function
#include<unistd.h> //header file for sleep
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *mythread(void *vargp)
{
sleep(1);
printf("welcome to Goeduhub!! ·\n");
return NULL;
}
int main()
{
pthread_t tid; printf("before
thread\n");
pthread_create(&tid,NULL,mythread,NULL);
pthread_join(tid,NULL);
exit(0);
}
OUTPUT:

15) Write a c program to illustrate concurrent executionof threads using pthreads library
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *mythread1(void *vargp)
{
int i; printf("thread1\n");
for(i=1;i<=10;i++)
printf("i=%d\n",i);
printf("exit from thread1\n"); return
NULL;
}
void *mythread2(void *vargp)
{
int j; printf("thread2 \
n"); for(j=1;j<=10;j++)
printf("j=%d\n",j);
printf("Exit from thread2\n"); return
NULL;
}
int main()
{
pthread_t tid; printf("before
thread\n");
pthread_create(&tid,NULL,mythread1,NULL);
pthread_create(&tid,NULL,mythread2,NULL);
pthread_join(tid,NULL); pthread_join(tid,NULL);
exit(0);
}

OUT PUT ::
$ cc w8.c – l pthread
$./a.out
thread1
i=1
i=2;
i=3
thread2
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
i=4
i=5
i=6
i=7
i=8
i=9
i=10
exit from thread1 j=9
j=10
exit from thread2
ADDITIONAL EXPERIMENTS

1) Simulate Scan Disk Scheduling algorithm

#include <stdio.h>
#include <stdlib.h>
int main(){
int RQ[100], i, j, n, TotalHeadMoment = 0, initial, size, move;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
printf("Enter total disk size\n");
scanf("%d", &size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d", &move);
for (i = 0; i < n; i++){
for (j = 0; j < n - i - 1; j++){
if (RQ[j] > RQ[j + 1]){
int temp;
temp = RQ[j];
RQ[j] = RQ[j + 1];
RQ[j + 1] = temp;
}
}
}
int index;
for (i = 0; i < n; i++){
if (initial < RQ[i]){
index = i;
break;
}
}
if (move == 1){
for (i = index; i < n; i++){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
TotalHeadMoment = TotalHeadMoment + abs(size - RQ[i - 1] - 1);
TotalHeadMoment = TotalHeadMoment + abs(size - 1 - 0);
initial = 0;
for (i = 0; i < index; i++){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
}
else{
for (i = index - 1; i >= 0; i--){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
TotalHeadMoment = TotalHeadMoment + abs(RQ[i + 1] - 0);
TotalHeadMoment = TotalHeadMoment + abs(size - 1 - 0);
initial = size - 1;
for (i = n - 1; i >= index; i--){
TotalHeadMoment = TotalHeadMoment + abs(RQ[i] - initial);
initial = RQ[i];
}
}
printf("Total head movement is %d", TotalHeadMoment);
return 0;
}
b) Linked file allocation:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
printf("Enter no. of files:");
scanf("%d",&n); for(i=0;i<n;i+
+)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start; printf("Enter
no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
}

Enter no. of
OUTP files:2 Enter
UT:
f lock:20 Enter
i no.of blocks:6
l Enter block
e numbers:4 12
15 45 32 25
n Enter file
a name:bunny
m Enter starting
e block:12 Enter
: no.of blocks:5
s Enter block numbers:6 5 4 3 2
u File start size block
n sunny 20 6 4--->12--->15--->45--->32--->25
n bunny 12 5 6--->5--->4--->3--->2
y

E
n
t
e
r

s
t
a
r
t
i
n
g

b
201
2 0 3 PF No. -- 5
203
4 0 3 PF No. -- 6
4 0 2 PF No. -- 7
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

Optimal page replacement algorithm


PROGRAM
#include<stdio.h>
int n;
main()
{
int seq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s; int
count=1,pf=0,p=0;
Float pfr;
clrscr();
printf("Enter maximum limit of the sequence: ");
scanf("%d",&max);
printf("\nEnter the sequence: ");
for(i=0;i<max;i++)
scanf("%d",&seq[i]); printf("\nEnter
no. of frames: "); scanf("%d",&n);
fr[0]=seq[0];
pf++;
printf("%d\t",fr[0]);
i=1;
while(count<n)
{
flag=1;
p++;
for(j=0;j<i;j++)
{
if(seq[i]==seq[j])
flag=0;
}
if(flag!=0)
{
fr[count]=seq[i];
printf("%d\t",fr[count]);
count++;
pf++;
}
i++;
}
printf("\n");
for(i=p;i<max;i++)
{
flag=1;
for(j=0;j<n;j++)
{
if(seq[i]==fr[j])
flag=0;
}
if(flag!=0)
{
for(j=0;j<n;j++)
{
m=fr[j]; for(k=i;k<max;k++)
{
if(seq[k]==m)
{
pos[j]=k;
break;
}
else
pos[j]=1;
}
}
for(k=0;k<n;k++)
{
if(pos[k]==1)
flag=0;
}
f(flag!=0)
s=findmax(pos);
if(flag==0)
{
for(k=0;k<n;k++)
{
if(pos[k]==1)
{
s=k;
break;
}
}
}
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%d\t",fr[k]);
pf++;
printf("\n");
}
}
pfr=(float)pf/(float)max;
printf("\nThe no. of page faults are %d",pf); printf("\
nPage fault rate %f",pfr);
getch();
}
Int findmax(int a[])
{
int max,i,k=0;
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
k=i;
}
}
return k;
}

INPUT

Enter number of page references --10


Enter the reference string -- 1 2 3 4 5 2 5 2 5 1 4 3
43
Enter the available no. of frames – 3

OUTPUT
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

You might also like