Question

In: Computer Science

Please do not use vectors or any previously posted code Write a C++ program which reads...

Please do not use vectors or any previously posted code

Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g.

ProcessA 4

ProcessB 10

Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data structure. You should use the alarm system call to schedule a timer interrupt every 3 seconds. The interrupt handler should pick the next process from the process list, write a message saying how much time it has left to execute,

i.e.

ProcessA 4

Then update its time left to execute by subtracting 3 seconds and return it to the end of the queue. If the process had no time left to execute, you should write a message saying this

i.e.

ProcessA Finished

And delete this process from the linked list.

If there are no processes left to execute, write a message saying

No processes left

And terminate your program.

If further information is needed please specifically comment what is needed.

Solutions

Expert Solution

#include <stdio.h>
#include <bits/stdc++.h>
#include <signal.h>
#include <unistd.h>
using namespace std;
volatile sig_atomic_t print_flag = false;

void handle_alarm( int sig ) {
print_flag = true;
}

int main() {
vector<pair<string,int>> processlist; //vector to contain the process and time
int n;
cout<<"Enter the number of processes"<<endl;
cin>>n;
cout<<"Enter the processes name and time: "<<endl;
while (n-->0){
string s;
int t;
cin>>s>>t; // read the process and time
processlist.push_back(make_pair(s,t)); //insert the current process in the list
}
signal( SIGALRM, handle_alarm ); // Install handler first,
alarm( 2 ); // before scheduling it to be called.
while (1) {
if ( print_flag ) {
if (processlist.size()>0){ //if list has still process left
if (processlist[0].second<=0){ // if process runs out of time
cout<<processlist[0].first<<" Finished"<<endl;
processlist.erase(processlist.begin()); //remove the process from the list
}
else{ //if process does not run out of time
cout<<processlist[0].first<<" "<<processlist[0].second<<endl; //print
pair<string,int> pa = make_pair(processlist[0].first,processlist[0].second-2);
processlist.erase(processlist.begin()); //remove the process from the front
processlist.push_back(pa); //add the same process with time reduced by 2 sec at the end of list
}
}
else{ //all processes has been finished
cout<<"No processes left"<<endl;
break;
}
print_flag = false;
alarm( 2 );
}
}
}

OUTPUT:


Related Solutions

PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q (4) Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the 1st token...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
***Please do not re post already posted answer to this question that was previously posted and...
***Please do not re post already posted answer to this question that was previously posted and answered. I need to see a new income statement and retained earnings statement with the breakdown of how certain totals are being reached. Thank you! Please help with creating a corrected 2021 multi-step income statement with EPS disclosure and a 2021 retained earnings statement and in good form with the information below. Please provide explanations to how you came up with totals for each...
Code is in C Write a program that reads integers from stdin. Once it reaches the...
Code is in C Write a program that reads integers from stdin. Once it reaches the * end of the input, it prints the smallest absolute value among those * of the numbers it read. * * For example, if * 4, 6 -3, 3, -2, 13, -4 * are read from stdin, the program should print 2. * * If the end of file is reached before any integer is seen, the * number printed should be INT_MAX (defined...
Write a program which reads the matrix A (2x2 matrix taking the first two column vectors...
Write a program which reads the matrix A (2x2 matrix taking the first two column vectors from the input file) and the vector b (the third column vector) and solve for x in Ax=b for general A. If there is a unique solution, your output should be a 2x2 matrix with two lines and two numbers per line. The output should contain numbers with up to four significant digits. If the system is unsolvable or inconsistent, then your output should...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT