0% found this document useful (0 votes)
4 views34 pages

Os Lab

The document outlines a laboratory manual for a Computer Science Engineering course at Avanthi's St. Theressa Institute, focusing on CPU scheduling algorithms. It includes detailed experiments on First Come First Serve, Shortest Job First, Round Robin, and Priority scheduling, along with algorithms, source codes, and sample inputs/outputs for each. The manual is prepared by Mr. Durga Prasad, Assistant Professor, and serves as a practical guide for students in the Operating Systems course.

Uploaded by

prasadavanthi2
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)
4 views34 pages

Os Lab

The document outlines a laboratory manual for a Computer Science Engineering course at Avanthi's St. Theressa Institute, focusing on CPU scheduling algorithms. It includes detailed experiments on First Come First Serve, Shortest Job First, Round Robin, and Priority scheduling, along with algorithms, source codes, and sample inputs/outputs for each. The manual is prepared by Mr. Durga Prasad, Assistant Professor, and serves as a practical guide for students in the Operating Systems course.

Uploaded by

prasadavanthi2
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

AVANTHI’S

ST. THERESSA INSTITUTE OF ENGG. & TECH


GARIVIDI
Vizianagaram Dist (AP)

Estd 2001

OPERATING SYSTEM

R23 CSE (AIML)

SEMISTER: III–II

BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE ENGINEERING

Prepared By
Mr. Durga Prasad
Assistant Professor, CSE
AVANTHI’S ST. THERESSA INSTITUTE OF ENGG. & TECH
GARIVIDI
Vizianagaram Dist (AP)

Estd 2001

CERTIFICATE

This is to certify that, it is the bonafide record of the work done in ……………….......

………………………………………... laboratory by Mr./MS…….................................

………………………bearing [Link]. /roll no………………………… of……….........

………………................................course during………………….……………………….

Total Number of Total Number of


Experiments held: …………... Experiments Done:
…………

LAB INCHARGE HEAD OF THE EPARTMENT

2
III-II CSE(AIML) OPERATING SYSTEM
EXPERIMENT NO.1

CPU SCHEDULINGALGORITHMS

A). FIRST COME FIRST SERVE:

AIM: To write a c program to simulate the CPU scheduling algorithm First Come First
Serve (FCFS)

DESCRIPTION:

To calculate the average waiting time using the FCFS algorithm first the
waiting time of the first process is kept zero and the waiting time of the second
process is the burst time of the first process and the waiting time of the third process
is the sum of the burst times of the first and the second process and so on. After
calculating all the waiting times the average waiting time is calculated as the average
of all the waiting times. FCFS mainly says first come first serve the algorithm which
came first will be served first.

ALGORITHM:

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 name and the 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 (n) = waiting time (n-1) + Burst time (n-1) b).
Turnaround time (n)= waiting time(n)+Burst time(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

SOURCE CODE:

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 1


III-II CSE(AIML) OPERATING SYSTEM

#include<stdio.h>
#include<conio.h> main()
{ int bt[20], wt[20],
tat[20], i, n;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n); for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg =
bt[0];
for(i=1;i<n;i++)
{ wt[i] = wt[i-1]
+bt[i-1]; tat[i] =
tat[i-1] +bt[i]; wtavg
= wtavg + wt[i];
tatavg = tatavg +
tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}

INPUT
Enter the number of processes -- 3
Enter Burst Time for Process 0 -- 24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3

OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND
TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time-- 17.000000
Average Turnaround Time -- 27.000000

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 2


III-II CSE(AIML) OPERATING SYSTEM
B). SHORTEST JOB FIRST:

AIM: To write a program to stimulate the CPU scheduling algorithm Shortest job first
(Non- Preemption)

DESCRIPTION:

To calculate the average waiting time in the shortest job first algorithm the
sorting of the process based on their burst time in ascending order then calculate the
waiting time of each process as the sum of the bursting times of all the process
previous or before to that process.

ALGORITHM:

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: Start the Ready Q according the shortest Burst time by sorting according to lowest
to highest burst time.
Step 5: Set the waiting time of the first process as ‗0‘ and its turnaround time as its burst
time.
Step 6: Sort the processes names based on their Burt
time Step 7: For each process in the ready queue,
calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n)
Step 8: Calculate
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number of process Step 9: Stop the
process
SOURCE CODE :

#include<stdi
o.h>
#include<con
io.h> main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp; float
wtavg, tatavg; clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i
]=
i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 3


III-II CSE(AIML) OPERATING SYSTEM
}
for(i=0;i<n;i
++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=b
t[i];
bt[i]=bt
[k];
bt[k]=temp;

temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0]; for(i=1;i<n;i++)
{
wt[i] = wt[i-1]
+bt[i-1]; tat[i] =
tat[i-1] +bt[i];
wtavg = wtavg +
wt[i]; tatavg =
tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n); getch();}
INPUT
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3
OUTPUT
PROCESS BURST WAITING TURNARO
TIME TIME UND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000
Average Turnaround Time -- 13.000000

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 4


III-II CSE(AIML) OPERATING SYSTEM
C). ROUND ROBIN:

AIM: To simulate the CPU scheduling algorithm round-robin.

DESCRIPTION:

To aim is to calculate the average waiting time. There will be a time slice, each
process should be executed within that time-slice and if not it will go to the
waiting state so first check whether the burst time is less than the time-slice. If it is
less than it assign the waiting time to the sum of the total times. If it is greater than
the burst-time then subtract the time slot from the actual burst time and increment it
by time-slot and the loop continues until all the processes are completed.

ALGORITHM:
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) Turnaround 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
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number of process Step 8: Stop
the process
SOURCE CODE
#include<stdi
o.h> main(){
int
i,j,n,bu[10],wa[10],tat[10],t,ct[10],m
ax; float awt=0,att=0,temp=0;clrscr();
printf("Enter the no of processes --
"); scanf("%d",&n);for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);ma
x=bu[0];
for(i=1;i<n;i++)

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 5


III-II CSE(AIML) OPERATING SYSTEM
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j+
+) for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t){
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[
i]=
0;
}el
se
{
bu[i]=bu[i]-t;
temp=temp+t;
}for(i=0;i<n;i++){wa[i]=tat[i]ct[i]; att+=tat[i];
awt+=wa[i];}printf("\nThe Average Turnaround time
is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND
TIME\n");for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
getch();}

INPUT:

Enter the no of processes – 3


Enter Burst Time for process 1 – 24
Enter Burst Time for process 2 -- 3
Enter Burst Time for process 3 – 3
Enter the size of time slice – 3

OUTPUT:
PROCESS BURST TIME WAITING TIME TURNAROUNDTIME
1 24 6 30
2 3 4 7
3 3 7 10
The Average Turnaround time is – 15.666667 The
Average Waiting time is ------------ 5.666667

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 6


III-II CSE(AIML) OPERATING SYSTEM
D). PRIORITY:

AIM: To write a c program to simulate the CPU scheduling priority algorithm.

DESCRIPTION:

To calculate the average waiting time in the priority algorithm, sort the burst
times according to their priorities and then calculate the average waiting time of the
processes. The waiting time of each process is obtained by summing up the burst
times of all the previous processes.

ALGORITHM:

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: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as ‗0‘ and its burst time as its turnaround
time Step 6: Arrange the processes based on process priority Step 7: For each
process in the Ready Q calculate Step 8: for each process in the Ready Q calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n)
Step 9: Calculate
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number of process Print the results
in an order. Step10: Stop
SOURCE CODE:
#include<std
io.h> main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp; float wtavg,
tatavg; clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i); scanf("%d
%d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++
)
for(k=i+1;k<n;
k++) if(pri[i] >
pri[k]){
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 7


III-II CSE(AIML) OPERATING SYSTEM
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] =
bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];

wtavg = wtavg + wt[i];


tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING
TIME\tTURNAROUND TIME"); for(i=0;i<n;i++) printf("\n%d \t\t %d \t\t %d \t\t
%d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]); printf("\nAverage Waiting Time is ---
%f",wtavg/n); printf("\nAverage
Turnaround Time is --- %f",tatavg/n);
getch();}

INPUT 3 TURNARO
Enter the number of processes -- 5 1 UND TIME
Enter the Burst Time & Priority of Process 0 --- 10 4 1
Enter the Burst Time & Priority of Process 1 --- 1 5 6
Enter the Burst Time & Priority of Process 2 --- 2 2 16
Enter the Burst Time & Priority of Process 3 --- 1 18
Enter the Burst Time & Priority of Process 4 --- 5 19
WAITIN

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 8


III-II CSE(AIML) OPERATING SYSTEM
OUTPUT BURST TIME G TIME
PROCESS PRIORITY 0
1 1
1 1 5 6
4 2 10 16
0 3 2 18
2 4 1
3 5 8.200000
Average Waiting Time is --- --- 1
Average Turnaround Time is 2.000000

Experiment – 2
Implementation of fork(), wait(), exec() and exit() System Calls

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 9


III-II CSE(AIML) OPERATING SYSTEM
Aim

To write a C program to demonstrate the working of the system calls fork system call, wait system call, exec
system call, and exit system call in a Unix/Linux operating system.

Description

In multiprogramming systems, multiple processes execute simultaneously.


The operating system provides system calls to manage these processes.

fork()

• Used to create a child process.


• The child process is a duplicate of the parent process.

wait()

• The parent process waits for the child process to finish execution.

exec()

• Used by a process to replace its memory with a new program.

exit()

• Terminates a process and returns control to the operating system.

These system calls help the OS manage process creation, execution, synchronization, and termination.

Program
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

intmain()
{
pid_tpid;

pid=fork(); // create child process

if(pid<0)
{
printf("Fork failed\n");
exit(1);
}

elseif(pid==0)
{
// Child process
printf("Child Process\n");
printf("Child PID: %d\n", getpid());

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 10


III-II CSE(AIML) OPERATING SYSTEM
printf("Executing 'ls' command using exec()\n");

execlp("ls","ls",NULL); // execute ls command

exit(0);
}

else
{
// Parent process
printf("Parent Process\n");
printf("Parent PID: %d\n", getpid());

wait(NULL); // wait for child to complete

printf("Child process finished\n");


printf("Parent exiting\n");

exit(0);
}

return0;
}

Compilation
gcc fork_program.c
./[Link]

Sample Output
Parent Process
Parent PID: 2456

Child Process
Child PID: 2457
Executing 'ls' command using exec()

[Link]
fork_program.c

Child process finished


Parent exiting

Experiment – 3
Simulation of Multiprogramming with Fixed Tasks (MFT) and Variable Tasks (MVT)

Aim

To write a C program to simulate multiprogramming memory management using Multiprogramming with a


Fixed number of Tasks and Multiprogramming with a Variable number of Tasks.

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 11


III-II CSE(AIML) OPERATING SYSTEM

Description

Multiprogramming allows multiple processes to reside in the main memory simultaneously so that CPU
utilization increases and system efficiency improves.

MFT (Multiprogramming with Fixed number of Tasks)

In this method, the memory is divided into fixed size partitions. Each partition can hold only one process. If
the process size is smaller than the partition size, internal fragmentation occurs.

MVT (Multiprogramming with Variable number of Tasks)

In this method, memory partitions are created dynamically depending on the size of the processes. Memory
is allocated according to the requirement of the process, reducing internal fragmentation but possibly
causing external fragmentation.

Program
#include<stdio.h>

intmain()
{
intms, ps, nop, np, rem, i;

printf("\n------MFT------\n");

printf("Enter total memory size: ");


scanf("%d",&ms);

printf("Enter block size: ");


scanf("%d",&ps);

np=ms/ps;

printf("Number of partitions available: %d\n",np);

printf("Enter number of processes: ");


scanf("%d",&nop);

if(nop>np)
{
printf("Memory is Full\n");
}
else
{
rem=ms- (nop*ps);
printf("Memory allocated for %d processes\n",nop);
printf("Remaining memory: %d\n",rem);
}

printf("\n------MVT------\n");

intprocess[10], total=0;

printf("Enter number of processes: ");

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 12


III-II CSE(AIML) OPERATING SYSTEM
scanf("%d",&nop);

for(i=0;i<nop;i++)
{
printf("Enter memory required for process %d: ",i+1);
scanf("%d",&process[i]);
total=total+process[i];
}

if(total<=ms)
{
printf("Memory allocated for all processes\n");
printf("Remaining memory: %d\n", ms-total);
}
else
{
printf("Memory is not sufficient for all processes\n");
}

return0;
}

Sample Output
------MFT------
Enter total memory size: 1000
Enter block size: 200
Number of partitions available: 5
Enter number of processes: 4
Memory allocated for 4 processes
Remaining memory: 200

------MVT------
Enter number of processes: 3
Enter memory required for process 1: 200
Enter memory required for process 2: 300
Enter memory required for process 3: 100
Memory allocated for all processes
Remaining memory: 400

Experiment – 4
Simulation of Banker’s Algorithm for Deadlock Avoidance

Aim

To write a C program to simulate Banker's Algorithm for Deadlock Avoidance in an operating system.

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 13


III-II CSE(AIML) OPERATING SYSTEM
Description

Deadlock is a situation in which a set of processes are blocked because each process is holding a resource
and waiting for another resource held by another process.

Banker’s Algorithm is a deadlock avoidance algorithm used by operating systems to ensure that the
system will always remain in a safe state.

The algorithm works by checking whether allocating requested resources to a process will lead the system to
an unsafe state.

Steps of Banker’s Algorithm

1. Calculate the Need matrix using the formula:


Need = Max − Allocation
2. Find a process whose Need is less than or equal to Available resources.
3. If such a process exists, allocate resources and mark the process as completed.
4. Add its allocated resources back to the Available resources.
5. Repeat until all processes finish or no safe sequence is found.

If all processes can complete successfully, the system is in a safe state.

Program
#include<stdio.h>

intmain()
{
intn, m, i, j, k;
intallocation[10][10], max[10][10], need[10][10];
intavailable[10], finish[10], safeSequence[10];

printf("Enter number of processes: ");


scanf("%d",&n);

printf("Enter number of resources: ");


scanf("%d",&m);

printf("Enter Allocation Matrix:\n");


for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&allocation[i][j]);

printf("Enter Max Matrix:\n");


for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);

printf("Enter Available Resources:\n");


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

for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j] =max[i][j] -allocation[i][j];

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 14


III-II CSE(AIML) OPERATING SYSTEM
for(i=0;i<n;i++)
finish[i] =0;

intcount=0;

while(count<n)
{
for(i=0;i<n;i++)
{
if(finish[i] ==0)
{
intflag=0;

for(j=0;j<m;j++)
{
if(need[i][j] >available[j])
{
flag=1;
break;
}
}

if(flag==0)
{
safeSequence[count++] =i;

for(k=0;k<m;k++)
available[k] +=allocation[i][k];

finish[i] =1;
}
}
}
}

printf("Safe Sequence is: ");

for(i=0;i<n;i++)
printf("P%d ", safeSequence[i]);

return0;
}

Output
Enter number of processes: 5
Enter number of resources: 3

Enter Allocation Matrix:


010
200
302
211
002

Enter Max Matrix:


753

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 15


III-II CSE(AIML) OPERATING SYSTEM
322
902
222
433

Enter Available Resources:


332

Safe Sequence is: P1 P3 P4 P0 P2

Experiment – 5
Simulation of Deadlock Detection Algorithm

Aim

To write a C program to simulate Deadlock Detection Algorithm for detecting deadlocks in an operating
system.

Description

A deadlock is a situation where a group of processes are blocked because each process is holding a resource
and waiting for another resource held by another process.

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 16


III-II CSE(AIML) OPERATING SYSTEM
The Deadlock Detection Algorithm is used by the operating system to check whether a deadlock has
occurred. It analyzes the system state using the following data structures:

• Allocation Matrix – Resources currently allocated to processes.


• Request Matrix – Resources requested by processes.
• Available Vector – Resources currently available in the system.

Steps of the Algorithm

1. Initialize all processes as unfinished.


2. Find a process whose request is less than or equal to available resources.
3. If found, mark it finished and release its resources.
4. Repeat the process until all processes are finished.
5. If some processes remain unfinished, a deadlock exists.

Program
#include<stdio.h>

intmain()
{
intallocation[10][10], request[10][10], available[10];
intfinish[10];
inti,j,k,n,m,flag;

printf("Enter number of processes: ");


scanf("%d",&n);

printf("Enter number of resources: ");


scanf("%d",&m);

printf("Enter Allocation Matrix:\n");


for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&allocation[i][j]);
}
}

printf("Enter Request Matrix:\n");


for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&request[i][j]);
}
}

printf("Enter Available Resources:\n");


for(i=0;i<m;i++)
{
scanf("%d",&available[i]);
}

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

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 17


III-II CSE(AIML) OPERATING SYSTEM
finish[i]=0;

for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
flag=0;

for(j=0;j<m;j++)
{
if(request[i][j] >available[j])
{
flag=1;
break;
}
}

if(flag==0)
{
for(j=0;j<m;j++)
available[j]+=allocation[i][j];

finish[i]=1;
}
}
}
}

flag=0;

for(i=0;i<n;i++)
{
if(finish[i]==0)
{
printf("Deadlock detected at process P%d\n",i);
flag=1;
}
}

if(flag==0)
printf("No Deadlock Detected\n");

return0;
}

Sample Output
Enter number of processes: 3
Enter number of resources: 3

Enter Allocation Matrix:


101
110
010

Enter Request Matrix:

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 18


III-II CSE(AIML) OPERATING SYSTEM
001
101
110

Enter Available Resources:


110

No Deadlock Detected

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 19


III-II CSE(AIML) OPERATING SYSTEM

Experiment – 6
Implementation of Page Replacement Algorithms

(a) FIFO Page Replacement Algorithm


Aim

To write a C program to implement the FIFO Page Replacement Algorithm.

Description

FIFO replaces the page that has been in memory for the longest time. When a page fault occurs and memory
is full, the oldest page is replaced.

Program
#include<stdio.h>

intmain()
{
intpages[30], frame[10];
intn, f, i, j, k=0, fault=0, found;

printf("Enter number of pages: ");


scanf("%d",&n);

printf("Enter page reference string:\n");


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

printf("Enter number of frames: ");


scanf("%d",&f);

for(i=0;i<f;i++)
frame[i]=-1;

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

for(j=0;j<f;j++)
{
if(frame[j]==pages[i])
{
found=1;
break;
}
}

if(found==0)
{
frame[k]=pages[i];
k=(k+1)%f;
fault++;
}

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 20


III-II CSE(AIML) OPERATING SYSTEM
}

printf("Total Page Faults = %d",fault);

return0;
}
Output
Enter number of pages: 12
Enter page reference string:
123412512345
Enter number of frames: 3
Total Page Faults = 9
Result

Thus the program for FIFO Page Replacement Algorithm was executed successfully.

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 21


III-II CSE(AIML) OPERATING SYSTEM

(b) LRU Page Replacement Algorithm


Aim

To write a C program to implement the Least Recently Used Page Replacement.

Description

LRU replaces the page that has not been used for the longest period of time.

Program
#include<stdio.h>

intmain()
{
intpages[30], frame[10], time[10];
intn, f, i, j, pos, fault=0, counter=0;

printf("Enter number of pages: ");


scanf("%d",&n);

printf("Enter page reference string:\n");


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

printf("Enter number of frames: ");


scanf("%d",&f);

for(i=0;i<f;i++)
frame[i]=-1;

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

for(j=0;j<f;j++)
{
if(frame[j]==pages[i])
{
counter++;
time[j]=counter;
found=1;
break;
}
}

if(found==0)
{
intmin=time[0];
pos=0;

for(j=1;j<f;j++)
{
if(time[j]<min)
{

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 22


III-II CSE(AIML) OPERATING SYSTEM
min=time[j];
pos=j;
}
}

frame[pos]=pages[i];
counter++;
time[pos]=counter;
fault++;
}
}

printf("Total Page Faults = %d",fault);

return0;
}
Output
Enter number of pages: 12
Enter page reference string:
123412512345
Enter number of frames: 3
Total Page Faults = 8
Result

Thus the program for LRU Page Replacement Algorithm was executed successfully.

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 23


III-II CSE(AIML) OPERATING SYSTEM

(c) Optimal Page Replacement Algorithm


Aim

To write a C program to implement the Optimal Page Replacement Algorithm.

Description

Optimal page replacement replaces the page that will not be used for the longest time in the future.

Program
#include<stdio.h>

intmain()
{
intpages[30], frame[10];
intn, f, i, j, k, pos, fault=0;

printf("Enter number of pages: ");


scanf("%d",&n);

printf("Enter page reference string:\n");


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

printf("Enter number of frames: ");


scanf("%d",&f);

for(i=0;i<f;i++)
frame[i]=-1;

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

for(j=0;j<f;j++)
{
if(frame[j]==pages[i])
{
found=1;
break;
}
}

if(found==0)
{
intfarthest=-1;
pos=-1;

for(j=0;j<f;j++)
{
for(k=i+1;k<n;k++)
{
if(frame[j]==pages[k])
break;

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 24


III-II CSE(AIML) OPERATING SYSTEM
}

if(k>farthest)
{
farthest=k;
pos=j;
}
}

frame[pos]=pages[i];
fault++;
}
}

printf("Total Page Faults = %d",fault);

return0;
}
Output
Enter number of pages: 12
Enter page reference string:
123412512345
Enter number of frames: 3
Total Page Faults = 7
Result

Thus the program for Optimal Page Replacement Algorithm was executed successfully.

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 25


III-II CSE(AIML) OPERATING SYSTEM
Experiment – 7
Implementation of File Allocation Strategies

(a) Sequential File Allocation


Aim

To write a C program to implement Sequential File Allocation.

Description

Sequential file allocation stores files in contiguous memory blocks.


All the blocks of a file are allocated one after another. This method provides fast access but may lead to
external fragmentation.

Program
#include<stdio.h>

intmain()
{
intstart, length, i;

printf("Enter starting block: ");


scanf("%d",&start);

printf("Enter length of file: ");


scanf("%d",&length);

printf("Allocated blocks are:\n");

for(i=0;i<length;i++)
{
printf("%d ", start+i);
}

return0;
}

Output
Enter starting block: 10
Enter length of file: 5
Allocated blocks are:
10 11 12 13 14

Result

Thus the program for Sequential File Allocation was implemented and executed successfully.

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 26


III-II CSE(AIML) OPERATING SYSTEM

(b) Linked File Allocation


Aim

To write a C program to implement Linked File Allocation.

Description

In linked file allocation, each file block contains a pointer to the next block.
The blocks may be scattered anywhere in the disk. This method eliminates external fragmentation but
access becomes slower.

Program
#include<stdio.h>

intmain()
{
intn,i,block[10];

printf("Enter number of blocks: ");


scanf("%d",&n);

printf("Enter block numbers:\n");


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

printf("Linked Allocation:\n");

for(i=0;i<n;i++)
{
if(i<n-1)
printf("%d -> ",block[i]);
else
printf("%d",block[i]);
}

return0;
}

Output
Enter number of blocks: 4
Enter block numbers:
7 12 20 25
Linked Allocation:
7 -> 12 -> 20 -> 25

Result

Thus the program for Linked File Allocation was implemented and executed successfully.

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 27


III-II CSE(AIML) OPERATING SYSTEM
(c) Indexed File Allocation
Aim

To write a C program to implement Indexed File Allocation.

Description

In indexed file allocation, each file has an index block which contains the addresses of all disk blocks of the
file. This allows direct access to any block of the file.

Program
#include<stdio.h>

intmain()
{
intindex, n, i, block[10];

printf("Enter index block number: ");


scanf("%d",&index);

printf("Enter number of blocks: ");


scanf("%d",&n);

printf("Enter block numbers:\n");

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

printf("Index Block: %d\n", index);

printf("Blocks allocated:\n");

for(i=0;i<n;i++)
printf("%d ", block[i]);

return0;
}

Output
Enter index block number: 5
Enter number of blocks: 4
Enter block numbers:
11 14 18 21
Index Block: 5
Blocks allocated:
11 14 18 21

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 28


III-II CSE(AIML) OPERATING SYSTEM
Experiment – 8
Program to Simulate Producer and Consumer Problem Using Semaphore

Aim

To write a C program to simulate the Producer Consumer Problem using Semaphore.

Description

The Producer-Consumer problem is a classical problem of process synchronization.


It consists of two processes:

• Producer – produces items and places them in a buffer.


• Consumer – consumes items from the buffer.

If the buffer is full, the producer must wait.


If the buffer is empty, the consumer must wait.

Semaphores are used to control access to the shared buffer and avoid race conditions.

The three semaphore variables used are:

• mutex – ensures mutual exclusion


• full – counts filled buffer slots
• empty – counts empty buffer slots

Program
#include<stdio.h>
#include<stdlib.h>

intmutex=1;
intfull=0;
intempty=3;
intx=0;

voidproducer()
{
mutex--;
full++;
empty--;
x++;
printf("Producer produces item %d\n",x);
mutex++;
}

voidconsumer()
{
mutex--;
full--;
empty++;
printf("Consumer consumes item %d\n",x);
x--;
mutex++;

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 29


III-II CSE(AIML) OPERATING SYSTEM
}

intmain()
{
intn;

while(1)
{
printf("\[Link] [Link] [Link]\n");
printf("Enter your choice: ");
scanf("%d",&n);

switch(n)
{
case1:
if((mutex==1) && (empty!=0))
producer();
else
printf("Buffer is Full\n");
break;

case2:
if((mutex==1) && (full!=0))
consumer();
else
printf("Buffer is Empty\n");
break;

case3:
exit(0);
}
}
}

Output
[Link] [Link] [Link]
Enter your choice: 1
Producer produces item 1

[Link] [Link] [Link]


Enter your choice: 1
Producer produces item 2

[Link] [Link] [Link]


Enter your choice: 2
Consumer consumes item 2

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 30


III-II CSE(AIML) OPERATING SYSTEM
Experiment – 9
Program to Create a Thread Using Pthreads Library

Aim

To write a C program to create a thread using the POSIX Threads and execute a function using Thread.

Description

A thread is the smallest unit of execution within a process. Multiple threads within a process can run
concurrently and share the same memory space.

The Pthreads (POSIX Threads) library provides functions for creating and managing threads in C
programs.

Important pthread functions used:

• pthread_create() – Creates a new thread


• pthread_join() – Waits for the thread to finish execution
• pthread_exit() – Terminates the thread

Threads help improve program performance by allowing parallel execution.

Program
#include<stdio.h>
#include<pthread.h>

void*thread_function()
{
printf("Thread is running...\n");
returnNULL;
}

intmain()
{
pthread_tthread;

pthread_create(&thread, NULL, thread_function, NULL);

pthread_join(thread, NULL);

printf("Thread execution completed\n");

return0;
}

Output
Thread is running...
Thread execution completed

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 31


III-II CSE(AIML) OPERATING SYSTEM
Experiment – 10
Program to Illustrate Concurrent Execution of Threads Using Pthreads Library
Aim

To write a C program to illustrate concurrent execution of threads using the POSIX Threads and
demonstrate the concept of Thread.

Description

A thread is a lightweight process that allows multiple tasks to execute within a single process. Threads
share the same memory space but execute independently.

Using the Pthreads library, multiple threads can run concurrently. This improves system performance and
CPU utilization.

Important functions used in pthreads:

• pthread_create() – Creates a new thread


• pthread_join() – Waits for thread termination
• pthread_exit() – Terminates the thread

In this program, two threads execute simultaneously to demonstrate concurrent execution.

Program
#include<stdio.h>
#include<pthread.h>
void*thread1()
{
printf("Thread 1 is running\n");
returnNULL;
}
void*thread2()
{
printf("Thread 2 is running\n");
returnNULL;
}
intmain()
{
pthread_tt1, t2;
pthread_create(&t1, NULL, thread1, NULL);
pthread_create(&t2, NULL, thread2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);

printf("Both threads have finished execution\n");

return0;
}
Output
Thread 1 is running
Thread 2 is running
Both threads have finished execution

AVANTHI’S ST. THERESSA INSTITUTE OF ENGINEERING & TECHNOLOGY 32

You might also like