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

Dev 7

The document discusses implementing two CPU scheduling algorithms: first come first serve (FCFS) and shortest job first (SJF). It includes code snippets to simulate each algorithm and calculate metrics like turnaround time and waiting time.

Uploaded by

papatel4045
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)
22 views5 pages

Dev 7

The document discusses implementing two CPU scheduling algorithms: first come first serve (FCFS) and shortest job first (SJF). It includes code snippets to simulate each algorithm and calculate metrics like turnaround time and waiting time.

Uploaded by

papatel4045
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

[ 2CEIT401 OPERATING SYSTEM]

Practical: 7

AIM- Apply the concept of CPU Scheduling Algorithms.

Department of Computer Engineering /Information Technology


2CEIT401:Operating Systems Practical-7

Aim: Apply the concept of CPU Scheduling Algorithms.


Q-1 Write a program to implement FCFS CPU scheduling Algorithm.
#include <stdio.h>
typedef struct {
int process_id;
int arrival_time;
int burst_time;
int completion_time;
int turnaround_time;
int waiting_time;
} Process;
void fcfs(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;
}
current_time += processes[i].burst_time;
processes[i].completion_time = current_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;
}
printf("Process Execution Order:\n");
printf("Process ID Arrival Time Burst Time Completion Time Turnaround Time
Waiting Time\n");
for (int i = 0; i < n; i++) {
printf("%8d%15d%13d%18d%17d%14d\n", processes[i].process_id,
processes[i].arrival_time,
processes[i].burst_time, processes[i].completion_time, processes[i].turnaround_time,
processes[i].waiting_time);
}
double avg_turnaround_time = 0.0;
double avg_waiting_time = 0.0;
for (int i = 0; i < n; i++) {
avg_turnaround_time += processes[i].turnaround_time;
avg_waiting_time += processes[i].waiting_time;
}
avg_turnaround_time /= n;
avg_waiting_time /= n;
printf("\nAverage Turnaround Time: %.2f\n", avg_turnaround_time);
printf("Average Waiting Time: %.2f\n", avg_waiting_time);
}
int main() {

Enrollment No: 22012011099


Name:Deva raval
2CEIT401:Operating Systems Practical-7

Process processes[] = {
{1, 0, 4, 0, 0, 0},
{2, 1, 2, 0, 0, 0},
{3, 2, 3, 0, 0, 0},
{4, 3, 5, 0, 0, 0},
{5, 4, 6, 0, 0, 0},
{6, 5, 1, 0, 0, 0},
};
int num_processes = sizeof(processes) / sizeof(Process);
printf("Input Processes:\n");
printf("Process ID Arrival Time Burst Time\n");
for (int i = 0; i < num_processes; i++) {
printf("%8d%15d%13d\n", processes[i].process_id, processes[i].arrival_time,
processes[i].burst_time);
}
printf("\n");
fcfs(processes, num_processes);
return 0;
}

Q-2. Write a program to implement SJF CPU scheduling Algorithm.


#include <stdio.h>
typedef struct {
int process_id;
int arrival_time;
int burst_time;
int completion_time;
int turnaround_time;
int waiting_time;
int is_completed; // Flag to check if the process is completed
Enrollment No: 22012011099
Name:Deva raval
2CEIT401:Operating Systems Practical-7

} Process;
void sjf(Process processes[], int n) {
int current_time = 0, completed = 0;
int min_burst_time, min_index;
while (completed != n) {
min_burst_time = INT_MAX ;
min_index = -1;
// Find the process with the shortest burst time among the processes that have arrived
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && !processes[i].is_completed) {
if (processes[i].burst_time < min_burst_time) {
min_burst_time = processes[i].burst_time;
min_index = i;
}} }
if (min_index == -1) {
current_time++;
} else {
current_time += processes[min_index].burst_time;
processes[min_index].completion_time = current_time;
processes[min_index].turnaround_time = processes[min_index].completion_time -
processes[min_index].arrival_time;
processes[min_index].waiting_time = processes[min_index].turnaround_time -
processes[min_index].burst_time;
processes[min_index].is_completed = 1;
completed++;
}
}
printf("Process Execution Order:\n");
printf("Process ID Arrival Time Burst Time Completion Time Turnaround Time
Waiting Time\n");
for (int i = 0; i < n; i++) {
printf("%8d%15d%13d%18d%17d%14d\n", processes[i].process_id,
processes[i].arrival_time,
processes[i].burst_time, processes[i].completion_time, processes[i].turnaround_time,
processes[i].waiting_time);
}
double avg_turnaround_time = 0.0, avg_waiting_time = 0.0;
for (int i = 0; i < n; i++) {
avg_turnaround_time += processes[i].turnaround_time;
avg_waiting_time += processes[i].waiting_time;
}
avg_turnaround_time /= n;
avg_waiting_time /= n;
printf("\nAverage Turnaround Time: %.2f\n", avg_turnaround_time);
printf("Average Waiting Time: %.2f\n", avg_waiting_time);
Enrollment No: 22012011099
Name:Deva raval
2CEIT401:Operating Systems Practical-7

int main() {
Process processes[] = {
{1, 0, 6, 0, 0, 0, 0},
{2, 0, 3, 0, 0, 0, 0},
{3, 1, 4, 0, 0, 0, 0},
{4, 2, 2, 0, 0, 0, 0},
{5, 3, 1, 0, 0, 0, 0},
{6, 4, 5, 0, 0, 0, 0},
{7, 6, 2, 0, 0, 0, 0}
};
int num_processes = sizeof(processes) / sizeof(Process);
printf("Input Processes:\n");
printf("Process ID Arrival Time Burst Time\n");
for (int i = 0; i < num_processes; i++) {
printf("%8d%15d%13d\n", processes[i].process_id, processes[i].arrival_time,
processes[i].burst_time);
}
printf("\n");
sjf(processes, num_processes);
return 0;
}

Enrollment No: 22012011099


Name:Deva raval

You might also like