Question

In: Computer Science

Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...

Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display:
(i) Gantt chart and determine the following:
(ii) Determine the Turnaround time(TAT), waiting time(WT) of each process
(iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT)
of all processes.
please write the comments

Solutions

Expert Solution

Preemptive Priority scheduling - each process has a priority associated with it and process with higher priority are executed first.

Implementing a Preemptive Priority scheduling algorithm using C++

#include<iostream>
#include<algorithm>
using namespace std;
struct node{
char pname;
int btime;
int atime;
int priority;
int restime=0;
int ctime=0;
int wtime=0;
}a[1000],b[1000],c[1000]; // declare 3 arrays
void insert(int n){
int i;
for(i=0;i<n;i++){ // for loop to get input from user
cin>>a[i].pname;
cin>>a[i].priority;
cin>>a[i].atime;
cin>>a[i].btime;
a[i].wtime=-a[i].atime+1;
}
}
bool btimeSort(node a,node b){
return a.btime < b.btime;
}
bool atimeSort(node a,node b){
return a.atime < b.atime;
}
bool prioritySort(node a,node b){ // sorting according to priority
return a.priority < b.priority;
}
int k=0,f=0,r=0;
void disp(int nop,int qt){
int n=nop,q;
sort(a,a+n,atimeSort);
int ttime=0,i;
int j,tArray[n];
int alltime=0;
bool moveLast=false;
for(i=0;i<n;i++){
alltime+=a[i].btime;
}
alltime+=a[0].atime;
for(i=0;ttime<=alltime;){
j=i;
while(a[j].atime<=ttime&&j!=n){
b[r]=a[j];
j++;
r++;
}
if(r==f){
c[k].pname='i';
c[k].btime=a[j].atime-ttime;
c[k].atime=ttime;
ttime+=c[k].btime;
k++;
continue;
}
i=j;
if(moveLast==true){
sort(b+f,b+r,prioritySort);
// b[r]=b[f];
// f++;
// r++;
}
j=f;
if(b[j].btime>qt){
c[k]=b[j];
c[k].btime=qt;
k++;
b[j].btime=b[j].btime-qt;
ttime+=qt;
moveLast=true;
for(q=0;q<n;q++){
if(b[j].pname!=a[q].pname){
a[q].wtime+=qt;
}
}
}
else{
c[k]=b[j];
k++;
f++;
ttime+=b[j].btime;
moveLast=false;
for(q=0;q<n;q++){
if(b[j].pname!=a[q].pname){
a[q].wtime+=b[j].btime;
}
}
}
if(f==r&&i>=n)
break;
}
tArray[i]=ttime;
ttime+=a[i].btime;
for(i=0;i<k-1;i++){
if(c[i].pname==c[i+1].pname){
c[i].btime+=c[i+1].btime;
for(j=i+1;j<k-1;j++)
c[j]=c[j+1];
k--;
i--;
}
}
  
int rtime=0;
for(j=0;j<n;j++){
rtime=0;
for(i=0;i<k;i++){
if(c[i].pname==a[j].pname){
a[j].restime=rtime;
break;
}
rtime+=c[i].btime;
}
}
float averageWaitingTime=0;
float averageResponseTime=0;
float averageTAT=0;
  
cout<<"\nGantt Chart\n"; // draw Gantt chart
rtime=0;
for (i=0; i<k; i++){
if(i!=k)
cout<<"| "<<'P'<< c[i].pname << " ";
rtime+=c[i].btime;
for(j=0;j<n;j++){
if(a[j].pname==c[i].pname)
a[j].ctime=rtime;
}
}
cout<<"\n";
rtime=0;
for (i=0; i<k+1; i++){
cout << rtime << "\t";
tArray[i]=rtime;
rtime+=c[i].btime;
}
cout<<"\n";
cout<<"\n";
cout<<"P.Name Priority AT\tBT\tCT\tTAT\tWT\tRT\n"; // draw table with all information
for (i=0; i<nop&&a[i].pname!='i'; i++){
if(a[i].pname=='\0')
break;
cout <<'P'<< a[i].pname << "\t";
cout << a[i].priority << "\t";
cout << a[i].atime << "\t";
cout << a[i].btime << "\t";
cout << a[i].ctime << "\t";
cout << a[i].wtime+a[i].ctime-rtime+a[i].btime << "\t";
averageTAT+=a[i].wtime+a[i].ctime-rtime+a[i].btime;
cout << a[i].wtime+a[i].ctime-rtime << "\t";
averageWaitingTime+=a[i].wtime+a[i].ctime-rtime;
cout << a[i].restime-a[i].atime << "\t";

cout <<"\n";
}

// calculating average waiting time and turn around time

cout<<"Average Waiting time: "<<(float)averageWaitingTime/(float)n<<endl;
cout<<"Average TA time: "<<(float)averageTAT/(float)n<<endl;
}
int main(){
int nop,choice,i,qt;
cout<<"Enter number of processes\n";
cin>>nop;
cout<<"Enter process, priority, AT, BT\n";
insert(nop);
disp(nop,1);
return 0;
}

Note - As per the policy answering only first part


Related Solutions

Develop an algorithm and implement Multilevel Queuing scheduling using C++.and using bubble sorting. Consider a set...
Develop an algorithm and implement Multilevel Queuing scheduling using C++.and using bubble sorting. Consider a set of user and system processes and determine (i) turnaround time (ii) waiting time of each process (iii) Average Waiting Time (AWT) and (iv) Average Turnaround Time (ATAT) of all processes. please write comment
The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each process is...
The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each process is assigned a numerical priority,with a higher number indicating a higher relative priority. The scheduler will execute the highest-priority process. For processes with the same priority, a round-robin scheduler will be used with a time quantum of 10 units. If a process is preempted by a higher-priority process, the preempted process is placed at the end of the queue. Process            Burst Time Arrival Time...
Question 5 The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each...
Question 5 The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each process is assigned a numerical priority,with a higher number indicating a higher relative priority. The scheduler will execute the highest-priority process. For processes with the same priority, a round-robin scheduler will be used with a time quantum of 10 units. If a process is preempted by a higher-priority process, the preempted process is placed at the end of the queue. Process          Burst Time      Arrival Time   Priority P1                15            0            8...
Implement the CPU scheduling algorithm SJF non-preemptive in python. Have the program answer the table. Simulate...
Implement the CPU scheduling algorithm SJF non-preemptive in python. Have the program answer the table. Simulate and evaluate with the set of eight processes below. Assumptions: All processes are activated at time 0 Assume that no process waits on I/O devices. After completing an I/O event, a process is transferred to the ready queue. Waiting time is accumulated while a process waits in the ready queue. Turnaround time is a total of (Waiting time) + (CPU burst time) + (I/O...
Implement the CPU scheduling algorithm FCFS non-preemptive in python. Have the program answer the table. Simulate...
Implement the CPU scheduling algorithm FCFS non-preemptive in python. Have the program answer the table. Simulate and evaluate with the set of eight processes below. Assumptions: All processes are activated at time 0 Assume that no process waits on I/O devices. After completing an I/O event, a process is transferred to the ready queue. Waiting time is accumulated while a process waits in the ready queue. Turnaround time is a total of (Waiting time) + (CPU burst time) + (I/O...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page faults and page hits by considering the Frame size=4, ReferenceString:2 4 6 7 8 2 4 9 13 9 2 7 2 6 1 4 9 2
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID,...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID, name, designation and department using linked list and perform the following operations on the list. Add employee details based on department Remove employee details based on ID if found, otherwise display appropriate message Display employee details Count the number of employees in each department
Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort,...
Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort, insertion sort, and merge sort, It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674...
Describe how the Round Robin Scheduling algorithm works. Explain the differences of working procedure between preemptive...
Describe how the Round Robin Scheduling algorithm works. Explain the differences of working procedure between preemptive and non-preemptive version of this algorithm.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT