$29
For Windows system, I recommend you to install WSL. https://learn.microsoft.com/en-us/windows/wsl/install
In this section, students are required to complete the following codes to implement several basic operating system scheduling algorithms. For each program, students should capture a screenshot of the successful execution with provided sample input, verify that the output is the same as sample output. In the submission file, please attach the added code and execution screenshot in sequence.
Q1: Complete the code at the blank underline to implements the First-Come First-Served (FCFS) scheduling algorithm. The program reads in a set of processes, each of which includes a Process ID, an Arrival Time(at) and a Service Time(st). The program should schedule the processes on a first-come, first-served basis based on arrival time and calculate the Wait Time(wt) and Turnaround Time(tt) for each process.
Input format.
The first line includes a positive integer n, indicating the number of processes. Each of the next n lines includes three positive integers indicating the process ID, arrival time, and service time, respectively.
Output format.
Outputs the wait time and turnaround time of each process in ascending order of process ID.
Sample input |
Sample output |
3 |
|
1 0 3 |
1 0 3 |
2 2 6 |
2 1 7 |
3 4 4 |
3 5 9 |
Tips.
Q2: Complete the code at the blank underline to implements the Shortest Job First (SJF) scheduling algorithm and answer a questions. The program reads in a set of processes, each of which includes a Process ID, an Arrival Time and a Service Time. The program should schedule the processes on a Shortest Job First basis based on service time (arrival time should also be considered) and calculate the Wait Time and Turnaround Time for each process.
Sample input1(at and st) |
Sample output1(wt and tt) |
3 |
|
0 3 |
0 3 |
2 6 |
5 11 |
3 4 |
0 4 |
Question: Comment out lines 29 to 33 of the code, run the program, and enter sample input2. What happens? Explain why.
Sample input2(at and st) |
3 |
2 3 |
0 1 |
2 2 |
Input(at and st) |
Avg.Wait_time(SJF) |
Avg.Wait_time(PSJF) |
[0 1, 2 7, 3 4] |
2 |
1.33 |
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
#include <stdlib.h>
struct process {
int id;
int arrival_time;
int service_time;
int wait_time;
int turnaround_time;
};
void calculate_times(struct process *p, int n) {
int i, j, time = 0, completed = 0;
int *remaining_time = (int*)malloc(n * sizeof(int));
for (i = 0; i < n; i++) {
remaining_time[i] = p[i].service_time;
}
while (completed != n) {
int shortest_index = -1;
int shortest_time = -1;
for (j = 0; j < n; j++) {
if (p[j].arrival_time <= time && remaining_time[j] > 0) {
if (shortest_time == -1 || remaining_time[j] < shortest_time) {
shortest_index = j;
shortest_time = remaining_time[j];
}
}
}
if (shortest_index == -1) {
time++;
continue;
}
remaining_time[shortest_index]--;
if (remaining_time[shortest_index] == 0) {
completed++;
int k = shortest_index;
p[k].wait_time = time - p[k].arrival_time - p[k].service_time + 1;
if (p[k].wait_time < 0) {
p[k].wait_time = 0;
}
p[k].turnaround_time = time - p[k].arrival_time;
}
time++;
}
free(remaining_time);
}
int main() {
int i, n;
printf("please enter the number of processes:");
scanf("%d", &n);
struct process p[n];
for (i = 0; i < n; i++) {
printf("Please enter the arrival time and execution time of process %d: ", i + 1);
scanf("%d%d", &p[i].arrival_time, &p[i].service_time);
p[i].id = i + 1;
p[i].wait_time = 0;
p[i].turnaround_time = 0;
}
calculate_times(p, n);
printf("Process\tArrival Time\tService Time\tWait Time\tTurnaround Time\n");
int total_wait_time = 0;
int total_turnaround_time = 0;
for (i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i].id, p[i].arrival_time, p[i].service_time, p[i].wait_time, p[i].turnaround_time);
total_wait_time += p[i].wait_time;
total_turnaround_time += p[i].turnaround_time;
}
printf("avg. Wait Time:%.2f\n", (float)total_wait_time/n);
printf("avg. Turnaround Time:%.2f\n", (float)total_turnaround_time/n);
return 0;
}