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

OS Lab Program 2a, 2b

The document outlines the simulation of four CPU scheduling algorithms: FCFS, SJF, Round Robin, and Priority, focusing on their impact on turnaround time and waiting time. Each algorithm has distinct characteristics, with FCFS executing processes in arrival order, SJF prioritizing shorter jobs, Round Robin ensuring fairness with time slices, and Priority allocating CPU to the highest priority processes. The document includes sample code for implementing FCFS and SJF algorithms to calculate average turnaround and waiting times.

Uploaded by

devishreejogi
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)
7 views5 pages

OS Lab Program 2a, 2b

The document outlines the simulation of four CPU scheduling algorithms: FCFS, SJF, Round Robin, and Priority, focusing on their impact on turnaround time and waiting time. Each algorithm has distinct characteristics, with FCFS executing processes in arrival order, SJF prioritizing shorter jobs, Round Robin ensuring fairness with time slices, and Priority allocating CPU to the highest priority processes. The document includes sample code for implementing FCFS and SJF algorithms to calculate average turnaround and waiting times.

Uploaded by

devishreejogi
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

2.

Simulate the following CPU scheduling algorithms to find turnaround time and
waiting time
a) FCFS b) SJF c) Round Robin d) Priority.

DESCRIPTION:
CPU scheduling is a crucial part of an operating system's task management. It's the
process of determining which processes in the ready queue should be allocated CPU time
for execution. Since there are typically more processes ready for execution than there are
CPUs available, the scheduler must decide the order and timing of process execution to
optimize system [Link] scheduling algorithms are critical in operating
systems to efficiently allocate CPU resources to processes. Each algorithm aims to
minimize turnaround time (total time taken to execute a process) and waiting time
(time a process spends waiting in the ready queue). Here's a brief description of four
common scheduling algorithms:

a) FCFS (First-Come, First-Served): Processes are executed in the order they


arrive. Turnaround time and waiting time heavily depend on the order of process
arrival, with early arriving processes completing first.

b) SJF (Shortest Job First): The process with the shortest burst time is executed
first. This algorithm minimizes both turnaround time and waiting time by prioritizing
short-duration processes.

c) Round Robin: Each process is assigned a fixed time slice (quantum) for
execution. Processes are executed in a circular manner, with each process given a
turn to execute for its allocated time slice. This algorithm ensures fairness and
prevents starvation while providing reasonable turnaround time and waiting time.

d) Priority: Processes are assigned priorities, and the CPU is allocated to the
highest priority process in the ready queue. This algorithm requires a mechanism to
determine priorities, which can be static or dynamic. Higher priority processes are
executed first, potentially reducing their turnaround time and waiting time.

a) FCFS
#include <stdio.h>
int main()
{
int n;
float arrivalTime[20], burstTime[20], startTime[20],
finishTime[20], waitingTime[20], turnaroundTime[20];
float avgTat, avgWt;
char processName[20][20];
printf("\n ******FCFS Algorithm****\n");
printf("Enter No. of Processes\n");
scanf("%d", &n);

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


{
printf("Enter Process %d s Name, Arrival Time and Burst
Time(separated by space):",i+1);
scanf("%s%f%f", processName[i], &arrivalTime[i],
&burstTime[i]);
}
for (int i = 0; i < n; i++)
{
if (i == 0)
{
startTime[i] = arrivalTime[i];
waitingTime[i] = startTime[i] - arrivalTime[i];
finishTime[i] = startTime[i] + burstTime[i];
turnaroundTime[i] = finishTime[i] - arrivalTime[i];
}
else
{
startTime[i] = finishTime[i - 1];
waitingTime[i] = startTime[i] - arrivalTime[i];
finishTime[i] = startTime[i] + burstTime[i];
turnaroundTime[i] = finishTime[i] - arrivalTime[i];
}
}
int totTat = 0;
int totWt = 0;
printf("\nProcess Arrival \tBurst \tStart \tTuraround \tWait \
tFinish");
for (int i = 0; i < n; i++)
{
printf("\n%s\t%.2f\t\t%.2f\t%.2f\t%.2f\t\t%.2f\t%.2f",
processName[i], arrivalTime[i], burstTime[i], startTime[i],
turnaroundTime[i], waitingTime[i], finishTime[i]);
totWt += waitingTime[i];
totTat += turnaroundTime[i];
}
avgTat = (float)totTat / n;
avgWt = (float)totWt / n;
printf("\nAverage Turnaround Time:%.2f ms", avgTat);
printf("\nAverage Wait Time:%.2f ms", avgWt);
}

SAMPLE OUTPUT :
b) SJF

#include <stdio.h>
#include <string.h>
int main()
{
int i = 0, processNumber[10], n, temp = 0, j;
float burstTime[10], waitTime[10],turnaroundTime[10];
float avgWaitTime, avgTat;
printf("\n ******SJF Algorithm****");
printf("\nAssume arrival time 0 for all processes\n");
printf("\n Enter the no of process ");
scanf("\n %d", &n);
printf("\n Enter the burst time of each process ");
for (i = 0; i < n; i++)
{
printf("\n p %d:", i+1);
scanf("%f", &burstTime[i]);
processNumber[i]=i;
}
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (burstTime[i] > burstTime[j])
{
temp = burstTime[i];
burstTime[i] = burstTime[j];
burstTime[j] = temp;
temp = processNumber[i];
processNumber[i] = processNumber[j];
processNumber[j]= temp;
}
}
}
waitTime[0] = 0;
for (i = 1; i < n; i++)
{
waitTime[i] = burstTime[i - 1] + waitTime[i - 1];
avgWaitTime = avgWaitTime + waitTime[i];
}
printf("\n process no \t burst time\t waiting time \t turn around
time\n");
for (i = 0; i < n; i++)
{
turnaroundTime[i] = burstTime[i] + waitTime[i];
avgTat += turnaroundTime[i];
printf("\n p%d\t\t%.2f\t\t%.2f\t\t%.2f", processNumber[i+1],
burstTime[i], waitTime[i], turnaroundTime[i]);
}
printf("\n\n\t Average waiting time %.2f\n\t Average turn around
time %.2f", (avgWaitTime/n), (avgTat/n));
}

SAMPLE OUTPUT:

You might also like