[go: up one dir, main page]

0% found this document useful (0 votes)
155 views15 pages

Dipu Os

The document contains code and explanations for implementing CPU scheduling algorithms like FCFS, SJF, and priority scheduling in C++. It includes code to calculate waiting time, turnaround time, completion time and average waiting/turnaround times for each algorithm. Experiments are presented for non-preemptive FCFS, SJF scheduling, and non-preemptive priority scheduling. Pseudocode and output are provided for some experiments.

Uploaded by

Gaurav Mishra
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)
155 views15 pages

Dipu Os

The document contains code and explanations for implementing CPU scheduling algorithms like FCFS, SJF, and priority scheduling in C++. It includes code to calculate waiting time, turnaround time, completion time and average waiting/turnaround times for each algorithm. Experiments are presented for non-preemptive FCFS, SJF scheduling, and non-preemptive priority scheduling. Pseudocode and output are provided for some experiments.

Uploaded by

Gaurav Mishra
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
You are on page 1/ 15

MAHARAJA SURAJMAL

INSTITUTE OF TECHNOLGY

OPERATING SYSTEMS PRACTICAL FILE


(ETCS-352)

Submitted By : DIPENDRA JHA


Submitted To : Dr. Sonika Malik
Enrollment No : 0035007721
Branch : IT-1

DIPENDRA JHA (IT-1)


(0035007721)
INDEX
S.NO TITLE DATE PAGE.NO SIGN

DIPENDRA JHA (IT-1)


(0035007721)
Experiment -1
AIM: WAP to implement CPU scheduling for first come first serve(FCFS).

Theory:

Simplest CPU scheduling algorithm that schedules according to arrival times of


processes. First come first serve scheduling algorithm states that the process that
requests the CPU first is allocated the CPU first. It is implemented by using the
FIFO queue. When a process enters the ready queue, its PCB is linked to the tail of
the queue. When the CPU is free, it is allocated to the process at the head of the
queue. The running process is then removed from the queue. FCFS is a non-
preemptive scheduling algorithm.

Basic Terminologies:

1. Completion Time: Time at which process completes its execution.


2. Turn Around Time: Time Difference between completion time and arrival
time. Turn Around Time = Completion Time – Arrival Time.
3. Waiting Time(W.T): Time Difference between turn around time and burst
time. Waiting Time = Turn Around Time – Burst Time.
4. Average Waiting Time = Sum of waiting time of all processes / total no. of
processes.

DIPENDRA JHA (IT-1)


(0035007721)
CODE :
#include <iostream>

using namespace std;

void waitingtime(int at[],int n,int bt[], int wt[]){

wt[0] = 0;

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

wt[i] = wt[i-1] + bt[i-1];

void turnaroundtime(int n, int bt[], int wt[], int tat[]){

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

tat[i] = bt[i] + wt[i];

void completiontime(int at[],int n, int tat[],int ct[]){

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

ct[i] = tat[i]+ at[i];

void avgtime(int at[],int n,int bt[]){

int wt[n], tat[n], ct[n];

float tot_tat = 0,tot_wt = 0;

waitingtime(at, n, bt, wt);

turnaroundtime(n, bt, wt, tat);

completiontime(at, n, tat, ct);

cout<<" Arival time " <<" Burst time " << " Waiting time "<< " Completion time " <<" Turn
around time "<< endl;

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

tot_tat = tot_tat + tat[i];

tot_wt = tot_wt + wt[i];

cout<<"\t "<< at[i]<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<ct[i]<<"\t\t"<<tat[i]<<endl;

}
DIPENDRA JHA (IT-1)
(0035007721)
cout<< " Average waiting time = "<<tot_wt/n<< endl;

cout<< " Average turn around time = " <<tot_tat/n;

int main()

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

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

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

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

for (int j = 0; j < n - i - 1; j++)

if (at[j] > at[j + 1])

swap(at[j], at[j + 1]);

avgtime(at, n, bt);

return 0;

OUTPUT

DIPENDRA JHA (IT-1)


(0035007721)
Experiment -2
a) AIM: WAP to implement CPU scheduling for shortest job first (SJF).
CODE :
#include<iostream>

using namespace std;

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

void arrangeArrival(int num, int mat[][5]) {

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

for(int j=0; j<num-i-1; j++)

if(mat[1][j] > mat[1][j+1])

for(int k=0; k<5; k++)

swap(mat[k][j], mat[k][j+1]);

void completionTime(int num, int mat[][5]) {

int temp, val;

mat[3][0] = mat[1][0] + mat[2][0];

mat[5][0] = mat[3][0] - mat[1][0];

mat[4][0] = mat[5][0] - mat[2][0];

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

temp = mat[3][i-1];

int low = mat[2][i];

for(int j=i; j<num; j++) {

if(temp >= mat[1][j] && low >= mat[2][j]) {

DIPENDRA JHA (IT-1)


(0035007721)
low = mat[2][j];

val = j;

mat[3][val] = temp + mat[2][val];

mat[5][val] = mat[3][val] - mat[1][val];

mat[4][val] = mat[5][val] - mat[2][val];

for(int k=0; k<6; k++) {

swap(mat[k][val], mat[k][i]);

void average(int num, int mat[][5])

float tot_tat = 0,tot_wt = 0;

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

tot_tat = tot_tat + mat[5][i];

tot_wt = tot_wt + mat[4][i];

cout<<" Average waiting time "<<tot_wt/num;

cout<<" \n Average turn arount time "<<tot_tat/num;

int main() {

int num = 5, temp;

int mat[6][5] = {1, 2, 3, 4, 5, 2, 5, 1, 0, 4, 6, 2, 8, 3, 4};

arrangeArrival(num, mat);

completionTime(num, mat);

DIPENDRA JHA (IT-1)


(0035007721)
cout<<"Process ID\tArrival Time\tBurst Time\tCompletion Time\tWaiting Time\tTurnaround
Time\n";

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

cout<<"\t"<<mat[0][i]<<"\t\t"<<mat[1][i]<<"\t\t"<<mat[2][i]<<"\t\t"<<mat[3][i]<<"\t\t"<<mat[4][i]<<"\t\t
"<<mat[5][i]<<"\n";

average(num,mat);

OUTPUT

DIPENDRA JHA (IT-1)


(0035007721)
b) AIM: WAP to implement CPU scheduling for shortest job first (SJF)
Preemptive.

CODE :
#include <bits/stdc++.h>

using namespace std;

struct Process {

int pid;

int bt;

int art;

};

void findWaitingTime(Process proc[], int n,int wt[])

int rt[n];

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

rt[i] = proc[i].bt;

int complete = 0, t = 0, minm = INT_MAX;

int shortest = 0, finish_time;

bool check = false;

while (complete != n) {

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

if ((proc[j].art<= t) &&(rt[j] <minm) && rt[j] > 0) {

minm = rt[j];

shortest = j;

check = true;

}
DIPENDRA JHA (IT-1)
(0035007721)
if (check == false) {

t++;

continue;

rt[shortest]--;

minm = rt[shortest];

if (minm == 0)

minm = INT_MAX;

if (rt[shortest] == 0) {

complete++;

check = false;

finish_time = t + 1;

wt[shortest] = finish_time -proc[shortest].bt -proc[shortest].art;

if (wt[shortest] <0)wt[shortest] = 0;

} t++;

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<< "Process ID\t"

DIPENDRA JHA (IT-1)


(0035007721)
<< "Burst Time\t"

<<"Arival time\t"

<< "Waiting Time\t"

<< "Turnaround Time\t\t\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\t" <<


proc[i].art<<"\t\t\t"<<wt[i]<< "\t\t" << tat[i] <<endl;}

cout<< "\nAverage waiting time = "<< (float)total_wt / (float)n;

cout<< "\nAverageturn around time = "<< (float)total_tat / (float)n;

int main()

Process proc[] = { { 1, 6, 2 }, { 2, 2, 5 },{ 3, 8, 1 }, { 4, 3, 0}, {5, 4, 4} };

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

findavgTime(proc, n);

return 0;

OUTPUT

DIPENDRA JHA (IT-1)


(0035007721)
Experiment -3
a) AIM: WAP to perform priority scheduling (non – preemptive).
CODE :
#include<iostream>

using namespace std;

int main()

int a[] = { 0, 1, 2, 4};

int b[] = { 5, 3, 4, 1};

int x[10];

int pr[] = { 10,20,30,40};

int waiting[10],turnaround[10],completion[10];

int i,j,smallest,count=0,time,n = 4;

double avg=0,tt=0,end;

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

x[i]=b[i];

pr[9]=-1;

for(time=0;count!=n;time++)

smallest=9;

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

if(a[i]<=time && pr[i]>pr[smallest] && b[i]>0 )

smallest=i;

DIPENDRA JHA (IT-1)


(0035007721)
time+=b[smallest]-1;

b[smallest]=-1;

count++;

end=time+1;

completion[smallest] = end;

waiting[smallest] = end - a[smallest] - x[smallest];

turnaround[smallest] = end - a[smallest];

cout<<"Process"<<"\t\t"<< "BT"<<"\t\t"<<"AT" <<"\t\t"<<"WT" <<"\t\t"<<"TAT"<<


"\t\t"<<"CT"<<"\t\t"<<"Priority"<<endl;

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

cout<<"
p"<<i+1<<"\t\t"<<x[i]<<"\t\t"<<a[i]<<"\t\t"<<waiting[i]<<"\t\t"<<turnaround[i]<<"\t\t"<<completio
n[i]<<"\t\t"<<pr[i]<<endl;

avg = avg + waiting[i];

tt = tt + turnaround[i];

cout<<"\n Average waiting time ="<<avg/n<<endl;

cout<<"Average Turnaround time ="<<tt/n<<endl;

OUTPUT

DIPENDRA JHA (IT-1)


(0035007721)
b) AIM: WAP to perform priority scheduling (preemptive).
CODE :
#include<iostream>

using namespace std;

int main()

int a[] = { 0,1,2,4 },

b[] = { 5, 3, 2, 1},

p[] ={ 10, 20, 30, 40},

x[10];

int waiting[10],turnaround[10],completion[10];

int i,j,smallest,count=0,time,n = 4;

double avg=0,tt=0,end;

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

x[i]=b[i];

p[9]=-1;

for(time=0; count!=n; time++)

smallest=9;

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

if(a[i]<=time && p[i]>p[smallest] && b[i]>0 )

smallest=i;

b[smallest]--;

DIPENDRA JHA (IT-1)


(0035007721)
if(b[smallest]==0)

count++;

end=time+1;

completion[smallest] = end;

waiting[smallest] = end - a[smallest] - x[smallest];

turnaround[smallest] = end - a[smallest];

cout<<"Process"<<"\t\t"<< "BT"<<"\t\t"<<"AT" <<"\t\t"<<"WT" <<"\t\t"<<"TAT"<<


"\t\t"<<"CT"<<"\t\t"<<"Priority"<<endl;

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

cout<<"\tp"<<i+1<<"\t\t"<<x[i]<<"\t\t"<<a[i]<<"\t\t"<<waiting[i]<<"\t\t"<<turnaround[i]<<"\t\t"<<c
ompletion[i]<<"\t\t"<<p[i]<<endl;

avg = avg + waiting[i];

tt = tt + turnaround[i];

cout<<"\n Average waiting time ="<<avg/n<<endl;

cout<<"Average Turnaround time ="<<tt/n<<endl;

OUTPUT

DIPENDRA JHA (IT-1)


(0035007721)

You might also like