Oriental College of Technology: Department of Computer Science & Engineering
Oriental College of Technology: Department of Computer Science & Engineering
TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
OPERATING SYSTEM
LAB FILE
SUBJECT CODE – CS405
wait_time[0] = 0;
return 0;
int i;
return 0;
}
int avgtime( int proc[], int n, int burst_time[]) {
int i;
return 0;
int main()
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];
int main()
{
int num, temp;
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;
};
wt[0] = 0;
findWaitingTime(proc, n, wt);
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;
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}
int main()
{
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;
};
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;
sort(p,p+n,compareArrival);
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);
}
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;
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
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;
seekcount += diff[index][0];
head = request[index];
}
seeksequence[n] = head;
return 0;
}
OUTPUT
EXPERIMENT-11