You are on page 1of 42

PANJAB UNIVERSITY SWAMI SARVANAND

GIRI REGIONAL CENTRE , HOSHIARPUR

OPERATING SYSTEM PRACTICAL FILE


SUBMITTED BY : - SUBMITTED TO ; -
Name – Amit kumar Ms. Divya Sharma
Roll No – SG21805
Branch – IT
Semester – 4th

1
INDEX

Sr. No Title Page No

0 Introduction to Operating System And Its Type 03 - 07


1

0 BASIC COMMANDS USED IN COMMAND PROMPT 04 - 14


2

0 First Come First Serve(FCFS) Algorithm 15 - 18


3

0 Shortest Job First (SJF) CPU Scheduling Algorithm 18 - 21


4

0 Priority Scheduling Algorithm 22 - 24


5

0 Round Robin Scheduling Algorithm 24 - 26


6

0 FIFO Page Replacement Algorithm 26 - 28


7

0 Optimal Page Replacement Algorithm 28 - 30


8

0 LRU Page Replacement Algorithm 30 - 32


9

1 Dining Philospher’s Problem ( Deadlock Problem) 33 - 36


0

1 Banking Problem ( Deadlock Problem) 37 - 38


1

2
PRACTICAL - 1
Introduction to Operating System And Its Type
Operating System
An operating system (OS) is the program that, after being initially loaded into the computer
by a boot program, manages all of the other application programs in a computer. The
application programs make use of the operating system by making requests for services
through a defined application program interface (API).Operating system controls the operation
of a computer, directs the input and output of data, keeps track of files, and controls the
processing of computer programs.

TYPES OF OPERATING SYSTEMS : -

 Batch OS.
 Distributed OS.
 Multitasking OS.
 Network OS.
 Real-OS.
 Mobile OS
Batch OS
A Batch Operating System (BOS) is a computer system that allows multiple users to use it,
without direct communication between them. It achieves this by keeping all users in
separate ‘batches’, meaning they can’t interact with each other directly. This makes it ideal
for applications where users need to work on separate parts of a project, without getting in
the way of each other.

3
2. Multiprocessing OS
A Multi-user os is a computer os which allows multiple users to access the single system with one
operating system on it. It is generally used on large mainframe computers.

3. Multiprogram OS

A Multiprogramming operating system is an operating system that supports the running of


numerous programs simultaneously on a single processor machine. If one program waits for an
input/ output transfer, the other programs are ready to utilize the CPU.
Imagine that the currently executing process is involved in I/O. (which, by definition, does not
need the CPU to be accomplished). The OS may then halt that process and transfer control to
another in-memory program that is prepared to run (i.e., context switching).

A multiprogramming operating system's overall purpose is to keep the CPU busy until all the tasks
in the CPU get executed entirely. Thus, A computer with a single processor may run several
programs simultaneously, and the CPU is never idle.
Types of Multiprogramming Operating System
There are two types of multiprogramming operating systems:
 Multitasking Operating Systems
 Multiuser Operating System

4
Multitasking Operating System
Modern computers use the word "multitasking." A multiprogramming operating system that
allows numerous applications to execute simultaneously supports multitasking as a logical
extension. In this method, a CPU and other shared processing resources are shared by several
tasks, also called processes.

Multiuser Operating System


A computer operating system that enables several users to connect to a single system running the
same operating system is known as the multi user os.
5
4. Handheld OS

Handheld Operating System:


Handheld operating systems are available in all handheld devices like Smartphones and tablets. It
is sometimes also known as a Personal Digital Assistant. The popular handheld device in today’s
world is Android and iOS. These operating systems need a high-processing processor and are also
embedded with various types of sensors.
Features of Handheld Operating System:
1. Its work is to provide real-time operations.
2. There is direct usage of interrupts.
3. Input/Output device flexibility.
4. Configurability.
Types of Handheld Operating Systems:
1. Palm OS
2. Symbian OS
3. Linux OS
4. Windows
5. Android

6
5. Cluster OS

Cluster systems are similar to parallel systems because both systems use multiple CPUs. The primary
difference is that clustered systems are made up of two or more independent systems linked
together. They have independent computer systems and a shared storage media, and all systems
work together to complete all tasks. All cluster nodes use two different approaches to interact with
one another, like message passing interface (MPI) and parallel virtual machine (PVM).

There are two clusters available to make a more efficient cluster. These are as follows:

1. Software Cluster
2. Hardware Cluster

7
PRACTICAL- 2
BASIC COMMANDS USED IN COMMAND PROMPT

Command Prompt is a command line interpreter application available in most Windows


operating systems. It's used to execute entered commands. Most of those commands
automate tasks via scripts and batch files, perform advanced administrative functions, and
troubleshoot or solve certain kinds of Windows issues.

Command Prompt is officially called Windows Command Processor, but it's also sometimes
referred to as the command shell or cmd prompt, or even by its filename, cmd.exe.

To open Command Prompt, open the Run box (Win+R) and type cmd and hit enter. Or
alternatively, Go on the start menu, and search cmd and select the command prompt and hit
enter.

Once you open the command prompt, the terminal will look like this :

Some of the most basic commands used in command prompt are :

8
1. Start

The start command is used to open a new command line window to run a specified
program or command.
Syntax : C:\Users\Admin>Start

2. Date & Time

The date and time command is used to show or change the current date and time
respectively

Syntax for date command : C:\Users\Admin>date Syntax


for time command : C:\Users\Admin>time

3. Dir

The dir command is used to display a list of files and folders contained inside the
folder that you are currently working in. The dir command also displays other

9
important information like the hard drive's serial number, the total number of files
listed, their combined size, the total amount of free space left on the drive, and
more.
Syntax : C:\Users\Admin\OS lab>dir

4. Tree

The tree command is used to graphically display the folder structure of a specified
drive or path.
Syntax : C:\Users\Admin\OS lab>tree

5. Mkdir(Md)

The mkdir command is used to create a new folder. The md command is the
shorthand version of the mkdir command.

Syntax : C:\Users\Admin\OS lab>mkdir trial

10
6. Rmdir(Rd)

The rmdir command is used to delete an existing or completely empty folder. The
rd command is the shorthand version of the rmdir command.

Syntax : C:\Users\Admin\OS lab>rmdir trial

7. Ping

The ping command sends an Internet Control Message Protocol (ICMP) Echo
Request message to a specified remote computer to verify IP-level connectivity.

Syntax : C:\Users\Admin>ping 172.16.20.138

11
8. Ipconfig

The ipconfig command is used to display detailed IP information for each network
adapter utilizing TCP/IP. The ipconfig command can also be used to release and
renew IP addresses on systems configured to receive them via a DHCP server.

Syntax : C:\Users\Admin> pconfig


i

9. Chdir(Cd)

The chdir command is used to display the drive letter and folder that you are currently
in. Chdir can also be used to change the drive and/or directory that you want to work
in.The cd command is the shorthand version of the chdir command.

Syntax : C:\Users\Admin>cd OS lab

12
10. Cd..

The Cd.. command is used to go back a folder in the current directory.

Syntax : C:\Users\Admin\OS lab>cd..

11. Cd/

The Cd/ command is used to revert to the first parent folder in the current
directory.

Syntax : C:\Users\Admin\OS lab>cd/

12. Chkdsk

The chkdsk command, often referred to as check disk, is used to identify and
correct certain hard drive errors.
Syntax : C:\>chkdsk

13. Help

The help command provides more detailed information on any of the other
Command Prompt or MS-DOS commands.

13
Syntax : C:\>help

14. Copy

The copy command copies one or more files from one location to another. Syntax :

C:\Users\Admin\OS lab>copy trial.txt copy.txt

15. Color

The color command is used to change the colors of the text and background within
the Command Prompt window.

Syntax : C:\Users\Admin\OS lab>color 7

16. Echo

The Echo command is used to print text on the command prompt. Syntax

: C:\Users\Admin\OS lab>Echo hello world

14
PRACTICAL - 3
First Come First Serve(FCFS) Algorithm
What is FCFC ?
The full form of FCFS Scheduling is First Come First Serve Scheduling. FCFS Scheduling algorithm
automatically executes the queued processes and requests in the order of their arrival. It allocates
the job that first arrived in the queue to the CPU, then allocates the second one, and so on.
C Program for implement FCFS Algorithm :
#include<stdio.h>
#include<stdlib.h>
void sortProcesses(int pid[], int at[], int bt[], int ct[], int tat[], int wt[], int n, int c)
{
int t; if(c==0) {
for(int i=0; i<n-1; i++) { for(int
j=0; j<n-1-i; j++) {
if(at[j]>at[j+1]){ t =
at[j];
at[j] = at[j+1]; at[j+1] = t;
t = pid[j];
pid[j] = pid[j+1];
pid[j+1] = t;
t = bt[j];
bt[j] = bt[j+1]; bt[j+1] = t;
}
}
}
}
else{
for(int i=0; i<n-1; i++){ for(int
j=0; j<n-1-i; j++){
if(pid[j]>pid[j+1]){ t =
at[j];
at[j] = at[j+1]; at[j+1] = t;
t = pid[j];
pid[j] = pid[j+1];
pid[j+1] = t;
t = bt[j];
bt[j] = bt[j+1]; bt[j+1] = t;
t = ct[j];

15
ct[j] = ct[j+1];
ct[j+1] = t;
t = tat[j];
tat[j] = tat[j+1];
tat[j+1] = t;
t = wt[j];
wt[j] = wt[j+1]; wt[j+1] =
t;
}
}
}
}
}
void completionTime(int at[], int bt[], int ct[], int n){ ct[0] =
at[0] + bt[0];
for(int i=1; i<n; i++){
if(at[i]<ct[i-1]){
ct[i] = ct[i-1] + bt[i];
}
else{
ct[i] = bt[i] + at[i];
}
}
}
void turnAroundTime(int at[], int ct[], int tat[], int n){ for(int
i=0; i<n; i++){
tat[i] = ct[i] - at[i];
}
}
float averageTAT(int tat[], int n){ float
s=0;
for(int i=0; i<n; i++){ s +=
tat[i];
}
return s/n;
}
void waitingTime(int tat[], int bt[], int wt[], int n){ for(int i=0;
i<n; i++){
wt[i] = tat[i] - bt[i];
}
}
float averageWT(int wt[], int n){ float
s=0;
for(int i=0; i<n; i++){ s +=
wt[i];
}

16
return s/n;
}
void print(int pid[], int at[], int ct[], int tat[], int bt[], int wt[], int n){ sortProcesses(pid,
at, bt, ct, tat, wt, n, 1);
printf("\nPID: Process ID\nAT: Arrival Time\nBT: Burst Time\nCT: Completion Time\nTAT:
Turn Around Time\nWT: Waiting Time\n\n");
printf("PID\tAT\tBT\tCT\tTAT\tWT\n"); for(int
i=0; i<n; i++){
printf("%d\t%d\t%d\t%d\t%d\t%d\n", pid[i], at[i], bt[i], ct[i], tat[i], wt[i]);
}
printf("\nAverage turn around time: %.2f\n", averageTAT(tat, n)); printf("Average
waiting time: %.2f\n", averageWT(wt, n));
}
int main(){
system("clear");
int n;
printf("\nEnter number of processes: ");
scanf("%d", &n);
int pid[n], at[n], bt[n], tat[n], wt[n], ct[n];
printf("\n");
for(int i=0; i<n; i++){
pid[i] = i+1;
printf("Enter Arrival Time and Burst Time for process %d\n", pid[i]); scanf("%d
%d", &at[i], &bt[i]);
}
sortProcesses(pid, at, bt, ct, tat, wt, n, 0);
completionTime(at, bt, ct, n);
turnAroundTime(at, ct, tat, n);
waitingTime(tat, bt, wt, n);
print(pid, at, ct, tat, bt, wt, n);
printf("\n");
return 0;

Output : -- Eg:
Process Arrival Time (AT) Burst Time (BT)

P1 0 11

P2 5 28

P3 12 2

P4 2 10

P5 9 16

17
Practical 4
Shortest Job First (SJF) CPU Scheduling Algorithm
What is SJF Algorithm ?
Shortest Job First (SJF) is a type of disk scheduling algorithm in the operating system in which the
processor executes the job first that has the smallest execution time. In the shortest Job First
algorithm, the processes are scheduled according to the burst time of these processes.
C Program for SJF Alg :
nclude<std o.h>
nclude<stdl b.h>
vo d sortProcesses( nt p d[], nt at[], nt bt[], nt ct[], nt tat[], nt
wt[], nt n, nt c){
nt t; f(c==0)
{

18
for( nt =0; <n-1; ++){
for( nt j=0; j<n-1- ; j++){
f(bt[j]>bt[j+1]){ t =
at[j];
at[j] = at[j+1];
at[j+1] = t;
t = p d[j];
p d[j] = p d[j+1]; p
d[j+1] = t;
t = bt[j];
bt[j] = bt[j+1];
bt[j+1] = t;
}
}
}
nt =0;
wh le(bt[ ] == bt[ +1]){
f(at[ ]>at[ +1]){ t =
at[ ];
at[ ] = at[ +1]; at[
+1] = t;
t = p d[ ];
p d[ ] = p d[ +1]; p d[
+1] = t;
}
++;
}
}
else{
for( nt =0; <n-1; ++){
for( nt j=0; j<n-1- ; j++){
f(p d[j]>p d[j+1]){ t =
at[j];
at[j] = at[j+1];
at[j+1] = t;
t = p d[j];
p d[j] = p d[j+1]; p
d[j+1] = t;
t = bt[j];
bt[j] = bt[j+1];
bt[j+1] = t;
t = ct[j];
ct[j] = ct[j+1];
ct[j+1] = t;
t = tat[j];
tat[j] = tat[j+1]; tat[j+1] =
t;
t = wt[j];
wt[j] = wt[j+1];
wt[j+1] = t;
}
}
}
}
}

19
vo d complet onT me( nt at[], nt bt[], nt ct[], nt n){
ct[0] = at[0] + bt[0];
for( nt =1; <n; ++){
f(at[ ]<ct[ -1]){
ct[ ] = ct[ -1] + bt[ ];
}
else{
ct[ ] = bt[ ] + at[ ];
}
}
}
vo d turnAroundT me( nt at[], nt ct[], nt tat[], nt n){ for(
nt =0; <n; ++){
tat[ ] = ct[ ] - at[ ];
}
}
float averageTAT( nt tat[], nt n){
float s=0;
for( nt =0; <n; ++){ s
+= tat[ ];
}
return s/n;
}
vo d wa t ngT me( nt tat[],nt bt[], nt wt[], nt n){ for(
nt =0; <n; ++){
wt[ ] = tat[ ] - bt[ ];
}
}
float averageWT( nt wt[], nt n){
float s=0;
for( nt =0; <n; ++){ s
+= wt[ ];
}
return s/n;
}
vo d pr nt( nt p d[], nt at[], nt ct[], nt tat[], nt bt[], nt wt[], nt n){
sortProcesses(p d, at, bt, ct, tat, wt, n, 1);
pr ntf("\nPID: Process ID\nAT: Arr val T me\nBT: Burst T me\nCT: Complet on T me\nTAT:
Turn Around T me\nWT: Wa t ng T me\n\n");
pr ntf("PID\tAT\tBT\tCT\tTAT\tWT\n"); for(
nt =0; <n; ++){
pr ntf("%d\t%d\t%d\t%d\t%d\t%d\n",p d[ ],at[ ],bt[ ],ct[ ],tat[ ],wt[ ]);
}
pr ntf("\nAverage turn around t me: %.2f\n", averageTAT(tat, n)); pr
ntf("Average wa t ng t me: %.2f\n", averageWT(wt, n));
}
nt ma n(){
nt n;
system("clear");
pr ntf("\nEnter number of processes: "); scanf("%d",
&n);
pr ntf("\n");
nt p d[n], at[n], bt[n], tat[n], wt[n], ct[n]; for( nt
=0; <n; ++)
{
p d[ ] = +1;
pr ntf("Enter Arr val T me and Burst T me for process %d\n", p d[ ]); scanf("%d
20
%d", &at[ ], &bt[ ]);
}
sortProcesses(p d, at, bt, ct, tat, wt, n, 0); complet onT
me(at, bt, ct, n);
turnAroundT me(at, ct, tat, n); wa t ngT
me(tat, bt, wt, n);
pr nt(p d, at, ct, tat, bt, wt, n); pr ntf("\
n");
return 0;
}

Eg:
Process Arrival Time (AT) Burst Time (BT) C

P1 0 11
P2 5 28
P3 12 2
P4 2 10
P5 9 16

Output : --

21
Practical – 5
Priority Scheduling Algorithm
What is Priority Scheduling ?
The Operating System has a major function of deciding the order in which processes / tasks will have
access to the processor and the amount of processing time each of these processes or tasks will
have. This function of the operating system is termed as process scheduling.
In priority scheduling in OS, processes are executed on the basis of their priority. The jobs / processes
with higher priority are executed first. Naturally, you might want to know how priority of processes is
decided.
Priority of processes depends some factors such as:
Time limit
Memory requirements of the process
Ratio of average I/O to average CPU burst time
Program for Priority Scheduling
nclude <std o.h> nt
ma n(){
nt bt[20], p[20], wt[20], tat[20], pr[20], , j, n, total = 0, pos, temp, avg_wt,
avg_tat;
pr ntf("Enter Total Number of Process:"); scanf("%d", &n);
pr ntf("\nEnter Burst T me and Pr or ty\n"); for ( =
0; < n; ++){
pr ntf("\nP[%d]\n", + 1);
pr ntf("Burst T me:");
scanf("%d", &bt[ ]);
pr ntf("Pr or ty:");
scanf("%d", &pr[ ]); p[ ]
= + 1;
}
for ( = 0; < n; ++){
pos = ;
for (j = + 1; j < n; j++){ f
(pr[j] < pr[pos])
pos = j;
}
temp = pr[ ]; pr[ ] =
pr[pos]; pr[pos] =
temp; temp = bt[ ]; bt[
] = bt[pos]; bt[pos] =
temp; temp = p[ ];
p[ ] = p[pos];

22
p[pos] = temp;
}
wt[0] = 0;
for ( = 1; < n; ++){
wt[ ] = 0;
for (j = 0; j < ; j++)
wt[ ] += bt[j];
total += wt[ ];
}
avg_wt = total / n;
total = 0;
pr ntf("\nProcess\t Burst T me \tWa t ng T me\tTurnaround T me"); for
( = 0; < n; ++){
tat[ ] = bt[ ] + wt[ ];
total += tat[ ];
pr ntf("\nP[%d]\t\t %d\t\t %d\t\t\t%d", p[ ], bt[ ], wt[ ],
tat[ ]);
}
avg_tat = total / n;
pr ntf("\n\nAverage Wa t ng T me=%d", avg_wt);
pr ntf("\nAverage Turnaround T me=%d\n", avg_tat); return
0;
}

Eg :

Process Arrival Time (AT) Burst Time (BT) Priority

P1 0 11 2

P2 5 28 0

P3 12 2 3

P4 2 10 1

P5 9 16 4

23
Output :--

Practical – 6
Round Robin Scheduling Algorithm
What is Round Robin SA ?
Round Robin Scheduling is a CPU scheduling algorithm in which each process is executed for a
fixed time slot. Since the resources are snatched after the time slot, round robin is preemptive.

Round Robin Scheduling Algorithm :


Step 1: Start the Program.
Step 2: Input the number of processes.
Step 3: Input the burst time and arrival time of each process and the limit of the time slot.
Step 4: Push all processes into the ready queue according to their arrival time. Then execute each
process upto time slot and push left over process in queue again for execution.
Step 5: After a process is completely executed, print its turn around time and waiting time.

24
Program in C of Round Robin Scheduling :
nclude<std o.h>
nt ma n(){
nt n;
pr ntf("Enter Total Number of Processes:"); scanf("%d",
&n);
nt wa t_t me = 0, ta_t me = 0, arr_t me[n], burst_t me[n], temp_burst_t
me[n];
nt x = n;
for( nt = 0; < n; ++){
pr ntf("Enter Deta ls of Process %d \n", + 1);
pr ntf("Arr val T me: ");
scanf("%d", &arr_t me[ ]);
pr ntf("Burst T me: ");
scanf("%d", &burst_t me[ ]);
temp_burst_t me[ ] = burst_t me[ ];
}
nt t me_slot;
pr ntf("Enter T me Slot:"); scanf("%d",
&t me_slot);
nt total = 0, counter = 0, ;
pr ntf("Process ID Burst T me Turnaround T me Wa t ng
T me\n");
for(total=0, = 0; x!=0; ){
f(temp_burst_t me[ ] <= t me_slot && temp_burst_t me[ ] > 0){ total =
total + temp_burst_t me[ ];
temp_burst_t me[ ] = 0;
counter=1;
}
else f(temp_burst_t me[ ] > 0){
temp_burst_t me[ ] = temp_burst_t me[ ] – t me_slot; total
+= t me_slot;
}
f(temp_burst_t me[ ]==0 && counter==1){ x--;
pr ntf(“\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d”, +1,
burst_t me[ ],
total-arr_t me[ ], total-arr_t me[ ]-burst_t me[ ]); wa t_t
me = wa t_t me+total-arr_t me[ ]-burst_t me[ ];
ta_t me += total -arr_t me[ ];
counter =0;
}
f( ==n-1){
=0;
}
else f(arr_t me[ +1]<=total){
25
++;
}
else{
=0;
}
}
float average_wa t_t me = wa t_t me * 1.0 / n; float
average_turnaround_t me = ta_t me * 1.0 / n;
pr ntf(“\nAverage Wa t ng T me:%f”, average_wa t_t me);
pr ntf(“\nAvg Turnaround T me:%f”, average_turnaround_t me); return 0;
}
Output : -- Eg
:
Process Arrival Time (AT) Burst Time (BT)
P1 0 10
P2 1 9
P3 2 12
P4 3 6
P5 4 5

Practical – 7
FIFO Page Replacement Algorithm
What is FIFO PRA ?
FIFO algorithm replaces the oldest (First) page which has been present for the longest time in the
main memory. In simple words, When a new page comes in from secondary memory to main
memory, It selects the front of the queue which is the oldest page present, and removes it
Algorithm for FIFO PRA :

26
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Forma queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process
Program in C for FIFO Page Replacement Algorithm :
nclude <std o.h>
nt ma n(){
nt , j, n, a[50], frame[10], no, k, ava l, count = 0; pr
ntf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d", &n);
pr ntf("\n ENTER THE PAGE NUMBER :\n");
for ( = 1; <= n; ++)
scanf("%d", &a[ ]);
pr ntf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d", &no);
for ( = 0; < no; ++)
frame[ ] = -1;
j = 0;
pr ntf("\tref str ng\t page frames\n"); for
( = 1; <= n; ++){
pr ntf("%d\t\t", a[ ]);
ava l = 0;
for (k = 0; k < no; k++) f
(frame[k] == a[ ])
ava l = 1; f
(ava l == 0){
frame[j] = a[ ]; j =
(j + 1) % no;
count++;
for (k = 0; k < no; k++)
pr ntf("%d\t", frame[k]);
}
pr ntf("\n");
}
pr ntf("Page Fault Is %d", count);
return 0;}
Output : -- Eg : ENTER THE NUMBER OF PAGES: 20
ENTER THE PAGE NUMBER : 70120304230321201701
27
ENTER THE NUMBER OF FRAMES :3

28
Practical – 8
Optimal Page Replacement Algorithm
What is Optimal PRA ?
As we know, when a process is executed in our system, the operating system divides this
process into small parts known as 'pages'. And these pages are loaded into secondary memory
known as RAMs. Also, the secondary memory is divided into several frames in which the pages
are loaded. The size of the page is always equal to the size of the frames. When a system calls a
page and it is not available in the frame, "page fault" occurs. The optimal page replacement
algorithm is used to reduce these page faults. It uses the principle that- "when a page is called
by the system and it is not available in the frames, the frame which is not in demand for the
longest future time is replaced by the new page".
Program in C for Optimal Page Replacement Algorithm :
nclude <std o.h>
nt ma n(){
nt no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2,
flag3, , j, k, pos, max, faults = 0;
pr ntf("Enter number of frames: ");
scanf("%d", &no_of_frames);
pr ntf("Enter number of pages: ");
scanf("%d", &no_of_pages);
pr ntf("Enter page reference str ng: "); for
( = 0; < no_of_pages; ++ ){
scanf("%d", &pages[ ]);
}
for ( = 0; < no_of_frames; ++ )

29
{

30
frames[ ] = -1;
}
for ( = 0; < no_of_pages; ++ ){
flag1 = flag2 = 0;
for (j = 0; j < no_of_frames; ++j){ f
(frames[j] == pages[ ]){
flag1 = flag2 = 1;
break;
}
}
f (flag1 == 0){
for (j = 0; j < no_of_frames; ++j){ f
(frames[j] == -1){
faults++;
frames[j] = pages[ ];
flag2 = 1;
break;
}
}
}
f (flag2 == 0){
flag3 = 0;
for (j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for (k = + 1; k < no_of_pages; ++k){ f
(frames[j] == pages[k]){
temp[j] = k; break;
}
}
}
for (j = 0; j < no_of_frames; ++j){ f
(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
f (flag3 == 0){ max
= temp[0]; pos
= 0;
for (j = 1; j < no_of_frames; ++j){ f
(temp[j] > max){
max = temp[j];
pos = j;
}
31
}
}
frames[pos] = pages[ ]; faults+
+;
}
pr ntf("\n");
for (j = 0; j < no_of_frames; ++j){ pr
ntf("%d\t", frames[j]);
}
}
pr ntf("\n\nTotal Page Faults = %d", faults); return
0;
}
Output : --
Eg : ENTER THE NUMBER OF PAGES: 20
ENTER THE PAGE NUMBER : 70120304230321201701
ENTER THE NUMBER OF FRAMES :3

Practical – 9
LRU Page Replacement Algorithm
What is LRU PRA ?

In this algoíithm, we íeplace the element which is the cuííent least íecently used element in
the cuííent stack.

ľhat is, when we look to the left of the table, that we have cíeated we choose the fuítheí most
page to get íeplaced.

LRU Page Replacement Algorithm:


1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
32
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
Program in C for LRU PRA
nclude <std o.h> nt
ma n(){
nt q[20], p[50], c = 0, c1, d, f, , j, k = 0, n, r, t, b[20], c2[20];
pr ntf("Enter no of pages:");
scanf("%d", &n);
pr ntf("Enter the reference str ng:"); for
( = 0; < n; ++)
scanf("%d", &p[ ]);
pr ntf("Enter no of frames:");
scanf("%d", &f);
q[k] = p[k];
pr ntf("\n\t%d\n", q[k]); c+
+;
k++;
for ( = 1; < n; ++){
c1 = 0;
for (j = 0; j < f; j++){ f
(p[ ] != q[j])
c1++;
}
f (c1 == 1){ c+
+;
f (k < f){
q[k] = p[ ]; k++;
for (j = 0; j < k; j++) pr
ntf("\t%d", q[j]);
pr ntf("\n");
}
else{
for (r = 0; r < f; r++){
c2[r] = 0;
for (j = - 1; j < n; j--){
f (q[r] != p[j]) c2[r]++;
else
break;
}

33
}
for (r = 0; r < f; r++)
b[r] = c2[r];
for (r = 0; r < f; r++){
for (j = r; j < f; j++){ f
(b[r] < b[j]){
t = b[r]; b[r]
= b[j]; b[j] =
t;
}
}
}
for (r = 0; r < f; r++){ f
(c2[r] == b[0])
q[r] = p[ ];
pr ntf("\t%d", q[r]);
}
pr ntf("\n");
}
}
}
pr ntf("\nThe no of page faults s %d", c);
}
Output : --
Eg : ENTER THE NUMBER OF PAGES: 20
ENTER THE PAGE NUMBER : 70120304230321201701
ENTER THE NUMBER OF FRAMES :3

Practical – 10
Dining Philospher’s Problem ( Deadlock Problem)
What is Dining Philospher’s Problem ?

It is the classical problem of synchronization which says that Five philosophers are sitting around

34
a circular table and their job is to think and eat alternatively. A bowl of noodles is

35
placed at the center of the table along with five chopsticks for each of the philosophers. To eat a
philosopher needs both their right and a left chopstick. A philosopher can only eat if both
immediate left and right chopsticks of the philosopher is available. In case if both immediate left
and right chopsticks of the philosopher are not available then the philosopher puts down their
(either left or right) chopstick and starts thinking again.

Program in C for Dining Philospher’s Problem

nclude <std o.h>

def ne n 5

nt compltedPh lo = 0, ;

struct fork

{ nt taken;}

ForkAv l[n];

struct ph losp

{ nt left,r ght;} Ph

lostatus[n];

vo d goForD nner( nt ph lID)

{ f (Ph lostatus[ph lID].left == 10 && Ph lostatus[ph lID].r ght == 10) pr

ntf("%d No Ph losopher has completed h s d nner\n", ph lID + 1);

else f (Ph lostatus[ph lID].left == 1 && Ph lostatus[ph lID].r ght == 1)

{pr ntf("%d No Ph losopher has completed h s d nner\n", ph lID + 1); Ph

lostatus[ph lID].left = Ph lostatus[ph lID].r ght = 10;

nt otherFork = ph lID - 1;

f (otherFork == -1)

otherFork = (n - 1);

ForkAv l[ph lID].taken = ForkAv l[otherFork].taken = 0;

pr ntf("Ph losopher %d released fork %d and fork %d\n", ph lID + 1, ph lID

36
+ 1, otherFork + 1);

compltedPh lo++;}

else f (Ph lostatus[ph lID].left == 1 && Ph lostatus[ph lID].r ght == 0)

{ f (ph lID == (n - 1))

{ f (ForkAv l[ph lID].taken == 0)

{ForkAv l[ph lID].taken = Ph lostatus[ph lID].r ght = 1;

pr ntf("Fork %d taken by ph losopher %d\n", ph lID + 1, ph lID

+ 1);}

else

{pr ntf("Ph losopher %d s wa t ng for fork %d\n", ph lID + 1, ph lID + 1);}}

else

{ nt dupph lID = ph lID; ph

lID -= 1;

f (ph lID == -1)

ph lID = (n - 1);

f (ForkAv l[ph lID].taken == 0)

{ForkAv l[ph lID].taken = Ph lostatus[dupph lID].r ght = 1; pr

ntf("Fork %d taken by Ph losopher %d\n", ph lID + 1, dupph lID +

1);}

else

{pr ntf("Ph losopher %d s wa t ng for Fork %d\n", dupph lID + 1, ph

lID + 1);}}}

else f (Ph lostatus[ph lID].left == 0)

{ f (ph lID == (n - 1))

37
{ f (ForkAv l[ph lID - 1].taken == 0)

{ForkAv l[ph lID - 1].taken = Ph lostatus[ph lID].left = 1; pr

ntf("Fork %d taken by ph losopher %d\n", ph lID, ph lID + 1);}

else

{pr ntf("Ph losopher %d s wa t ng for fork %d\n", ph lID + 1, ph

lID);}}

else

{ f (ForkAv l[ph lID].taken == 0)

{ForkAv l[ph lID].taken = Ph lostatus[ph lID].left = 1; pr

ntf("Fork %d taken by Ph losopher %d\n", ph lID + 1, ph lID +

1);}

else

{pr ntf("Ph losopher %d s wa t ng for Fork %d\n", ph lID + 1, ph lID

+ 1);}}}

else{}}

nt ma n()

{for ( = 0; < n; ++)

ForkAv l[ ].taken = Ph lostatus[ ].left = Ph lostatus[ ].r ght = 0; wh le

(compltedPh lo < n)

{for ( = 0; < n; ++)

goForD nner( );

pr ntf("\nT ll now num of ph losophers completed d nner are %d\n\n",

compltedPh lo);}

return 0;}

38
Output : --

Practical – 10
Banking Problem ( Deadlock Problem)
What is Banker’s Alg ?

39
Banker’s algorithm is named so because it is used in the banking system to check whether a
loan can be sanctioned to a person or not. Suppose there are n number of account holders in
a bank and the total sum of their money is S. If a person applies for a loan then the bank first
subtracts the loan amount from the total money that the bank has and if the remaining
amount is less than S then only the loan is sanctioned. It is done because if all the account
holders come to withdraw their money then the bank can easily do it.
Banker’s Algorithm :
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe
state
C Program for Banker's Algorithm :
nclude <std o.h>
nt ma n(){
nt n, m, , j, k; n
= 5;
m = 3;
nt alloc[5][3] = { { 0, 1, 0 },
{ 2, 0, 0 },
{ 3, 0, 2 },
{ 2, 1, 1 },
{ 0, 0, 2 } };
nt max[5][3] = { { 7, 5, 3 },
{ 3, 2, 2 },
{ 9, 0, 2 },
{ 2, 2, 2 },
{ 4, 3, 3 } };
nt ava l[3] = { 3, 3, 2 };
nt f[n], ans[n], nd = 0;
for (k = 0; k < n; k++) {
f[k] = 0;}
nt need[n][m];
40
for ( = 0; < n; ++) {
for (j = 0; j < m; j++)

41
need[ ][j] = max[ ][j] - alloc[ ][j];
}
nt y = 0;
for (k = 0; k < 5; k++) {
for ( = 0; < n; ++) {
f (f[ ] == 0) {
nt flag = 0;
for (j = 0; j < m; j++) {
f (need[ ][j] > ava l[j]){ flag
= 1;
break;
}
}
f (flag == 0) { ans[
nd++] = ;
for (y = 0; y < m; y++)
ava l[y] += alloc[ ][y]; f[
] = 1;
}
}
}
}
nt flag = 1;
for( nt =0; <n; ++){
f(f[ ]==0){
flag=0;
pr ntf("The follow ng system s not safe");
break;
}
}
f(flag==1){
pr ntf("Follow ng s the SAFE Sequence\n");
for ( = 0; < n - 1; ++)
pr ntf(" P%d ->", ans[ ]);
pr ntf(" P%d", ans[n - 1]);
}
return (0);
}

Output : --

42

You might also like