0% found this document useful (0 votes)
2 views

os5

The document contains a C program that implements a priority scheduling algorithm for processes. It defines a structure for processes and includes functions to calculate waiting, turnaround, and completion times based on priority and arrival time. The program prompts the user to input process details and then displays the scheduling results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

os5

The document contains a C program that implements a priority scheduling algorithm for processes. It defines a structure for processes and includes functions to calculate waiting, turnaround, and completion times based on priority and arrival time. The program prompts the user to input process details and then displays the scheduling results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>

struct Process {
int pid; // Process ID
int arrival_time; // Arrival Time
int burst_time; // Burst Time
int priority; // Priority
int waiting_time;
int turnaround_time;
int completion_time;
};

void calculateTimes(struct Process processes[], int n) {


// Sorting processes by priority and arrival time (if priorities are equal)
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].priority > processes[j + 1].priority ||
(processes[j].priority == processes[j + 1].priority &&
processes[j].arrival_time > processes[j + 1].arrival_time)) {
struct Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}

int current_time = 0;
for (int i = 0; i < n; i++) {
if (current_time < processes[i].arrival_time) {
current_time = processes[i].arrival_time; // Wait for process arrival
}
processes[i].completion_time = current_time + processes[i].burst_time;
processes[i].turnaround_time = processes[i].completion_time -
processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time -
processes[i].burst_time;
current_time += processes[i].burst_time;
}
}

void printProcesses(struct Process processes[], int n) {


printf("PID\tArrival\tBurst\tPriority\tCompletion\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t\t%d\t\t%d\t%d\n",
processes[i].pid, processes[i].arrival_time,
processes[i].burst_time,
processes[i].priority, processes[i].completion_time,
processes[i].waiting_time, processes[i].turnaround_time);
}
}

int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);

struct Process processes[n];


for (int i = 0; i < n; i++) {
printf("Enter Process ID, Arrival Time, Burst Time, and Priority for
process %d: ", i + 1);
scanf("%d %d %d %d", &processes[i].pid, &processes[i].arrival_time,
&processes[i].burst_time, &processes[i].priority);
}

calculateTimes(processes, n);

printf("\nPriority Scheduling Results:\n");


printProcesses(processes, n);

return 0;
}

You might also like