0% found this document useful (0 votes)
25 views4 pages

Average Waiting & Turnaround Time in C

Uploaded by

parkaraaryan.908
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)
25 views4 pages

Average Waiting & Turnaround Time in C

Uploaded by

parkaraaryan.908
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

EXP 6: C program to calculate average waiting time and Turnaround Time of n

processes with Priority CPU scheduling algorithm.


CODE:
#include <stdio.h>

// Structure to hold process information

struct Process {

int pid; // Process ID

int arrival_time;

int burst_time;

int priority; // Lower number = Higher priority

int completion_time;

int waiting_time;

int turnaround_time;

int is_completed; // Flag to track completion

};

// Function to find the next highest priority job that has arrived

int find_highest_priority_job(struct Process *p, int n, int current_time) {

int highest_priority_index = -1;

int min_priority = 99999;

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

// Check if the process has arrived, is not completed, and has the minimum priority number

if (p[i].arrival_time <= current_time && p[i].is_completed == 0) {

if (p[i].priority < min_priority) {

min_priority = p[i].priority;

highest_priority_index = i;

// Tie-breaker: If priorities are equal, use FCFS (earlier arrival time)

else if (p[i].priority == min_priority && p[i].arrival_time <


p[highest_priority_index].arrival_time) {

highest_priority_index = i;

}
}

return highest_priority_index;

// Function to calculate Non-Preemptive Priority scheduling parameters

void calculate_priority_np(struct Process *p, int n) {

int current_time = 0;

int completed_processes = 0;

float total_waiting_time = 0;

float total_turnaround_time = 0;

// Initialize completion status

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

p[i].is_completed = 0;

while (completed_processes < n) {

int next_process_index = find_highest_priority_job(p, n, current_time);

if (next_process_index == -1) {

// No process has arrived yet, advance time to the arrival of the next process

int min_arrival = 99999;

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

if(p[i].is_completed == 0 && p[i].arrival_time < min_arrival) {

min_arrival = p[i].arrival_time;

if(min_arrival != 99999) current_time = min_arrival;

continue;

// Schedule and run the process (Non-Preemptive)

int i = next_process_index;

// Calculate completion time

p[i].completion_time = current_time + p[i].burst_time;


current_time = p[i].completion_time;

// Calculate turnaround time

p[i].turnaround_time = p[i].completion_time - p[i].arrival_time;

// Calculate waiting time

p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;

// Mark as completed

p[i].is_completed = 1;

completed_processes++;

total_waiting_time += p[i].waiting_time;

total_turnaround_time += p[i].turnaround_time;

// Sort by PID for final output (as shown in the image: 1, 2, 3)

struct Process output_p[n];

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

output_p[p[i].pid - 1] = p[i];

// Print results (matching the image format)

printf("\nPID\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",

output_p[i].pid, output_p[i].arrival_time, output_p[i].burst_time,

output_p[i].priority, output_p[i].completion_time,

output_p[i].waiting_time, output_p[i].turnaround_time);

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

printf("Average Turnaround Time = %.2f\n", total_turnaround_time / n);

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 arrival time for process %d: ", p[i].pid);

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

printf("Enter burst time for process %d: ", p[i].pid);

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

printf("Enter priority for process %d: ", p[i].pid);

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

calculate_priority_np(p, n);

return 0;

OUTPUT:

You might also like