Question

In: Computer Science

Make a C program that simulates time-sharing in the operating system. Please follow the instructions provided:...

Make 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.

Thank you.

Solutions

Expert Solution

#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;
}


Related Solutions

Create a C program that simulates time-sharing in the operating system. Please follow the instructions provided:...
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...
in c++ please follow instructions and fix the errors and please make a comment next to...
in c++ please follow instructions and fix the errors and please make a comment next to each code error you fix. Below are 25 code fragments, inserted into a try catch block. Each line has zero or more errors. Your task is to find and remove all errors in each fragment. Do not fix problems by simply deleting a statement; repair the problems by changing, adding, or deleting a few characters. There may be different ways to fix them You...
can someone code this problem please? Introduction Students will create a C++ program that simulates a...
can someone code this problem please? Introduction Students will create a C++ program that simulates a Pokemon battle mainly with the usage of a while loop, classes, structs, functions, pass by reference and arrays. Students will be expected to create the player’s pokemon and enemy pokemon using object-oriented programming (OOP). Scenario You’ve been assigned the role of creating a Pokemon fight simulator between a player’s Pikachu and the enemy CPU’s Mewtwo. You need to create a simple Pokemon battle simulation...
Please follow the instructions and solve it by c++ Close the project and create a new...
Please follow the instructions and solve it by c++ Close the project and create a new one called 'Lab0-Part3' with the same options as in the previous step. Write a C++ program that does the following: Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console.
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the following: 1) Your .cpp file of the solution. 2) For what n value (approximately) does your computer stop producing output? Why is this? Enter your answer in the 'Comments' field when you submit the file.   So far in our study of recursion we identified a few common recursive number sequences, such as the Fibonacci numbers. Number sequences like this are not recursive algorithms themselves...
C++ Follow Instructions Please By placing a break in the IIF statement you are using it...
C++ Follow Instructions Please By placing a break in the IIF statement you are using it to force an exit from the WHILE loop. The break statement is only allowed with the SELECT/CASE statement. Also, don't use one structure to force an exit from another. Use the logic from the WHILE loop to end by removing the IF with the break #include <iostream> using namespace std; bool IsMultiple(int,int); int main() { char choice; int n1,n2; bool result; int test =...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Instructions: Please read problem carefully. Write the answers on the answer sheets provided. Make sure that...
Instructions: Please read problem carefully. Write the answers on the answer sheets provided. Make sure that you clearly show all of the work that went in to solving the problem. Partial credit will be awarded on each part of the problem, so make sure that you attempt each part. You are the marketing manager for TotalControl, an electronics company in Orangeburg. Your company produces a line of remote controls for use in home theaters. The company’s main product, the UR...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements the algorithm given in Example 1 - 3 (Chapter 1), which determines the monthly wages of a salesperson. The instructions for Example 1 - 3have been posted below for your convenience. Example 1 - 3 Every salesperson has a base salary. The salesperson also receives a bonus at the end of each month, based on the following criteria: If the salesperson has been with...
Please make it simply and easy for a beginner to follow.. -Write in C++ -Use Char...
Please make it simply and easy for a beginner to follow.. -Write in C++ -Use Char library functions -Must show that is runs Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT