Untitled
Untitled
Untitled
1)
FCFS Scheduling
CODE
#include <iostream>
#include <algorithm>
struct Process {
int id;
int burstTime;
int priority;
int arrivalTime;
int startTime;
int endTime;
int waitingTime;
int turnaroundTime;
};
int currentTime = 0;
currentTime = processes[i].endTime;
int main() {
Process processes[] = {
{1, 3, 2, 0, 0, 0, 0, 0},
{2, 1, 1, 0, 0, 0, 0, 0},
{3, 7, 4, 0, 0, 0, 0, 0},
{4, 4, 2, 0, 0, 0, 0, 0},
{5, 5, 3, 0, 0, 0, 0, 0}
};
fcfs(processes, n);
cout << processes[i].id << "\t" << processes[i].burstTime << "\t\t" << processes[i].waitingTime << "\t\t"
<< processes[i].turnaroundTime << "\n";
return 0;
}
OUTPUT
P1 3 0 3
P2 1 3 4
P3 7 4 11
P4 4 11 15
P5 5 15 20
P1 0
P2 3
P3 4
P4 11
P5 15
(3 + 4 + 11 + 15 + 20) / 5 = 10.6
(0 + 3 + 4 + 11 + 15) / 5 = 6.6
2)
SJF
#include <iostream>
#include <queue>
#include <vector>
struct Process {
int id;
int burstTime;
int priority;
};
struct CompareSJF {
};
struct ComparePriority {
};
void nonpreemptiveSJF(vector<Process>& processes) {
pq.push(process);
int currentTime = 0;
while (!pq.empty()) {
pq.pop();
cout << "turnaround time = " << turnaroundTime << ", ";
currentTime += currentProcess.burstTime;
pq.push(process);
int currentTime = 0;
while (!pq.empty()) {
pq.pop();
cout << "turnaround time = " << turnaroundTime << ", ";
currentTime += currentProcess.burstTime;
int main() {
vector<Process> processes = {
{1, 3, 2},
{2, 1, 1},
{3, 7, 4},
{4, 4, 2},
{5, 5, 3}
};
nonpreemptiveSJF(processes);
nonpreemptivePriority(processes);
return 0;
}
OUTPUT
Nonpreemptive SJF
P2 1 0
P1 4 1
P4 8 4
P5 13 8
P3 20 13
Nonpreemptive Priority
A,B)Process Turnaround Time Waiting Time
P2 1 0
P1 4 1
P4 8 4
P5 13 8
P3 20 13
#include <queue>
struct Process {
};
// Comparison function for priority queue
struct ComparePriority {
};
int main() {
Process p[] = { {1, 3, 2, 3}, {2, 1, 1, 1}, {3, 7, 4, 7}, {4, 4, 2, 4}, {5, 5, 3, 5} };
queue<Process> q;
int quantum = 2;
q.push(p[i]);
// Initialize the current time and the turnaround time and waiting time arrays
int time = 0;
turnaround_time[i] = waiting_time[i] = 0;
}
// Execute the processes in the queue
while (!q.empty()) {
q.pop();
// Execute the process for the time quantum or the remaining burst time, whichever is smaller
curr.remaining_time -= executed_time;
// Update the current time and the waiting time of all other processes
time += executed_time;
waiting_time[i] += executed_time;
// If the process still has remaining burst time, add it back to the queue with updated priority
if (curr.remaining_time > 0) {
curr.priority++;
q.push(curr);
// Otherwise, calculate the turnaround time of the process and update the turnaround time array
else {
}
// Print the turnaround time and waiting time of each process
cout << "P" << i + 1 << "\t" << turnaround_time[i] << "\t\t\t" << waiting_time[i] << endl;
// Calculate and print the average turnaround time and waiting time
avg_turnaround_time += turnaround_time[i];
avg_waiting_time += waiting_time[i];
avg_turnaround_time /= n;
avg_waiting_time /= n;
cout << "Average Turnaround Time: " << avg_turnaround_time << endl;
cout << "Average Waiting Time: " << avg_waiting_time << endl;
return 0;
}
a. Using Round Robin (quantum = 2):
Assuming that the processes are scheduled in the order P1, P2, P3, P4, P5, we can simulate the
execution of the processes using round-robin scheduling with a time quantum of 2 milliseconds:
Time 0: P1 arrives in the ready queue. Ready queue: P1 Time 2: P1 gets the CPU for 2
milliseconds. Ready queue: P2, P4, P1 Time 4: P2 gets the CPU for 1 millisecond. Ready queue:
P4, P1, P3 Time 6: P4 gets the CPU for 2 milliseconds. Ready queue: P1, P3, P5 Time 8: P1 gets
the CPU for 2 milliseconds. Ready queue: P3, P5 Time 10: P3 gets the CPU for 2 milliseconds.
Ready queue: P5 Time 12: P5 gets the CPU for 2 milliseconds. Ready queue: Empty
Q2
#include<bits/stdc++.h>
struct process
{
int pid, priority, arrival_time, burst_time, completion_time, waiting_time, turnaround_time,
response_time;
};
int main()
int n;
cin>>n;
vector<process> p(n);
p[i].pid = i+1;
cout<<"Priority: ";
cin>>p[i].priority;
cin>>p[i].arrival_time;
cout<<"Burst Time: ";
cin>>p[i].burst_time;
while(completed != n)
process next;
idx = i;
next = p[i];
if(idx != -1)
vis[idx] = true;
p[idx].response_time = p[idx].waiting_time;
completed++;
else
curr_time++;
avg_turnaround_time += p[i].turnaround_time;
avg_waiting_time += p[i].waiting_time;
avg_turnaround_time /= n;
avg_waiting_time /= n;
cout<<p[i].pid<<"\t"<<p[i].priority<<"\t\t"<<p[i].arrival_time<<"\t\t"<<p[i].burst_time<<"\t\
t"<<p[i].completion_time<<"\t\t"<<p[i].waiting_time<<"\t\t"<<p[i].turnaround_time<<"\t\
t"<<p[i].response_time<<"\n";
return 0;
}
OUTPUT
B,C,D
PID Priority Arrival Time Burst Time Completion Time Waiting Time Turnaround
Time Response Time
1 1 0 4 4 0 4
0
2 2 0 3 7 4 7
4
3 1 6 7 14 1 8
1
4 3 11 4 20 5 9
5
5 2 12 2 16 2 4
2
Average Waiting Time: 2.4
Q3
#include <iostream>
#include <queue>
#include <vector>
struct Process {
int id;
float arrival_time;
int burst_time;
int remaining_time;
int completion_time;
int turnaround_time;
int waiting_time;
};
int main() {
vector<Process> processes = {
{0, 0, 3, 3, 0, 0, 0},
{2, 0.9, 7, 7, 0, 0, 0}
};
float current_time = 0;
int completed_processes = 0;
priority_queue<Process> ready_queue;
// Check for newly arrived processes and add them to the ready queue
ready_queue.push(process);
if (!ready_queue.empty()) {
ready_queue.pop();
current_process.completion_time = current_time + 1;
current_process.remaining_time--;
if (current_process.remaining_time > 0) {
ready_queue.push(current_process);
} else {
current_process.turnaround_time = current_process.completion_time -
current_process.arrival_time;
completed_processes++;
current_time++;
// Print results
cout << "P" << process.id << "\t" << process.arrival_time << "\t\t" << process.burst_time << "\t\t"
return 0;
}
output
Process Arrival Time Burst Time Turnaround Time Waiting Time
P0 0 3 0 0
P1 0.3 1 0 0
P2 0.9 7 0 0
B,C,D
d. Using the above algorithm, the average turnaround time is (3 + 1 + 8) / 3 = 4, and the average waiting
time is (0 + 0.7 + 0.3) / 3 = 0.333.