Write the code in C++ with 4 requirements:1、 Simulate the operation of the round-robin algorithm for process scheduling. 2、 Create at least 15 processes and output their scheduling situation under the scheduling algorithm mentioned above and output it to the terminal to check the execution of the algorithm. 3、 The output should include the arrival time of the processes, the end time, and the average execution time 4.use the function fork() to create process
时间: 2024-01-25 08:02:20 浏览: 75
jquery.simulate.drag-sortable.js:在JQuery UI Sortable小部件内模拟拖动事件(主要用于集成测试)
```
#include <iostream>
#include <unistd.h>
#include <queue>
using namespace std;
struct Process {
int pid;
int arrival_time;
int burst_time;
int remaining_time;
int end_time;
};
int main() {
int quantum = 2; // time quantum for round-robin algorithm
int n = 15; // number of processes
int time = 0; // current time
float total_exec_time = 0; // total execution time of all processes
queue<Process> ready_queue; // queue to hold processes that are ready to execute
for (int i = 0; i < n; i++) {
int pid = fork(); // create a new process using fork()
if (pid == 0) { // child process
srand(getpid()); // seed the random number generator with the process ID
int arrival_time = rand() % 10; // randomly generate arrival time between 0 and 9
int burst_time = rand() % 5 + 1; // randomly generate burst time between 1 and 5
cout << "Process " << getpid() << " created with arrival time " << arrival_time << " and burst time " << burst_time << endl;
sleep(arrival_time); // simulate wait time until arrival
for (int j = 0; j < burst_time; j++) {
cout << "Process " << getpid() << " executing at time " << time << endl;
time++;
if (j == burst_time - 1) { // process finished executing
cout << "Process " << getpid() << " finished executing at time " << time << endl;
exit(0); // exit child process
}
else if ((j + 1) % quantum == 0) { // time quantum expired
cout << "Process " << getpid() << " time quantum expired at time " << time << endl;
ready_queue.push({getpid(), arrival_time, burst_time, burst_time - (j + 1), 0}); // add process to ready queue with updated remaining time
exit(0); // exit child process
}
}
}
}
while (!ready_queue.empty() || time == 0) { // execute processes until all have finished executing
while (!ready_queue.empty() && ready_queue.front().arrival_time <= time) { // add processes from ready queue to execute queue
Process p = ready_queue.front();
ready_queue.pop();
cout << "Process " << p.pid << " added to execution queue at time " << time << endl;
}
if (!ready_queue.empty()) { // execute next process in execute queue
Process p = ready_queue.front();
p.remaining_time--;
if (p.remaining_time == 0) { // process finished executing
p.end_time = time + 1;
total_exec_time += p.end_time - p.arrival_time;
cout << "Process " << p.pid << " finished executing at time " << p.end_time << endl;
ready_queue.pop();
}
else if ((p.burst_time - p.remaining_time) % quantum == 0) { // time quantum expired
cout << "Process " << p.pid << " time quantum expired at time " << time << endl;
ready_queue.pop();
ready_queue.push(p); // add process back to ready queue with updated remaining time
}
else { // continue executing process
cout << "Process " << p.pid << " executing at time " << time << endl;
ready_queue.pop();
ready_queue.push(p); // add process back to ready queue with updated remaining time
}
}
time++;
}
float avg_exec_time = total_exec_time / n;
cout << "Average execution time: " << avg_exec_time << endl;
return 0;
}
```
阅读全文