0% found this document useful (0 votes)
52 views13 pages

OS Lab-3 - 19JE0425 - Kartikekishore

1) The document describes implementing three CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF), and Shortest Remaining Time First (SRTF). 2) Code and algorithms are provided for each scheduling method. For FCFS, it calculates waiting times and average times. For SJF, it sorts by arrival time then burst time. For SRTF, it finds the next process with the shortest remaining time at each time step. 3) The student, Kartike Kishore, is working on an OS lab assignment to analyze different CPU scheduling algorithms through coding and outputting results. Output is generated for each algorithm showing waiting times, turnaround times,

Uploaded by

Utkarsh Tiwari
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)
52 views13 pages

OS Lab-3 - 19JE0425 - Kartikekishore

1) The document describes implementing three CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF), and Shortest Remaining Time First (SRTF). 2) Code and algorithms are provided for each scheduling method. For FCFS, it calculates waiting times and average times. For SJF, it sorts by arrival time then burst time. For SRTF, it finds the next process with the shortest remaining time at each time step. 3) The student, Kartike Kishore, is working on an OS lab assignment to analyze different CPU scheduling algorithms through coding and outputting results. Output is generated for each algorithm showing waiting times, turnaround times,

Uploaded by

Utkarsh Tiwari
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/ 13

OS lab-3

Name: Kartike Kishore


Adm no. :19JE0425

Aim​:- Implement Cpu Scheduling algorithms

1)First come First serve​ (FCFS) CPU scheduling algorithm and printing its waiting time and
the average time for the processes.

Soln:

Algorithm:
a. Start the program.
b. Input the number of processes and respective programs burst time.
c. Initialize the waiting time for processes.
d. Process for remaining processes :
wt[i] = ct[i-1]-at[i]
ct[i] = at[i] + wt[i] + bt[i]
tat[i] = ct[i] – at[i]
e. Increment the total turn-around time and total waiting time.
f. Calculate the average turn-around time and average waiting, and output them.

PROGRAM CODE:

#include <bits/stdc++.h>
using namespace std;
//program for FCFS
//OSLAB_3
//made by Kartike kishore(19JE0425)
struct process
{
int pro_id, waiting_time, arrival_time;
int burst_time, turn_around_time, completion_time;
};
//comparision operator
bool operator<(process p1, process p2)
{
return p1.arrival_time < p2.arrival_time;
}
//solution part for FCFS
void sol()
{
int n;
int total_wait_time = 0, total_turnaround = 0;
cout << "Enter the total no. of processes: ";
cin >> n;
//declaring processes;
process processes[n];
for (int i = 0; i < n; i++)
{
cout << "\nEnter process id: ";
cin >> processes[i].pro_id;
cout << "\nEnter arrival time:";
cin >> processes[i].arrival_time;
cout << "\nEnter burst time:";cin >> processes[i].burst_time;
}
sort(processes, processes + n);
processes[0].waiting_time = 0;
processes[0].completion_time =processes[0].arrival_time + processes[0].burst_time;
processes[0].turn_around_time = processes[0].completion_time - processes[0].arrival_time;

total_wait_time = processes[0].waiting_time;
total_turnaround = processes[0].turn_around_time;

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


{
processes[i].completion_time = processes[i - 1].completion_time +
processes[i].burst_time;
processes[i].waiting_time = max(0, processes[i - 1].completion_time -
processes[i].arrival_time);

processes[i].turn_around_time =processes[i].completion_time -
processes[i].arrival_time;
total_wait_time += processes[i].waiting_time;

total_turnaround += processes[i].turn_around_time;
}
double avg_wait = (double)total_wait_time / n;
double avg_turn = (double)total_turnaround / n;
cout << setw(10) << "Process"<< setw(10) << "Arrival Time";
cout << setw(10) << "Burst Time"<< setw(10) << "Waiting Time";
cout<< setw(10) << "Completion Time";
cout << setw(10) << "Turnaround Time\n";
for (int i = 0; i < n; i++)
{
cout<< setw(8) << "P" << processes[i].pro_id;
cout<< setw(10) << processes[i].arrival_time;
cout<< setw(10) << processes[i].burst_time;
cout<< setw(10) << processes[i].waiting_time;
cout<< setw(10) << processes[i].completion_time;
cout<< setw(10) << processes[i].turn_around_time;
cout<<endl;
}
cout<<endl<<"FINAL ANSWERS FOR GIVEN SET OF PROCESSES\n";
cout << "THe total turn-around time: " << total_turnaround << endl;
cout << "The Average turn-around time:" << setprecision(5)<< avg_turn << endl;
cout << "The Total waiting time:" << total_wait_time << endl;
cout << "The Average waiting time:" << setprecision(5) << avg_wait << endl;

getchar();
}
int main()
{

sol();
return 0;
}
OUTPUT :
2)Shortest Job First (SJF) ​CPU scheduling algorithm and printing its waiting time and the
average time for the processes.

Soln:

Algorithm:

a. Sort the process according to the arrival time.


b. Select that process that has minimum arrival time and minimum Burst time.
c. After completion of the process make a pool of processes which after till the completion
of the previous process.
d. select that process among the pool which is having minimum Burst time.

PROGRAM CODE :

#include <bits/stdc++.h>
//SHORTEST JOB FIRST(SJF)
using namespace std;
struct process
{
int pid, arrival_time, waiting_time, burst_time;
int turn_around_time, completion_time;
};
bool operator<(process p1, process p2)
{
return p1.arrival_time < p2.arrival_time;
}
void waiting_time_fun(process proc[], int n)
{
int rt[n];
for (int i = 0; i < n; i++) {
rt[i] = proc[i].burst_time;
}
int shortest = 0, finish_time;
int completed = 0, tim = 0, mi_nm = INT_MAX;

bool check = false;


while (completed != n)
{
for (int j = 0; j < n; j++)
{
if ((proc[j].arrival_time <= tim) && (rt[j] < mi_nm)
&& rt[j] > 0)
{
mi_nm = rt[j];
shortest = j;
check = true;
}
}
if (check == false)
{
tim++;
continue;
}

rt[shortest]--;
mi_nm = rt[shortest];
if (mi_nm == 0)
mi_nm = INT_MAX;

if (rt[shortest] == 0)
{
completed++;
check = false;
finish_time = tim + 1;

proc[shortest].waiting_time = finish_time - proc[shortest].burst_time -


proc[shortest].arrival_time;
if (proc[shortest].waiting_time < 0)
{
proc[shortest].waiting_time = 0;
}
}

tim++;
}
}

void sol()
{
int n;
cout << "Enter the total number of processes:";
cin >> n;process processes[n];
cout << "Enter process id, arrival time and burst time for each process:\n";
for (int i = 0; i < n; i++)
{
cout<<"Enter "<<i+1<<"th information \n";
cout<<"Enter process id: ";cin >> processes[i].pid;
cout<<"Enter arrival time: ";cin >> processes[i].arrival_time;
cout<<"Enter burst time: ";
cin >> processes[i].burst_time;
cout<<endl;
}

sort(processes, processes + n);

processes[0].waiting_time = 0;
processes[0].turn_around_time = processes[0].completion_time - processes[0].arrival_time;
waiting_time_fun(processes, n);
int total_wait = 0, total_turn_around = 0;
for (int i = 0; i < n; i++)
{
processes[i].turn_around_time = processes[i].burst_time + processes[i].waiting_time;

processes[i].completion_time = processes[i].arrival_time +
processes[i].turn_around_time;

total_wait += processes[i].waiting_time;
total_turn_around += processes[i].turn_around_time;
}
double avg_wait = (double)total_wait / n;
double avg_turn = (double)total_turn_around / n;
cout << setw(14) << "Process";
cout << setw(14) << "Arrival Time";
cout << setw(14) << "Burst Time";
cout << setw(14) << "Waiting Time";
cout << setw(19) << "Turnaround Time";
cout << setw(19) << "Completion Time\n";
for (int i = 0; i < n; i++)
{
cout << setw(13) << "P" << processes[i].pid;
cout << setw(14) << processes[i].arrival_time;
cout << setw(14) << processes[i].burst_time;
cout<< setw(14) << processes[i].waiting_time;
cout<< setw(14) << processes[i].turn_around_time;
cout << setw(14) << processes[i].completion_time << endl;
};
cout << endl;

cout << "Total turn-around time:" << total_turn_around;


cout<< endl;

cout << "Average turn-around time:" << setprecision(5) <<avg_turn << endl;
cout << "Total waiting time:" << total_wait << endl;

cout << "Average waiting time:" << setprecision(5) <<avg_wait << endl;
}
int main()
{

sol();
return 0;
}
OUTPUT :
3)Shortest Remaining Time First​ (SRTF)​ ​CPU scheduling algorithm and printing its average
waiting time and the turn-around time for the processes.

Soln:

Algorithm:

i. Traverse until all process gets completely executed.


a) Find process with minimum remaining time at every single time lap.
b) Reduce its time by 1.
c) Check if its remaining time becomes 0
d) Increment the counter of process completion.
e) Completion time of current process = current_time +1;
f) Compute waiting time for each completed process.
g) wt[i]= Completion time - arrival_time-burst_time
h) Increment time lap by one.

ii. Find turnaround time (waiting_time+burst_time).

PROGRAM CODE :

#include <bits/stdc++.h>

using namespace std;


//Shortest Remaining Time First (SRTF)
struct process
{
int pid, arrival_time, waiting_time;
int burst_time, turn_around_time, completion_time;
};
bool operator<(process p1, process p2)
{
return p1.arrival_time < p2.arrival_time;
}
void findWaitingTime(process proc[], int n)
{
int rt[n];
for (int i = 0; i < n; i++)
rt[i] = proc[i].burst_time;
int complete = 0, tim = 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].arrival_time <= tim) && (rt[j] < minm) && rt[j] > 0)
{ check = true;
minm = rt[j];
shortest = j;

}
}
if (check == false)
{
tim++;
continue;
}
rt[shortest]--;
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
if (rt[shortest] == 0)
{
complete++;
check = false;
finish_time = tim + 1;
proc[shortest].waiting_time = finish_time - proc[shortest].burst_time -
proc[shortest].arrival_time;

if (proc[shortest].waiting_time < 0) {
proc[shortest].waiting_time = 0;
}
}
tim++;
}
}
void sol()
{
int n;
cout << "Enter the total number of processes:";
cin >> n; process processes[n];
cout << "Enter process id, arrival time and burst time for each process:\n";
for (int i = 0; i < n; i++)
{
cout << "Enter " << i + 1 << "th information \n";
cout << "Enter process id: "; cin >> processes[i].pid;
cout << "Enter arrival time: "; cin >> processes[i].arrival_time;
cout << "Enter burst time: ";
cin >> processes[i].burst_time;
cout << endl;
}

sort(processes, processes + n);

processes[0].waiting_time = 0;
processes[0].turn_around_time = processes[0].completion_time - processes[0].arrival_time;

findWaitingTime(processes, n);
int total_wait = 0, total_turn_around = 0;

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


{
processes[i].turn_around_time = processes[i].burst_time + processes[i].waiting_time;
processes[i].completion_time = processes[i].arrival_time +
processes[i].turn_around_time;

total_wait += processes[i].waiting_time;
total_turn_around += processes[i].turn_around_time;
}
double avg_wait = (double)total_wait / n;
double avg_turn = (double)total_turn_around / n;
cout << setw(14) << "Process"
<< setw(14) << "Arrival Time"
<< setw(14) << "Burst Time"
<< setw(14) << "Waiting Time"
<< setw(19) << "Turnaround Time"
<< setw(19) << "Completion Time\n";
for (int i = 0; i < n; i++)
{
cout << setw(13) << "P" << processes[i].pid
<< setw(14) << processes[i].arrival_time
<< setw(14) << processes[i].burst_time
<< setw(14) << processes[i].waiting_time
<< setw(14) << processes[i].turn_around_time
<< setw(14) << processes[i].completion_time <<
"\n";
};
cout << "\n";
cout << "Total turn-around time:" << total_turn_around <<endl;
cout << "Average turn-around time:" << setprecision(5) <<avg_turn << endl;
cout << "Total waiting time:" << total_wait << endl;
cout << "Average waiting time:" << setprecision(5) <<avg_wait << endl;
}
int main()
{

sol();
return 0;
}

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy