0% found this document useful (0 votes)
4 views3 pages

Priority Operating System

The document contains a C program that implements a priority scheduling algorithm for processes. It defines a structure for processes and includes functions to sort them by priority, calculate waiting time, and turnaround time. The program also outputs the details of each process along with average waiting and turnaround times.

Uploaded by

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

Priority Operating System

The document contains a C program that implements a priority scheduling algorithm for processes. It defines a structure for processes and includes functions to sort them by priority, calculate waiting time, and turnaround time. The program also outputs the details of each process along with average waiting and turnaround times.

Uploaded by

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

#include <stdio.

h>

// Structure to represent a process

struct Process {

int pid; // Process ID

int bt; // Burst Time

int priority; // Priority

int wt; // Waiting Time

int tat; // Turnaround Time

};

// Function to sort by priority (lower value = higher priority)

void sortByPriority(struct Process p[], int n) {

struct Process temp;

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

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

if(p[j].priority > p[j+1].priority) {

temp = p[j];

p[j] = p[j+1];

p[j+1] = temp;

}
int main() {

int n;

printf("Enter number of processes: ");

scanf("%d", &n);

struct Process p[n];

// Input process details

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

p[i].pid = i + 1;

printf("Enter Burst Time and Priority for Process %d: ", p[i].pid);

scanf("%d %d", &p[i].bt, &p[i].priority);

// Sort processes based on priority

sortByPriority(p, n);

// Calculate Waiting Time and Turnaround Time

p[0].wt = 0;

p[0].tat = p[0].bt;

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

p[i].wt = p[i-1].wt + p[i-1].bt;

p[i].tat = p[i].wt + p[i].bt;

}
// Output

printf("\nProcess\tBT\tPriority\tWT\tTAT\n");

int total_wt = 0, total_tat = 0;

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

total_wt += p[i].wt;

total_tat += p[i].tat;

printf("P%d\t%d\t%d\t\t%d\t%d\n", p[i].pid, p[i].bt, p[i].priority, p[i].wt, p[i].tat);

printf("\nAverage Waiting Time = %.2f", (float)total_wt / n);

printf("\nAverage Turnaround Time = %.2f\n", (float)total_tat / n);

return 0;

You might also like