0% found this document useful (0 votes)
3 views4 pages

230101098-lab3

The document contains two C++ programs that implement CPU scheduling algorithms: Longest Job First (LJF) and Shortest Remaining Time First (SRTF). Each program prompts the user for process details, calculates completion, turnaround, and waiting times, and outputs the average turnaround and waiting times. The LJF program prioritizes processes with the longest burst time, while the SRTF program focuses on the shortest remaining time for execution.

Uploaded by

sinhaaaryankumar
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)
3 views4 pages

230101098-lab3

The document contains two C++ programs that implement CPU scheduling algorithms: Longest Job First (LJF) and Shortest Remaining Time First (SRTF). Each program prompts the user for process details, calculates completion, turnaround, and waiting times, and outputs the average turnaround and waiting times. The LJF program prioritizes processes with the longest burst time, while the SRTF program focuses on the shortest remaining time for execution.

Uploaded by

sinhaaaryankumar
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/ 4

Aaryan Kumar Sinha

230101098

Program: Write a program to implement Longest job first.

#include <iostream>

using namespace std;

struct Process {
int id, arrival, burst, completion, turnaround, waiting;
};

int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;

Process processes[n];

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


cout << "Enter Arrival Time and Burst Time for Process " << i + 1 << ": ";
processes[i].id = i + 1;
cin >> processes[i].arrival >> processes[i].burst;
processes[i].completion = 0;
}

int time = 0, completed = 0;


while (completed < n) {
int idx = -1, maxBurst = -1;

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


if (processes[i].arrival <= time && processes[i].completion == 0) {
if (processes[i].burst > maxBurst) {
maxBurst = processes[i].burst;
idx = i;
}
}
}

if (idx == -1) {
time++;
} else {
processes[idx].completion = time + processes[idx].burst;
processes[idx].turnaround = processes[idx].completion - processes[idx].arrival;
processes[idx].waiting = processes[idx].turnaround - processes[idx].burst;
time += processes[idx].burst;
completed++;
}
}

cout << "\nProcess\tAT\tBT\tCT\tTAT\tWT\n";


float totalTAT = 0, totalWT = 0;
for (int i = 0; i < n; i++) {
cout << processes[i].id << "\t" << processes[i].arrival << "\t" << processes[i].burst << "\t"
<< processes[i].completion << "\t" << processes[i].turnaround << "\t" <<
processes[i].waiting << "\n";
totalTAT += processes[i].turnaround;
totalWT += processes[i].waiting;
}

cout << "\nAverage Turnaround Time: " << totalTAT / n << endl;
cout << "Average Waiting Time: " << totalWT / n << endl;

return 0;
}
Output:
Program: Write a program to implement SRTF

#include<iostream>
#include<climits>
using namespace std;

int main() {
​ int n;
​ int bt[20], at[20], ct[20], wt[20], pid[20], tat[20], remaining_bt[20];
​ float avgtat = 0, avgwt = 0;

​ cout << "Enter the number of processes (Max 20): ";


​ cin >> n;

​ cout << "Enter unique process ids: ";


​ for (int i = 0; i < n; i++) {
​ cin >> pid[i];
​ }

​ cout << "Enter arrival times: ";


​ for (int i = 0; i < n; i++) {
​ cin >> at[i];
​ }

​ cout << "Enter burst times: ";


​ for (int i = 0; i < n; i++) {
​ cin >> bt[i];
​ remaining_bt[i] = bt[i];
​ }

​ int time = 0, completed = 0;


​ bool isCompleted[20] = {false};

​ while (completed < n) {


​ int shortest = -1;
​ int minRemainingTime = INT_MAX;

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


​ if (at[i] <= time && !isCompleted[i] && remaining_bt[i] < minRemainingTime) {
​ minRemainingTime = remaining_bt[i];
​ shortest = i;
​ }
​ }
​ if (shortest == -1) {
​ time++;
​ continue;
​ }

​ remaining_bt[shortest]--;
​ time++;

​ if (remaining_bt[shortest] == 0) {
​ ct[shortest] = time;
​ tat[shortest] = ct[shortest] - at[shortest];
​ wt[shortest] = tat[shortest] - bt[shortest];
​ isCompleted[shortest] = true;
​ completed++;
​ }
​ }

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


​ avgtat += tat[i];
​ avgwt += wt[i];
​ }

​ avgtat /= n;
​ avgwt /= n;

​ cout << "Average Turnaround Time: " << avgtat << endl;
​ cout << "Average Waiting Time: " << avgwt << endl;

​ return 0;
}

Output:

You might also like