OS Lab-3 - 19JE0425 - Kartikekishore
OS Lab-3 - 19JE0425 - Kartikekishore
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;
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:
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;
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;
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;
}
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 << "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:
PROGRAM CODE :
#include <bits/stdc++.h>
}
}
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;
}
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;
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 :