0% found this document useful (0 votes)
2 views6 pages

4th Os Experiement

The document presents a C++ program that implements the Priority CPU Scheduling algorithm. It defines a structure for processes and includes functions for inserting processes into a priority heap, extracting the minimum priority process, and calculating average waiting, response, and turnaround times. The program outputs the order of process execution along with the average metrics after scheduling.

Uploaded by

pandmridul694
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

4th Os Experiement

The document presents a C++ program that implements the Priority CPU Scheduling algorithm. It defines a structure for processes and includes functions for inserting processes into a priority heap, extracting the minimum priority process, and calculating average waiting, response, and turnaround times. The program outputs the order of process execution along with the average metrics after scheduling.

Uploaded by

pandmridul694
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Write a program to implement Priority CPU Scheduling algorithm.

#include <iostream>
#include <vector>
#include <algorithm>

struct Process {
int processID;
int burstTime;
int tempburstTime;
int responsetime;
int arrivalTime;
int priority;
int outtime;
int intime;
};

void insert(Process Heap[], Process value, int* heapsize, int* currentTime) {


int start = *heapsize;
Heap[*heapsize] = value;
if (Heap[*heapsize].intime == -1)
Heap[*heapsize].intime = *currentTime;
++(*heapsize);

while (start != 0 && Heap[(start - 1) / 2].priority > Heap[start].priority)


{
Process temp = Heap[(start - 1) / 2];
Heap[(start - 1) / 2] = Heap[start];
Heap[start] = temp;
start = (start - 1) / 2;
}
}

void order(Process Heap[], int* heapsize, int start) {


int smallest = start;
int left = 2 * start + 1;
int right = 2 * start + 2;
if (left < *heapsize && Heap[left].priority < Heap[smallest].priority)
smallest = left;
if (right < *heapsize && Heap[right].priority < Heap[smallest].priority)
smallest = right;

if (smallest != start) {
Process temp = Heap[smallest];
Heap[smallest] = Heap[start];
Heap[start] = temp;
order(Heap, heapsize, smallest);
}
}

Process extractminimum(Process Heap[], int* heapsize, int* currentTime) {


Process min = Heap[0];
if (min.responsetime == -1)
min.responsetime = *currentTime - min.arrivalTime;
--(*heapsize);
if (*heapsize >= 1) {
Heap[0] = Heap[*heapsize];
order(Heap, heapsize, 0);
}
return min;
}

bool compare(Process p1, Process p2) {


return (p1.arrivalTime < p2.arrivalTime);
}

void scheduling(Process Heap[], Process array[], int n, int* heapsize, int*


currentTime) {
if (*heapsize == 0)
return;

Process min = extractminimum(Heap, heapsize, currentTime);


min.outtime = *currentTime + 1;
--min.burstTime;
std::cout << "process id = " << min.processID << " current time = " <<
*currentTime << std::endl;

if (min.burstTime > 0) {
insert(Heap, min, heapsize, currentTime);
return;
}

for (int i = 0; i < n; i++)


if (array[i].processID == min.processID) {
array[i] = min;
break;
}
}

void priority(Process array[], int n) {


std::sort(array, array + n, compare);

int totalwaitingtime = 0, totalbursttime = 0, totalturnaroundtime = 0, i,


insertedprocess = 0, heapsize = 0,
currentTime = array[0].arrivalTime, totalresponsetime = 0;
Process Heap[4 * n];

for (int i = 0; i < n; i++) {


totalbursttime += array[i].burstTime;
array[i].tempburstTime = array[i].burstTime;
}

do {
if (insertedprocess != n) {
for (i = 0; i < n; i++) {
if (array[i].arrivalTime == currentTime) {
++insertedprocess;
array[i].intime = -1;
array[i].responsetime = -1;
insert(Heap, array[i], &heapsize, &currentTime);
}
}
}
scheduling(Heap, array, n, &heapsize, &currentTime);
++currentTime;
if (heapsize == 0 && insertedprocess == n)
break;
} while (1);

for (int i = 0; i < n; i++) {


totalresponsetime += array[i].responsetime;
totalwaitingtime += (array[i].outtime - array[i].intime -
array[i].tempburstTime);
totalbursttime += array[i].burstTime;
}

std::cout << "Average waiting time = " << ((float)totalwaitingtime /


(float)n) << std::endl;
std::cout << "Average response time = " << ((float)totalresponsetime /
(float)n) << std::endl;
std::cout << "Average turn around time = " << ((float)(totalwaitingtime +
totalbursttime) / (float)n) << std::endl;
}

int main() {
int n, i;
Process a[5];
a[0].processID = 1;
a[0].arrivalTime = 4;
a[0].priority = 2;
a[0].burstTime = 6;
a[1].processID = 4;
a[1].arrivalTime = 5;
a[1].priority = 1;
a[1].burstTime = 3;
a[2].processID = 2;
a[2].arrivalTime = 5;
a[2].priority = 3;
a[2].burstTime = 1;
a[3].processID = 3;
a[3].arrivalTime = 1;
a[3].priority = 4;
a[3].burstTime = 2;
a[4].processID = 5;
a[4].arrivalTime = 3;
a[4].priority = 5;
a[4].burstTime = 4;
priority(a, 5);
return 0;
}

OUTPUT

process id = 3 current time = 1

process id = 3 current time = 2

process id = 5 current time = 3

process id = 1 current time = 4

process id = 4 current time = 5

process id = 4 current time = 6

process id = 4 current time = 7

process id = 1 current time = 8

process id = 1 current time = 9

process id = 1 current time = 10

process id = 1 current time = 11

process id = 1 current time = 12

process id = 2 current time = 13


process id = 5 current time = 14

process id = 5 current time = 15

process id = 5 current time = 16

Average waiting time = 4.2

Average response time = 1.6

Average turn around time = 7.4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy