In: Computer Science
Create a C program that simulates time-sharing in the operating system.
Please follow the instructions provided:
a) Use a circular queue.
b) It is required that the process be inputted by the user. The user must input the process name and the duration in seconds, and for this simulation let the user input 5 processes.
c) As this requires process name and duration, use an array of structures.
d) To simulate time-sharing, following the algorithm presented below:
d.1) Use the sleep() function to simulate the time duration in
seconds.
d.2) The CPU processes the job at the head of the queue.
d.3) The maximum time each process can execute is 10 seconds.
d.3.1) If a process has a duration of more than 10 seconds, then after running for 10 seconds it is timed out and returned to the queue (and reprocessed later).
d.3.2) Do not forget to reduce the time duration of the job by 10 seconds.
d.4) If a process has a remaining time to execute of x seconds (where x <= 10), then this process can execute until the x seconds is consumed. The process is then terminated.
d.5) As the process is executed, display the process name as well as the remaining time after it timed out.
d.6) While there are still processes left in the queue, repeat step 3b. Otherwise, the program terminates. The program shall display a message upon completion of all the jobs in the queue.
e) To test your program, try out four different sets of processes.
e.1) There should be five processes per set and each process should have its corresponding process name and duration in seconds.
e.2) In the input, if the duration is zero or negative, your program should warn the user about it and should ask the user to input again the duration.
#include <Windows.h>
#include<bits/stdc++.h>
using namespace std;
#define MAX 5
// Process Structure
struct Process {
char p_name[100]; // Process Name
int p_time; // Process Time
};
int itemCount = 0;
struct Process pro[MAX];
// Return First Process from Queue
Process getTop() {
return pro[0];
}
// Return whether Queue is empty or not
bool isEmpty() {
return itemCount==0;
}
// Insert Process
void insert(Process p) {
pro[itemCount] = p;
itemCount++;
}
// Remove Process
void removeData() {
for(int i=1;i<itemCount;i++) {
pro[i-1] = pro[i];
}
itemCount--;
}
// Main Function
int main() {
int time;
char name[100];
for(int i=0;i<MAX;i++) {
struct Process p;
scanf("%s",&p.p_name);
scanf("%d",&p.p_time);
while(p.p_time <= 0) {
printf("Process Time must be greater than or Equal to 0.");
scanf("%d",&p.p_time);
}
insert(p);
}
while(!isEmpty()) {
Process top = getTop();
if(top.p_time <= 10) {
printf("%s process is started and ends in %d
sec.\n",top.p_name,top.p_time);
for(int i=0;i<top.p_time;i++) {
printf("%d sec passed...\n",i+1);
Sleep(1000);
}
removeData();
}
else {
printf("%s process is started and ends in 10
sec.\n",top.p_name);
for(int i=0;i<10;i++) {
printf("%d sec passed...\n",i+1);
Sleep(1000);
}
top.p_time = top.p_time-10;
printf("10 sec reduced in %s process with time left %d
sec.\n",top.p_name,top.p_time);
removeData();
insert(top);
}
}
printf("All Processes are executed...");
return 0;
}
THANK YOU!! PLEASE VOTE