0% found this document useful (0 votes)
26 views5 pages

Sagar Kumar Singh BCS 3A 1065

Uploaded by

Sagar Singh
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)
26 views5 pages

Sagar Kumar Singh BCS 3A 1065

Uploaded by

Sagar Singh
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/ 5

ASSIGNMENT-04

Name:- Sagar Kumar Singh Id:-221001001065


Batch:- BCS 3A (Group-A) Subject:-Operating System

QUESTION: 1) Simulate the following Short-term scheduler that will follow


the following conditions—
(a) Maintain two queues for this implementation. The input process queue(
Ready queue) will have the list of processes. Every process will have a process
ID and some other information( CPU Burst time and I/O Burst time, Arrival
time, total burst time, Priority score, Turn around time, wait time, response
time). Consider the CPU Burst time and I/O Burst time, Arrival time as
random value but it should be within some certain range.
(b) There will be an another output queue which will represent the Gantt
chart after the schedule of the processes. We also need to keep the
information regarding the process execution start time, execution finish time.
(c) We need to implement SJF CPU scheduling algorithms.
(d) We need to plot a graph to show the variation of average turnaround
time, average wait time for every scheduling algorithm with respect to avg.
burst time.
(e) Explain the graph in details and write your inference of the graph.

Code-:
#include <stdio.h>
#include <stdlib.h>

#define MAX_PROCESSES 10

typedef struct {
int pid;
int arrival_time;
int burst_time;
int completion_time;
int waiting_time;
int turnaround_time;
} Process;

int compare_arrival(const void* a, const void* b) {


Process* p1 = (Process*)a;
Process* p2 = (Process*)b;
return p1->arrival_time - p2->arrival_time;
}

int compare_burst(const void* a, const void* b) {


Process* p1 = (Process*)a;
Process* p2 = (Process*)b;
return p1->burst_time - p2->burst_time;
}

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


int current_time = 0;
qsort(processes, n, sizeof(Process), compare_arrival);
printf("\nFCFS Scheduling Gantt Chart:\n");
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].completion_time = current_time + processes[i].burst_time;
processes[i].turnaround_time = processes[i].completion_time -
processes[i].arrival_time;
printf("P%d (%d-%d) ", processes[i].pid, current_time,
processes[i].completion_time);
current_time = processes[i].completion_time;
}
printf("\n");
}

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


int current_time = 0;
qsort(processes, n, sizeof(Process), compare_burst);
printf("\nSJF Scheduling Gantt Chart:\n");
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].completion_time = current_time + processes[i].burst_time;
processes[i].turnaround_time = processes[i].completion_time -
processes[i].arrival_time;
printf("P%d (%d-%d) ", processes[i].pid, current_time,
processes[i].completion_time);
current_time = processes[i].completion_time;
}
printf("\n");
}

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


for (int i = 0; i < n; i++) {
processes[i].pid = i + 1;
processes[i].arrival_time = rand() % 10;
processes[i].burst_time = rand() % 10 + 1;
}
}

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


int total_turnaround_time = 0;
int total_waiting_time = 0;
for (int i = 0; i < n; i++) {
total_turnaround_time += processes[i].turnaround_time;
total_waiting_time += processes[i].waiting_time;
}
printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time /
n);
printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);
}

int main() {
Process processes[MAX_PROCESSES];
int n = 5;
generate_processes(processes, n);
fcfs_scheduling(processes, n);
calculate_average_times(processes, n);
sjf_scheduling(processes, n);
calculate_average_times(processes, n);
return 0;
}

OUTPUT--:

You might also like