0% found this document useful (0 votes)
4 views

Lab2

The document outlines a lab assignment focused on simulating various CPU scheduling algorithms, including FCFS, SJF, Round Robin, and Priority. It provides code snippets for each algorithm that calculate average turnaround and waiting times based on user input for process names, arrival times, burst times, and priorities. The document serves as a practical guide for implementing these scheduling techniques in a programming environment.

Uploaded by

babithaganesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab2

The document outlines a lab assignment focused on simulating various CPU scheduling algorithms, including FCFS, SJF, Round Robin, and Priority. It provides code snippets for each algorithm that calculate average turnaround and waiting times based on user input for process names, arrival times, burst times, and priorities. The document serves as a practical guide for implementing these scheduling techniques in a programming environment.

Uploaded by

babithaganesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Operating Systems

Lab-1
Program-2
Simulate the following CPU scheduling algorithms to find average turn
around time and waiting time
a) FCFS b) SJF c) Round robin d) Priority
FCFS
#include <stdio.h>
#include <conio.h>

int main()
{
int n, arrivalTime[20], burstTime[20], startTime[20], finishTime[20],
waitingTime[20], turnaroundTime[20];
float avgTat, avgWt;
char processName[20][20];
printf("Enter No. of Processes\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter Processes Name, Arrival Time and Burst Time:");
scanf("%s%d%d", &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%4d\t\t%4d\t%4d\t%4d\t\t%4d\t%4d", 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", avgTat);


printf("\nAverage Wait Time:%.2f", avgWt);
}
SJF
// Header files
//Main function
//Declare variables
//Read no.of processes and store it in variable n
//Read the burst time of each process
printf("Enter the burst time of each process : \n");
for(i=0;i<n;i++)
{
printf("P%d:",i);
scanf("%d",&burstTime[i]);
processNumber[i] = i;
}
//Sort the processes according to the increasing order of Burst times
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;
}
}
}
//Calculate the waiting times of each process
waitTime[0] = 0;
int totalWt=0,totalTat=0;
for(i=1;i<n;i++)
{
waitTime[i] = burstTime[i-1] + waitTime[i-1];
//Calculate the total waiting time
totalWt += waitTime[i];
}
//Calculate the turn around time of each process
printf("\nProcess no.\tBurst time\tWaiting time\tTurn around time\n");
for(i=0;i<n;i++)
{
turnaroundTime[i] = burstTime[i] + waitTime[i];
//Calculate the total turn around time
totalTat += turnaroundTime[i];
printf("\nP%d\t\t%d\t\t%d\t\t
%d",processNumber[i],burstTime[i],waitTime[i],turnaroundTime[i]);
}
//Calculate the average waiting time and turn around time
//Display the result
3) Priority
// Header files
//Main function
//Declare variables
//Read no.of processes and store it in variable n
//Read the burst time and priority of each process
//Sort the processes according to the priority( Assumption-Lower number indicates higher priority)
for (i = 0; i < n-1; i++)
{
for (j = i + 1; j < n; j++)
if (priority[i] > priority[j])
{
temp = priority[i];
priority[i] = priority[j];
priority[j] = temp;
temp = burstTime[i];
burstTime[i] = burstTime[j];
burstTime[j] = temp;
temp = processNo[i];
processNo[i] = processNo[j];
processNo[j] = temp;
}
}
for (i = 0; i < n-1; i++)
{
for (j = i + 1; j < n; j++)
if (priority[i] > priority[j])
{
temp = priority[i];
priority[i] = priority[j];
priority[j] = temp;
temp = burstTime[i];
burstTime[i] = burstTime[j];
burstTime[j] = temp;
temp = processNo[i];
processNo[i] = processNo[j];
processNo[j] = temp;
}
}
//Calculate the waiting times of each process
//Calculate the total waiting time
//Calculate the turnaround time of each process
//Calculate the total waiting time
//Calculate the average waiting time and turnaround time
//Display the result
4) Round robin
//header file
struct process
{
int burst, wait, comp, f;
} p[20];
int main()
{
int n, i, j, totalwait = 0, totalturn = 0, quantum, flag = 1, time = 0;
//Read no. of process-n
//Read the time slice-quantum
for (i = 0; i < n; i++)
{
printf("Enter The Burst Time (in ms) For Process #%2d :", i + 1);
scanf("%d", &p[i].burst);
p[i].f = 1;
}
printf("\nOrder Of Execution \n");
printf("\nProcess Starting Ending Remaining");
printf("\n\t\tTime \tTime \t Time");
while (flag == 1)
{
flag = 0;
for (i = 0; i < n; i++)
{
if (p[i].f == 1)
{
flag = 1;
j = quantum;
if ((p[i].burst - p[i].comp) > quantum)
{
p[i].comp += quantum;
}
else
{
p[i].wait = time - p[i].comp;
j = p[i].burst - p[i].comp;
p[i].comp = p[i].burst;
p[i].f = 0;
}
printf("\nprocess # %-3d %-10d %-10d %-10d", i + 1, time, time + j,
p[i].burst - p[i].comp);
time += j;
}
}
}
printf("\n\n------------------");
printf("\nProcess \t Waiting Time TurnAround Time ");
for (i = 0; i < n; i++)
{
printf("\nProcess # %-12d%-15d%-15d", i + 1, p[i].wait, p[i].wait + p[i].burst);
totalwait = totalwait + p[i].wait;
totalturn = totalturn + p[i].wait + p[i].burst;
}
printf("\n\nAverage\n------------------ ");
printf("\nWaiting Time: %fms", totalwait / (float)n);
printf("\nTurnAround Time : %fms\n\n", totalturn / (float)n);
return 0;
}

You might also like