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

Oriental College of Technology: Department of Computer Science & Engineering

The document contains code and output for experiments on different CPU scheduling algorithms, including FCFS, SJF, Priority, Round Robin, and a comparison of the algorithms. The five experiments implement and test each of these algorithms. The code includes functions for calculating waiting times, turnaround times, and average times for each algorithm. The output displays the results of running sample processes through each scheduling algorithm.

Uploaded by

ritika makhija
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)
49 views

Oriental College of Technology: Department of Computer Science & Engineering

The document contains code and output for experiments on different CPU scheduling algorithms, including FCFS, SJF, Priority, Round Robin, and a comparison of the algorithms. The five experiments implement and test each of these algorithms. The code includes functions for calculating waiting times, turnaround times, and average times for each algorithm. The output displays the results of running sample processes through each scheduling algorithm.

Uploaded by

ritika makhija
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/ 38

ORIENTAL COLLEGE OF

TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING

OPERATING SYSTEM
LAB FILE
SUBJECT CODE – CS405

SUBMITTED BY: SUBMITTED TO:


RITIKA MAKHIJA PRIYANKA SHARMA
CSE-B MAM
SEM- III
ENRL NO. – 0126CS191091
EXPERIMENT-1
Write a program to implement FCFS CPU scheduling
algorithm.
#include <stdio.h>

int waitingtime(int proc[], int n,

int burst_time[], int wait_time[])

wait_time[0] = 0;

for (int i = 1; i < n ; i++ )

wait_time[i] = burst_time[i-1] + wait_time[i-1] ;

return 0;

int turnaroundtime( int proc[], int n,

int burst_time[], int wait_time[], int tat[])

int i;

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

tat[i] = burst_time[i] + wait_time[i];

return 0;

}
int avgtime( int proc[], int n, int burst_time[]) {

int wait_time[n], tat[n], total_wt = 0, total_tat = 0;

int i;

waitingtime(proc, n, burst_time, wait_time);

turnaroundtime(proc, n, burst_time, wait_time, tat);

printf("Processes Burst Waiting Turn around \n");

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

total_wt = total_wt + wait_time[i];

total_tat = total_tat + tat[i];

printf(" %d\t %d\t\t %d \t%d\n", i+1, burst_time[i], wait_time[i], tat[i]);

printf("Average waiting time = %f\n", (float)total_wt / (float)n);

printf("Average turn around time = %f\n", (float)total_tat / (float)n);

return 0;

int main()

int proc[] = { 1, 2, 3};

int n = sizeof proc / sizeof proc[0];


int burst_time[] = {5, 8, 12};

avgtime(proc, n, burst_time);

return 0;

OUTPUT
EXPERIMENT-2
Write a program to implement SJF CPU scheduling algorithm.
#include <iostream>
using namespace std;
int mat[10][6];

void swap(int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}

void arrangeArrival(int num, int mat[][6])


{
for (int i = 0; i < num; i++)
{
for (int j = 0; j < num - i - 1; j++)
{
if (mat[j][1] > mat[j + 1][1])
{
for (int k = 0; k < 5; k++)
{
swap(mat[j][k], mat[j + 1][k]);
}
}
}
}
}

void completionTime(int num, int mat[][6])


{
int temp, val;
mat[0][3] = mat[0][1] + mat[0][2];
mat[0][5] = mat[0][3] - mat[0][1];
mat[0][4] = mat[0][5] - mat[0][2];

for (int i = 1; i < num; i++)


{
temp = mat[i - 1][3];
int low = mat[i][2];
for (int j = i; j < num; j++)
{
if (temp >= mat[j][1] && low >= mat[j][2])
{
low = mat[j][2];
val = j;
}
}
mat[val][3] = temp + mat[val][2];
mat[val][5] = mat[val][3] - mat[val][1];
mat[val][4] = mat[val][5] - mat[val][2];
for (int k = 0; k < 6; k++)
{
swap(mat[val][k], mat[i][k]);
}
}
}

int main()
{
int num, temp;

cout << "Enter number of Process: ";


cin >> num;

cout << "...Enter the process ID...\n";


for (int i = 0; i < num; i++)
{
cout << "...Process " << i + 1 << "...\n";
cout << "Enter Process Id: ";
cin >> mat[i][0];
cout << "Enter Arrival Time: ";
cin >> mat[i][1];
cout << "Enter Burst Time: ";
cin >> mat[i][2];
}
cout << "Before Arrange...\n";
cout << "Process ID\tArrival Time\tBurst Time\n";
for (int i = 0; i < num; i++)
{
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t" << mat[i][2] << "\n";
}

arrangeArrival(num, mat);
completionTime(num, mat);
cout << "Final Result...\n";
cout << "Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround
Time\n";
for (int i = 0; i < num; i++)
{
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t" << mat[i][2] << "\t\t" <<
mat[i][4] << "\t\t" << mat[i][5] << "\n";
}
}
OUTPUT
EXPERIMENT-3
Write a program to implement Priority CPU Scheduling
algorithm.

#include<bits/stdc++.h>
using namespace std;

struct Process
{
int pid;
int bt;
int priority;
};

bool comparison(Process a, Process b)


{
return (a.priority > b.priority);
}

void findWaitingTime(Process proc[], int n,


int wt[])
{

wt[0] = 0;

for (int i = 1; i < n ; i++ )


wt[i] = proc[i-1].bt + wt[i-1] ;
}

void findTurnAroundTime( Process proc[], int n,


int wt[], int tat[])
{

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


tat[i] = proc[i].bt + wt[i];
}
void findavgTime(Process proc[], int n)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime(proc, n, wt);

findTurnAroundTime(proc, n, wt, tat);

cout << "\nProcesses "<< " Burst time "


<< " Waiting time " << " Turn around time\n";

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


{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << "\t\t"
<< proc[i].bt << "\t " << wt[i]
<< "\t\t " << tat[i] <<endl;
}

cout << "\nAverage waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

void priorityScheduling(Process proc[], int n)


{

sort(proc, proc + n, comparison);

cout<< "Order in which processes gets executed \n";


for (int i = 0 ; i < n; i++)
cout << proc[i].pid <<" " ;

findavgTime(proc, n);
}

int main()
{
Process proc[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}};
int n = sizeof proc / sizeof proc[0];
priorityScheduling(proc, n);
return 0;
}

OUTPUT

EXPERIMENT-4
Write a program to implement Round Robin CPU scheduling
algorithm.

#include <iostream>
using namespace std;
void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{

int rem_bt[n];
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];

int t = 0;

while (1)
{
bool done = true;

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


{
if (rem_bt[i] > 0)
{
done = false;
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];

wt[i] = t - bt[i];

rem_bt[i] = 0;
}
}
}

if (done == true)
break;
}
}

void findTurnAroundTime(int processes[], int n,


int bt[], int wt[], int tat[])
{

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


tat[i] = bt[i] + wt[i];
}
void findavgTime(int processes[], int n, int bt[],
int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime(processes, n, bt, wt, quantum);

findTurnAroundTime(processes, n, bt, wt, tat);

cout << "Processes "


<< " Burst time "
<< " Waiting time "
<< " Turn around time\n";

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


{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i + 1 << "\t\t" << bt[i] << "\t "
<< wt[i] << "\t\t " << tat[i] << endl;
}

cout << "Average waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

int main()
{

int processes[] = {1, 2, 3};


int n = sizeof processes / sizeof processes[0];

int burst_time[] = {10, 5, 8};

int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}

OUTPUT
EXPERIMENT-5
Write a program to compare various CPU Scheduling Algorithms
over different Scheduling Criteria.
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;

struct process {
int pid;
int arrival_time;
int burst_time;
int start_time;
int completion_time;
int turnaround_time;
int waiting_time;
int response_time;
};

bool compareArrival(process p1, process p2)


{
return p1.arrival_time < p2.arrival_time;
}

bool compareID(process p1, process p2)


{
return p1.pid < p2.pid;
}

int main() {

int n;
struct process p[100];
float avg_turnaround_time;
float avg_waiting_time;
float avg_response_time;
float cpu_utilisation;
int total_turnaround_time = 0;
int total_waiting_time = 0;
int total_response_time = 0;
int total_idle_time = 0;
float throughput;

cout << setprecision(2) << fixed;

cout<<"Enter the number of processes: ";


cin>>n;

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


cout<<"Enter arrival time of process "<<i+1<<": ";
cin>>p[i].arrival_time;
cout<<"Enter burst time of process "<<i+1<<": ";
cin>>p[i].burst_time;
p[i].pid = i+1;
cout<<endl;
}

sort(p,p+n,compareArrival);

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


p[i].start_time = (i == 0)?p[i].arrival_time:max(p[i-
1].completion_time,p[i].arrival_time);
p[i].completion_time = p[i].start_time + p[i].burst_time;
p[i].turnaround_time = p[i].completion_time - p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
p[i].response_time = p[i].start_time - p[i].arrival_time;

total_turnaround_time += p[i].turnaround_time;
total_waiting_time += p[i].waiting_time;
total_response_time += p[i].response_time;
total_idle_time += (i == 0)?(p[i].arrival_time):(p[i].start_time - p[i-
1].completion_time);
}

avg_turnaround_time = (float) total_turnaround_time / n;


avg_waiting_time = (float) total_waiting_time / n;
avg_response_time = (float) total_response_time / n;
cpu_utilisation = ((p[n-1].completion_time - total_idle_time) / (float) p[n-
1].completion_time)*100;
throughput = float(n) / (p[n-1].completion_time - p[0].arrival_time);

sort(p,p+n,compareID);

cout<<endl;
cout<<"#P\t"<<"AT\t"<<"BT\t"<<"ST\t"<<"CT\t"<<"TAT\t"<<"WT\t"<<"RT\
t"<<"\n"<<endl;

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


cout<<p[i].pid<<"\t"<<p[i].arrival_time<<"\t"<<p[i].burst_time<<"\
t"<<p[i].start_time<<"\t"<<p[i].completion_time<<"\
t"<<p[i].turnaround_time<<"\t"<<p[i].waiting_time<<"\
t"<<p[i].response_time<<"\t"<<"\n"<<endl;
}
cout<<"Average Turnaround Time = "<<avg_turnaround_time<<endl;
cout<<"Average Waiting Time = "<<avg_waiting_time<<endl;
cout<<"Average Response Time = "<<avg_response_time<<endl;
cout<<"CPU Utilization = "<<cpu_utilisation<<"%"<<endl;
cout<<"Throughput = "<<throughput<<" process/unit time"<<endl;

OUTPUT
EXPERIMENT-6
Write a program to implement classical inter process
communication problem (producer consumer).
#include <stdio.h>
#include <stdlib.h>

int mutex = 1;

int full = 0;
int empty = 10, x = 0;

void producer()
{

--mutex;

++full;

--empty;

x++;
printf("\nProducer produces"
"item %d",
x);

++mutex;
}

void consumer()
{

--mutex;

--full;

++empty;
printf("\nConsumer consumes "
"item %d",
x);
x--;

++mutex;
}

int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
#pragma omp critical

for (i = 1; i > 0; i++) {

printf("\nEnter your choice:");


scanf("%d", &n);

switch (n) {
case 1:

if ((mutex == 1)
&& (empty != 0)) {
producer();
}

else {
printf("Buffer is full!");
}
break;

case 2:
if ((mutex == 1)
&& (full != 0)) {
consumer();
}

else {
printf("Buffer is empty!");
}
break;

case 3:
exit(0);
break;
}
}
}

OUTPUT
EXPERIMENT-7
EXPERIMENT-8
Write a program to implement classical inter process
communication problem (Dining Philosophers).
#include<iostream>
#define n 4
using namespace std;
int compltedPhilo = 0,i;
struct fork{
int taken;
}ForkAvil[n];
struct philosp{
int left;
int right;
}Philostatus[n];
void goForDinner(int philID)
{
if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
cout<<"Philosopher "<<philID+1<<" completed his dinner\n";
else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
cout<<"Philosopher "<<philID+1<<" completed his dinner\n";
Philostatus[philID].left = Philostatus[philID].right = 10;
int otherFork = philID-1;
if(otherFork== -1)
otherFork=(n-1);
ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0;
cout<<"Philosopher "<<philID+1<<" released fork "<<philID+1<<" and fork
"<<otherFork+1<<"\n";
compltedPhilo++;
}
else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){
if(philID==(n-1)){
if(ForkAvil[philID].taken==0){
ForkAvil[philID].taken = Philostatus[philID].right = 1;
cout<<"Fork "<<philID+1<<" taken by philosopher
"<<philID+1<<"\n";
}else{
cout<<"Philosopher "<<philID+1<<" is waiting for fork
"<<philID+1<<"\n";
}
}else{
int dupphilID = philID;
philID-=1;
if(philID== -1)
philID=(n-1);

if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
cout<<"Fork "<<philID+1<<" taken by Philosopher
"<<dupphilID+1<<"\n";
}else{
cout<<"Philosopher "<<dupphilID+1<<" is waiting for Fork
"<<philID+1<<"\n";
}
}
}
else if(Philostatus[philID].left==0){
if(philID==(n-1)){
if(ForkAvil[philID-1].taken==0){
ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
cout<<"Fork "<<philID<<" taken by philosopher
"<<philID+1<<"\n";
}else{
cout<<"Philosopher "<<philID+1<<" is waiting for fork
"<<philID<<"\n";
}
}else{
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[philID].left = 1;
cout<<"Fork "<<philID+1<<" taken by Philosopher
"<<philID+1<<"\n";
}else{
cout<<"Philosopher "<<philID+1<<" is waiting for Fork
"<<philID+1<<"\n";
}
}
}else{}
}

int main(){
for(i=0;i<n;i++)
ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;
while(compltedPhilo<n){
for(i=0;i<n;i++)
goForDinner(i);
cout<<"\nTill now num of philosophers completed dinner are
"<<compltedPhilo<<"\n\n";
}
return 0;
}

OUTPUT

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

OUTPUT
EXPERIMENT-10
Write a program to implement & Compare various Disk & Drum
scheduling Algorithms
#include <bits/stdc++.h>
using namespace std;
void calculatedifference(int request[], int head,int diff[][2], int n)
{
for(int i = 0; i < n; i++)
{
diff[i][0] = abs(head - request[i]);
}
}
int findMIN(int diff[][2], int n)
{
int index = -1;
int minimum = 1e9;
for(int i = 0; i < n; i++)
{
if (!diff[i][1] && minimum > diff[i][0])
{
minimum = diff[i][0];
index = i;
}
}
return index;
}
void shortestSeekTimeFirst(int request[],
int head, int n)
{
if (n == 0)
{
return;
}

int diff[n][2] = { { 0, 0 } };

int seekcount = 0;

int seeksequence[n + 1] = {0};

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


{
seeksequence[i] = head;
calculatedifference(request, head, diff, n);
int index = findMIN(diff, n);
diff[index][1] = 1;

seekcount += diff[index][0];

head = request[index];
}
seeksequence[n] = head;

cout << "Total number of seek operations = "


<< seekcount << endl;
cout << "Seek sequence is : " << "\n";
for(int i = 0; i <= n; i++)
{
cout << seeksequence[i] << "\n";
}
}
int main()
{
int n = 8;
int proc[n] = { 176, 79, 34, 60, 92, 11, 41, 114 };

shortestSeekTimeFirst(proc, 50, n);

return 0;
}
OUTPUT
EXPERIMENT-11

You might also like