Question

(C++) I need to write a priority queue(where the processes inside are listed as example: P25:2...

(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

………………..

………………..

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:-

Add a comment
Know the answer?
Add Answer to:
(C++) I need to write a priority queue(where the processes inside are listed as example: P25:2...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT