0% found this document useful (0 votes)
90 views10 pages

SJF & Round Robin

The document provides C programs to simulate two CPU scheduling algorithms: Shortest Job First (SJF) and Round Robin. It includes structures to store process details, functions to calculate waiting and turnaround times, and methods to display results. Sample inputs and outputs for both algorithms are provided to illustrate their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views10 pages

SJF & Round Robin

The document provides C programs to simulate two CPU scheduling algorithms: Shortest Job First (SJF) and Round Robin. It includes structures to store process details, functions to calculate waiting and turnaround times, and methods to display results. Sample inputs and outputs for both algorithms are provided to illustrate their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Write C programs to simulate the following CPU Scheduling algorithms.

 SJF
 Round Rbbin

#include <stdio.h>

// Structure to store process details

typedef struct {

int pid;

int arrival_time;

int burst_time;

int waiting_time;

int turnaround_time;

} Process;

// Function to swap two processes

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

Process temp = *a;

*a = *b;

*b = temp;

// Function to sort processes by arrival time, then by burst time

void sort_by_burst_time(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) ||


(processes[j].arrival_time == processes[j + 1].arrival_time &&

processes[j].burst_time > processes[j + 1].burst_time)) {

swap(&processes[j], &processes[j + 1]);

// Function to calculate waiting time and turnaround time

void calculate_times(Process processes[], int n) {

int current_time = 0;

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

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

current_time = processes[i].arrival_time;

processes[i].waiting_time = current_time - processes[i].arrival_time;

processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;

current_time += processes[i].burst_time;

// Function to display process details

void display(Process processes[], int n) {

float avg_waiting_time = 0, avg_turnaround_time = 0;

printf("\nProcess ID | Arrival Time | Burst Time | Waiting Time | Turnaround Time\n");

printf("---------------------------------------------------------------\n");
for (int i = 0; i < n; i++) {

printf("%9d | %12d | %10d | %12d | %15d\n",

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

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

avg_waiting_time += processes[i].waiting_time;

avg_turnaround_time += processes[i].turnaround_time;

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

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

int main() {

int n;

printf("Enter the number of processes: ");

scanf("%d", &n);

Process processes[n];

printf("Enter Arrival Time and Burst Time for each process:\n");

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

processes[i].pid = i + 1;

printf("Process %d Arrival Time: ", i + 1);

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

printf("Process %d Burst Time: ", i + 1);

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

}
// Sorting processes by arrival time, then by burst time

sort_by_burst_time(processes, n);

// Calculate waiting and turnaround times

calculate_times(processes, n);

// Display results

display(processes, n);

return 0;

Sample Input.

Enter the number of processes: 4

Enter Arrival Time and Burst Time for each process:

Process 1 Arrival Time: 0

Process 1 Burst Time: 5

Process 2 Arrival Time: 1

Process 2 Burst Time: 3

Process 3 Arrival Time: 2

Process 3 Burst Time: 8

Process 4 Arrival Time: 3

Process 4 Burst Time: 6


Sample Output:

Process ID | Arrival Time | Burst Time | Waiting Time | Turnaround Time

-------------------------------------------------------------------------

1 | 0 | 5 | 0 | 5

2 | 1 | 3 | 4 | 7

4 | 3 | 6 | 5 | 11

3 | 2 | 8 | 12 | 20

Average Waiting Time: 5.25

Average Turnaround Time: 10.75

Round Robbin CPU Scheduling Algorithm.

#include <stdio.h>

// Structure to store process details

typedef struct {

int pid;

int arrival_time;

int burst_time;

int remaining_time;

int waiting_time;

int turnaround_time;

} Process;

void round_robin(Process processes[], int n, int time_quantum) {


int time = 0, completed = 0;

int queue[n], front = 0, rear = 0;

int in_queue[n];

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

in_queue[i] = 0;

processes[i].remaining_time = processes[i].burst_time;

queue[rear++] = 0; // Enqueue first process

in_queue[0] = 1;

while (completed < n) {

int index = queue[front++]; // Dequeue process

if (processes[index].remaining_time > time_quantum) {

time += time_quantum;

processes[index].remaining_time -= time_quantum;

} else {

time += processes[index].remaining_time;

processes[index].remaining_time = 0;

processes[index].turnaround_time = time - processes[index].arrival_time;

processes[index].waiting_time = processes[index].turnaround_time -
processes[index].burst_time;

completed++;

}
// Add newly arrived processes to queue

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

if (!in_queue[i] && processes[i].arrival_time <= time && processes[i].remaining_time > 0)


{

queue[rear++] = i;

in_queue[i] = 1;

// Re-enqueue current process if not finished

if (processes[index].remaining_time > 0) {

queue[rear++] = index;

void display_results(Process processes[], int n) {

float avg_waiting_time = 0, avg_turnaround_time = 0;

printf("\nProcess ID | Arrival Time | Burst Time | Waiting Time | Turnaround Time\n");

printf("---------------------------------------------------------------\n");

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

printf("%9d | %12d | %10d | %12d | %15d\n",

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

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

avg_waiting_time += processes[i].waiting_time;
avg_turnaround_time += processes[i].turnaround_time;

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

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

int main() {

int n, time_quantum;

printf("Enter the number of processes: ");

scanf("%d", &n);

Process processes[n];

printf("Enter Arrival Time and Burst Time for each process:\n");

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

processes[i].pid = i + 1;

printf("Process %d Arrival Time: ", i + 1);

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

printf("Process %d Burst Time: ", i + 1);

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

printf("Enter Time Quantum: ");

scanf("%d", &time_quantum);

round_robin(processes, n, time_quantum);
display_results(processes, n);

return 0;

Sample Input & Output:

Input:

Enter the number of processes: 4

Enter Arrival Time and Burst Time for each process:

Process 1 Arrival Time: 0

Process 1 Burst Time: 8

Process 2 Arrival Time: 1

Process 2 Burst Time: 4

Process 3 Arrival Time: 2

Process 3 Burst Time: 9

Process 4 Arrival Time: 3

Process 4 Burst Time: 5

Enter Time Quantum: 3

Output:

Process ID | Arrival Time | Burst Time | Waiting Time | Turnaround Time

-------------------------------------------------------------------------

1 | 0 | 8 | 9 | 17

2 | 1 | 4 | 6 | 10

3 | 2 | 9 | 12 | 21

4 | 3 | 5 | 9 | 14
Average Waiting Time: 9.00

Average Turnaround Time: 15.50

You might also like