0% found this document useful (0 votes)
108 views2 pages

RIshi

The C program implements the shortest job first CPU scheduling algorithm. It takes process details as input, sorts by arrival time, calculates completion time and waiting/turnaround times for each process, and outputs average waiting and turnaround times.
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)
108 views2 pages

RIshi

The C program implements the shortest job first CPU scheduling algorithm. It takes process details as input, sorts by arrival time, calculates completion time and waiting/turnaround times for each process, and outputs average waiting and turnaround times.
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/ 2

// C program to implement CPU scheduling for the shortest job first

#include <stdio.h>

struct Process {
int id;
int arrival_time;
int burst_time;
int completion_time;
int turnaround_time;
int waiting_time;
};

void swap(struct Process *a, struct Process *b) {


struct Process temp = *a;
*a = *b;
*b = temp;
}

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


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].arrival_time > processes[j + 1].arrival_time) {
swap(&processes[j], &processes[j + 1]);
}
}
}
}

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


sort_by_arrival_time(processes, n);

int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;

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


if (current_time < processes[i].arrival_time) {
current_time = processes[i].arrival_time;
}

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;

total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;

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

double avg_waiting_time = (double)total_waiting_time / n;


double avg_turnaround_time = (double)total_turnaround_time / n;

printf("\nAverage Turnaround Time: %.2f\n", avg_turnaround_time);


printf("Average Waiting Time: %.2f\n", avg_waiting_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++) {


processes[i].id = i + 1;
printf("Enter arrival time and burst time for process %d: ", i + 1);
scanf("%d%d", &processes[i].arrival_time, &processes[i].burst_time);
}

sjf_schedule(processes, n);

return 0;
}




OUTPUT

You might also like