0% found this document useful (0 votes)
39 views9 pages

Lab 7

The document contains programming assignments for implementing various CPU scheduling algorithms including First Come First Serve, Shortest Job First, Priority Scheduling, and Round Robin. Each algorithm calculates average waiting time and average turnaround time based on process arrival and burst times. The document includes code snippets for each algorithm in C programming language.

Uploaded by

kali2023sahoo
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)
39 views9 pages

Lab 7

The document contains programming assignments for implementing various CPU scheduling algorithms including First Come First Serve, Shortest Job First, Priority Scheduling, and Round Robin. Each algorithm calculates average waiting time and average turnaround time based on process arrival and burst times. The document includes code snippets for each algorithm in C programming language.

Uploaded by

kali2023sahoo
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/ 9

Assignment Lab-7

Name-Kalicharan Chinmay Sahoo


Roll no-122Cs0103

Q1)Write a program to implement the First Come First Serve algorithm


considering the arrival time of the process?
The algorithm should calculate the average waiting time, average turn
around time

Code:
#include <stdio.h>

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

int arrival_time[n];
int burst_time[n];
int completion_time[n];
int waiting_time[n];
int turnaround_time[n];

printf("Enter arrival and burst times for each process:\n");


for (int i = 0; i < n; i++) {
printf("Enter arrival and burst time of process %d: ", i + 1);
scanf("%d %d", &arrival_time[i], &burst_time[i]);
}

int ct = 0;
for (int i = 0; i < n; i++) {
if (ct < arrival_time[i]) {
ct = arrival_time[i];
}
ct += burst_time[i];
completion_time[i] = ct;
}

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


turnaround_time[i] = completion_time[i] - arrival_time[i];
waiting_time[i] = turnaround_time[i] - burst_time[i];
}

float avg_wait_time=0;
float avg_turn_around_time=0;
for(int i=0;i<n;i++){
avg_wait_time+=waiting_time[i];
avg_turn_around_time+=turnaround_time[i];
}

printf("The avg waiting time:%f and avg turnaroud


time:%f",avg_wait_time/n,avg_turn_around_time/n);

return 0;
}

Question 2)
Write a program to implement the Shortest Job First scheduling algorithm
considering the preemption of the process and arrival time of the process?
The algorithm should calculate the average waiting time, average turn
around time
#include <stdio.h>
#include <stdbool.h>
#define MAX 100

typedef struct {
int pid;
int arrival;
int burst;
int remaining;
int waiting;
int turnaround;
} Process;

void calculateTimes(Process p[], int n) {


int time = 0;
int completed = 0;
int min_remaining_time_index;
int min_remaining_time;

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


p[i].remaining = p[i].burst;
}

while (completed < n) {


min_remaining_time_index = -1;
min_remaining_time = 99999;

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


if (p[i].arrival <= time && p[i].remaining > 0) {
if (p[i].remaining < min_remaining_time) {
min_remaining_time = p[i].remaining;
min_remaining_time_index = i;
}
}
}

if (min_remaining_time_index == -1) {
time++;
continue;
}

p[min_remaining_time_index].remaining--;
time++;

if (p[min_remaining_time_index].remaining == 0) {
completed++;
p[min_remaining_time_index].turnaround = time -
p[min_remaining_time_index].arrival;
p[min_remaining_time_index].waiting =
p[min_remaining_time_index].turnaround -
p[min_remaining_time_index].burst;
}
}
}

void printResults(Process p[], int n) {


float total_waiting = 0, total_turnaround = 0;
printf("\nProcess ID\tArrival Time\tBurst Time\tWaiting
Time\tTurnaround Time\n");

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


total_waiting += p[i].waiting;
total_turnaround += p[i].turnaround;
}

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


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

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

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


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

calculateTimes(p, n);

printResults(p, n);
return 0;
}
Quesssstion 3
Write a program to implement the priority scheduling algorithm considering
the arrival time and preemption?
The algorithm should calculate the average waiting time, average turn
around time
#include <stdio.h>
#define MAX 100

typedef struct {
int pid;
int arrival;
int burst;
int priority;
int waiting;
int turnaround;
int remaining;
} Process;

void calculateTimes(Process p[], int n) {


int time = 0;
int completed = 0;

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


p[i].remaining = p[i].burst;
}

while (completed < n) {


int min_priority_index = -1;
int min_priority = 99999;
for (int i = 0; i < n; i++) {
if (p[i].arrival <= time && p[i].remaining > 0) {
if (p[i].priority < min_priority) {
min_priority = p[i].priority;
min_priority_index = i;
}
}
}

if (min_priority_index == -1) {
time++;
continue;
}

p[min_priority_index].remaining--;
time++;

// If a process is completed
if (p[min_priority_index].remaining == 0) {
p[min_priority_index].turnaround = time -
p[min_priority_index].arrival;
p[min_priority_index].waiting = p[min_priority_index].turnaround
- p[min_priority_index].burst;
completed++;
}
}
}

void printResults(Process p[], int n) {


float total_waiting = 0, total_turnaround = 0;

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


total_waiting += p[i].waiting;
total_turnaround += p[i].turnaround;
}

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


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

int main() {
Process p[MAX];
int n;

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


scanf("%d", &n);

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


printf("Enter arrival time, burst time, and priority for process %d: ", i
+ 1);
p[i].pid = i + 1;
scanf("%d %d %d", &p[i].arrival, &p[i].burst, &p[i].priority);
p[i].remaining = p[i].burst;
}

calculateTimes(p, n);

printResults(p, n);
return 0;
}

Question 4)
Write a program to implement the Round Robin scheduling algorithm
considering the arrival time and preemption?
The algorithm should calculate the average waiting time, average turn
around time
#include <stdio.h>

int main() {
int n;

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


scanf("%d", &n);
int p_arrival[n];
int p_burst[n];
int rem_time[n];

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


printf("Enter arrival and burst time of process %d: ", i + 1);
scanf("%d %d", &p_arrival[i], &p_burst[i]);
rem_time[i] = p_burst[i];
printf("\n");
}

int q = 5;
int rem_p = n;
float t_waiting = 0;
float t_turnaround = 0;
int curr_t = 0;

while(rem_p > 0) {
for(int i = 0; i < n; i++) {
if(rem_time[i] == 0) continue;

if(rem_time[i] <= q) {
curr_t += rem_time[i];
int turnaround = curr_t - p_arrival[i];
int waiting = turnaround - p_burst[i];
t_waiting += waiting;
t_turnaround += turnaround;
rem_p--;
rem_time[i] = 0;
} else {
rem_time[i] -= q;
curr_t += q;
}
}
}

float avg_wait = t_waiting / n;


float avg_turnaround = t_turnaround / n;

printf("Average waiting time: %.2f \n", avg_wait);


printf("Average turnaround time: %.2f \n", avg_turnaround);

return 0;
}

You might also like