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

P 8

This document contains a C++ program that simulates process scheduling by calculating waiting and turnaround times for a set of processes. It defines a Process structure to store arrival and burst times, and includes functions to compute waiting and turnaround times, as well as to print the results. The program prompts the user to input the number of processes and their respective arrival and burst times.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

P 8

This document contains a C++ program that simulates process scheduling by calculating waiting and turnaround times for a set of processes. It defines a Process structure to store arrival and burst times, and includes functions to compute waiting and turnaround times, as well as to print the results. The program prompts the user to input the number of processes and their respective arrival and burst times.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <vector>

using namespace std;

// Process structure to store arrival time and burst time


struct Process {
int arrivalTime;
int burstTime;
};

// Function to calculate waiting time for each process


void calculateWaitingTime(vector<Process>& processes) {
int waitingTime = 0;
for (int i = 1; i < processes.size(); ++i) {
waitingTime += processes[i - 1].burstTime;
processes[i].waitingTime = waitingTime;
}
}

// Function to calculate turnaround time for each process


void calculateTurnaroundTime(vector<Process>& processes) {
for (int i = 0; i < processes.size(); ++i) {
processes[i].turnaroundTime = processes[i].burstTime +
processes[i].waitingTime;
}
}

// Function to print the results


void printResults(const vector<Process>& processes) {
cout << "Process | Arrival Time | Burst Time | Waiting Time | Turnaround Time" <<
endl;
cout << "-------|--------------|-----------|-------------|------------------" <<
endl;
for (const Process& process : processes) {
cout << process.arrivalTime << " | " << process.burstTime << " | "
<< process.waitingTime << " | " << process.turnaroundTime << endl;
}
}

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

// Create a vector to store processes


vector<Process> processes(numberOfProcesses);

// Get arrival time and burst time for each process


cout << "Enter arrival time and burst time for each process:" << endl;
for (int i = 0; i < numberOfProcesses; ++i) {
cout << "Process " << (i + 1) << ":" << endl;
cout << "Arrival Time: ";
cin >> processes[i].arrivalTime;
cout << "Burst Time: ";
cin >> processes[i].burstTime;
}

// Calculate waiting time and turnaround time


calculateWaitingTime(processes);
calculateTurnaroundTime(processes);

// Print the results


printResults(processes);

return 0;
}

You might also like