(C++) I need to write a priority queue(where the processes inside are listed as example: P25:2 , 25 being a randomly assigned ID(1-50) and 2 being a randomly assigned priority number(1-10). Once the priority queue is full(10 max), it will transfer the highest priority process to a waiting queue. Once the waiting queue is full(7 max), one process(highest priority) will transfer to a ready queue where the system would perform the process in a round robin style. Once 25 processes have been "performed" the operation finishes.
Example output:
Step 0:
The new generated process is P10 with priority 5
The processes in the priority queue are: P5:1 P3:3 P2:2 P1:4 P7:5 P4:4 P8:7 P9:6 P10:5 P6:7
The processes in the waiting queue are:
The processes in the ready queue is
Step 1:
The new generated process is P11 with priority 8
The processes in the priority queue are: P2:2 P3:3 P4:4 P1:4 P7:5 P6:7 P8:7 P9:6 P10:5 P11:8
The processes in the waiting queue are: P5:1
The processes in the ready queue is
Step 2:
The new generated process is P12 with priority 7
The processes in the priority queue are: P3:3 P1:4 P4:4 P10:5 P7:5 P6:7 P8:7 P9:6 P11:8 P12:7
The processes in the waiting queue are: P5:1 P2:2
The processes in the ready queue is
Step 3:
The new generated process is P13 with priority 2
The processes in the priority queue are: P13:2 P1:4 P4:4 P9:6 P10:5 P6:7 P8:7 P12:7 P11:8 P7:5
The processes in the waiting queue are: P5:1 P2:2 P3:3
The processes in the ready queue is
………………..
………………..
LOOK THE PROGRAM FILE SINCE SOME COMMENTS WHICH ARE LONG MAY SPLIT INTO TWO LINES WHILE COPY AND PASTE WHICH MAY CAUSE ERORS.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iterator>
#include <map>
#include <queue>
using namespace std;
typedef pair<int, int> pi; //defining the type of pi
void shuffle(int *arr, size_t n) // shuffle method is shuffle so
that process will assigned with random numbers
{
if (n > 1) // checking whether the size greater than one
{
size_t i;
srand(time(NULL)); // Use current time as seed for random
generator
for (i = 0; i < n - 1; i++)
{
size_t j = i + rand() / (RAND_MAX / (n - i) + 1); // finding a
number between 1 and n and only be foound once
int t = arr[j]; //saving the number in the jth index to t
//swapping the ith and jth element of the array
arr[j] = arr[i];
arr[i] = t;
}
}
}
int main()
{
int arr[50],i,j,k;
map<int,int> dict; //Initializing the map dict
//defining priority queue,waiting queue and ready queue along
tempporary queue to store the duplicate of these queue
priority_queue<pi, vector<pi>, greater<pi> >
pq;
priority_queue<pi, vector<pi>, greater<pi> >
temp_pq;
queue< pair<int, int> > wq;
queue< pair<int, int> > temp_wq;
queue< pair<int, int> > rq;
queue< pair<int, int> > temp_rq;
for(i=0;i<50;i++)//inserting numbers from 0 - 49 to the array
of process number
{
arr[i]=i;
}
shuffle(arr,50); //calling the shuffle function,so that array is
shuffled which is same as storing random numbers from 0-49
for(int i=0;i<50;i++)
{
dict.insert(pair<int,int>(arr[i],(rand()% 10 + 1))); //
insering random priorities as value to the corresponding process
which is key in map
}
map<int, int>::iterator itr; //defining map iterator
i=0,j=0,k=0; //intialize the iterator value of priority
queue,waiting queue and for stopping after 25 process
for (itr = dict.begin(); itr != dict.end(); ++itr) { //iterating
through the map
if(k>=25) //if 25 process are loaded then we exit
break;
if(i<10) //inserting elements to the priority queue only if the
number of element in priority is less than 10
{
pq.push(make_pair(itr->second,itr->first)); //inserting
elements to the priority queue
i++; //incrementing iterator of priority queue
}
if(i==10) //if priority queue is full i.e 10 elements
{
pair<int,int> top = pq.top(); //saving the top into a pair
top
wq.push(make_pair(top.first,top.second)); //inserting that pair
into waiting queue
pq.pop(); //poping the highest priority element from priority
queue
j++;//incrementing iterator of waiting queue
i--;//decrementing iterator of priority queue
}
if(j == 7)
{
pair<int,int> top = wq.front(); //saving the front of waiting
queue into a pair top
rq.push(make_pair(top.first,top.second));//inserting that pair into
ready queue
wq.pop(); //poping the element from waiting queue
j--;//decrementing iterator of waiting queue
}
temp_pq = pq; //saving the priority queue into tempporary priority
queue
temp_rq = rq; //saving the waiting queue into tempporary waiting
queue
temp_wq = wq; //saving the ready queue into tempporary ready
queue
std::cout << "\nprocess in priority_queue are :-" ;
while (!temp_pq.empty()) //printing the priority queue
{
pair<int,int> top = temp_pq.top();
std::cout << "P" << top.second+1 << ":" <<
top.first << " "; //top.second+1 is because the iteration
starts from 0
temp_pq.pop();
}
std::cout << "\nprocess in waiting queue are :-" ;
while (!temp_wq.empty()) //printing the waiting queue
{
pair<int,int> top = temp_wq.front();
std::cout << "P" << top.second+1 << ":" <<
top.first << " ";
temp_wq.pop();
}
std::cout << "\nprocess in ready queue are :-" ;
while (!temp_rq.empty()) //printing the ready queue
{
pair<int,int> top = temp_rq.front();
std::cout << "P" << top.second+1 << ":" <<
top.first << " ";
temp_rq.pop();
}
cout <<
"\n---------------------------------------------\n";
cout <<
"---------------------------------------------\n";
cout <<
"---------------------------------------------\n";
k++; //incrementing iterator for stopping the loading of processes
after 25 process
}
return 0;
}
PROGRAM FILE
OUTPUT:-
(C++) I need to write a priority queue(where the processes inside are listed as example: P25:2...
C program Read file problem: For example, I have an input.txt file like this: q 1 tq 4 p1 10 p2 5 p3 7 p4 20 p5 17 p6 9 p7 3 p8 11 p9 15 p10 1 I want the num of q is in one array, tq is in one array, the order of process(ie,p1,p2) is in one array, and process time is in one array(process time is the number after like p1,p2). There may many q,tq,p so...
implement MLFQ using C++ please preferably linked list P1 {4,24,5,73,3,31,5,27,4,33,6,43,4,64,5,19,2} P2 {18,31,19,35,11,42,18,43,19,47,18,43,17,51,19,32,10} P3 {6,18,4,21,7,19,4,16,5,29,7,21,8,22,6,24,5} P4 {17,42,19,55,20,54,17,52,15,67,12,72,15,66,14} P5 {5,81,4,82,5,71,3,61,5,62,4,51,3,77,4,61,3,42,5} P6 {10,35,12,41,14,33,11,32,15,41,13,29,11} P7 {21,51,23,53,24,61,22,31,21,43,20} P8 {11,52,14,42,15,31,17,21,16,43,12,31,13,32,15} will compute the overall wait times, response times, and turnaround times for each process and averages for FCFS the processes follow {CPU, IO, CPU, IO, ...} output should look like this Now Running: P1 Ready Queue: Process Burst P2 18 P3 6 P4 17 P5 5 P6 10 P7 21 P8 11 Now In I/O: Process...
In this exercise, you will illustrate a small network and calculate some of its basic properties. For this case, it will be a social network though any network could do. You will calculate PageRank for the network. Given the following people (nodes) and relations (edges): Nodes: P1: Tom, P2: Jean, P3: Nat, P4: Steven, P5: Vijay, P6: Mary, P7: Oscar, P8: Michelle, P9: Jian, P10: Clark Edges: P1 -> P4 P2 -> P1, P2 -> P4 P3 -> P2, P4...
1. Fill in the table below for the following compounds- molecules and polyatomic ions. For each compound, calculate the number of available electrons for all the atoms in the molecule and determine what the central atom in the molecule will be. Central Atom Total Number Molecular of Available Formula Electrons HCN P1 P2 SO3 P3 P4 NO3 P5 P6 P7 P8 SIH4 P9 P10 SO2CI2 NH4 P11 P12 P13 P14 PO43 P15 P16 PF5 SF&C P18 P17 P19 P20 XeF2
Please answer the following question in C++ language Consider the following set of processes, with the length of the CPU burst time given in milliseconds: Process Burst Time Priority P1. 7 5 P2 2 4 P3 11 3 P4 9 1 P5 5 3 The processes are assumed to have arrived in the order P1,P2, P3, P4, P5, all at time 0. a. Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive...
8. Consider the following 5 processes in the ready queue: Process Burst Priority Arrival time n w P1 P2 w N P3 P P - W WE O P4 N P5 N A O Draw Gantt charts illustrating the execution of these processes for each of the following algorithms: (a) preemptive SJF, (b) RR (with quantum = 1), (C) FCFS, and (d) preemptive priority, and calculate the respective turnaround and waiting times.
(d) Pre-emptive priority scheduling [4 marks]: Consider the following set of processes, with the length of the CPU-burst time given in milliseconds: Process Burst Priority Arrival Time P1 8 3 0 P2 3 4 1 P3 6 2 3 P4 3 1 5 P5 1 5 7 P6 3 8 14 P7 8 5 18 (d-1) Draw Gantt chart illustrating the execution of these processes using pre-emptive priority (a smaller priority number implies a higher priority) [2 marks] 00 01 ...
Memory Allocation a) Is it possible to have both internal and external fragmentation with fixed partition memory allocation? How about with dynamic/variable memory partition allocation? Explain. (1 point) b) Suppose we have 1000K of memory where the first 100K is reserved. Also suppose that the following processes have been allocated memory in the following order: P1: 100K, P2: 100K, P3: 25K, P4: 200K, P5: 200K, P6: 75K, P7: 100K. Create a diagram of memory. Suppose that P2, P4, and P6...
4. Consider a ready queue with four processes :- Process Arrival Time Burst Time (ms) P1 Priority P2 P3 P4 P5 For each of the following CPU scheduling algorithms, determine the turnaround and average waiting time for each of the process :- a. Shortest remaining Time First b. Shortest Job First C. Priority Scheduling (Both pre-emptive and non-preemptive) d. Round Robin (quantum is 1 ms)
Topic Round Robin Answer the following using the First Come First Serve Scheduling Algorithm SHOW COMPLETE SOLUTION 1. Process AT BT P1 1 10 P2 0 15 P3 2 8 P4 5 7 P5 6 5 P6 9 3 P7 8 4 P8 3 5 P9 4. 20