0% found this document useful (0 votes)
32 views25 pages

Os3 21BLC1603

The document discusses three CPU scheduling algorithms: First Come First Serve (FCF), Shortest Job First (SJF), and Priority Scheduling. It provides code implementations and explanations for each algorithm. It also discusses a Round Robin scheduling implementation without arrival times.
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)
32 views25 pages

Os3 21BLC1603

The document discusses three CPU scheduling algorithms: First Come First Serve (FCF), Shortest Job First (SJF), and Priority Scheduling. It provides code implementations and explanations for each algorithm. It also discusses a Round Robin scheduling implementation without arrival times.
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/ 25

OPERATING SYSTEMS

LAB-3
CPU Scheduling

NAME : DARSHNI B

REG_NUM: 21BLC1603

1. FIRST COME FIRST SERVE (FCFS):


#include <stdio.h>

int main() {

int pid[15];

int bt[15];

int n;

printf("Enter no of processes: ");

scanf("%d", &n);

printf("Enter process num : ");

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

scanf("%d", &pid[i]);

printf("Enter burst time : ");


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

scanf("%d", &bt[i]);

int i, wt[n];

wt[0] = 0;

// for calculating waiting time of each process

for (i = 1; i < n; i++) {

wt[i] = bt[i - 1] + wt[i - 1];

printf("\n1. FCFS Scheduling table:\n");

printf("Process ID Burst Time Waiting Time Turnaround Time\n");

float twt = 0.0;

float tat = 0.0;

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

printf("%d\t\t", pid[i]);

printf("%d\t\t", bt[i]);

printf("%d\t\t", wt[i]);

// calculating and printing turnaround time of each process

printf("%d\t\t", bt[i] + wt[i]);

printf("\n");
// for calculating total waiting time

twt += wt[i];

// for calculating total turnaround time

tat += (wt[i] + bt[i]);

float att, awt;

// for calculating average waiting time

awt = twt / n;

// for calculating average turnaround time

att = tat / n;

printf("\n2. Gantt Chart:\n");

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

printf("--------");

printf("\n");

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

printf("| P%d ", pid[i]);

printf("|\n");
for (i = 0; i < n; i++) {

printf("--------");

printf("\n");

printf("0\t");

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

printf("%d\t", wt[i] + bt[i]);

printf("\n");

printf("\n3. Average values:\n");

printf("Avg. waiting time= %f\n", awt);

printf("Avg. turnaround time= %f", att);

printf("\n");

return 0;
}
2. SHORTEST JOB FIRST (SJF):

#include <stdio.h>

void sortProcesses(int n, int bt[], int pid[]) {

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

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

if (bt[j] > bt[j + 1]) {

// Swap burst time

int tempBt = bt[j];

bt[j] = bt[j + 1];

bt[j + 1] = tempBt;
// Swap process ID

int tempPid = pid[j];

pid[j] = pid[j + 1];

pid[j + 1] = tempPid;

int main() {

int pid[15];

int bt[15];

int n;

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

scanf("%d", &n);

printf("Enter process num : ");

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

scanf("%d", &pid[i]);

printf("Enter burst time : ");

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


scanf("%d", &bt[i]);

// Sort processes based on burst time

sortProcesses(n, bt, pid);

int i, wt[n];

wt[0] = 0;

// for calculating waiting time of each process

for (i = 1; i < n; i++) {

wt[i] = bt[i - 1] + wt[i - 1];

printf("\n1. SJF Scheduling table\n");

printf("Process ID Burst Time Waiting Time Turnaround Time\n");

float twt = 0.0;

float tat = 0.0;

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

printf("%d\t\t", pid[i]);

printf("%d\t\t", bt[i]);

printf("%d\t\t", wt[i]);

// calculating and printing turnaround time of each process

printf("%d\t\t", bt[i] + wt[i]);


printf("\n");

// for calculating total waiting time

twt += wt[i];

// for calculating total turnaround time

tat += (wt[i] + bt[i]);

float att, awt;

// for calculating average waiting time

awt = twt / n;

// for calculating average turnaround time

att = tat / n;

printf("\n2. Gantt Chart:\n");

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

printf("--------");

printf("\n");

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

printf("| P%d ", pid[i]);

}
printf("|\n");

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

printf("--------");

printf("\n");

printf("0\t");

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

printf("%d\t", wt[i] + bt[i]);

printf("\n");

printf("\n3. Average values:\n");

printf("Avg. waiting time= %f\n", awt);

printf("Avg. turnaround time= %f", att);

printf("\n");

return 0;

}
3. PRIORITY SCHEDULING:

//Priority Scheduling

#include <stdio.h>

void sortProcessesByPriority(int n, int bt[], int pid[], int priority[]) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
// Sort by priority (highest to lowest or lowest to highest)
if (priority[j] < priority[j + 1]) {
// Swap burst time
int tempBt = bt[j];
bt[j] = bt[j + 1];
bt[j + 1] = tempBt;

// Swap process ID
int tempPid = pid[j];
pid[j] = pid[j + 1];
pid[j + 1] = tempPid;

// Swap priority
int tempPriority = priority[j];
priority[j] = priority[j + 1];
priority[j + 1] = tempPriority;
}
}
}
}

int main() {
int pid[15];
int bt[15];
int priority[15];
int n;

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


scanf("%d", &n);

printf("Enter process id, burst time, and priority of all the processes:\n");
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &pid[i], &bt[i], &priority[i]);
}

int i, wt[n];
wt[0] = 0;

// Ask the user for priority order


int isHighestPriorityFirst;
printf("Enter 1 for Highest to Lowest Priority or 0 for Lowest to Highest Priority: ");
scanf("%d", &isHighestPriorityFirst);

// Sort processes based on priority


if (isHighestPriorityFirst) {
sortProcessesByPriority(n, bt, pid, priority);
} else {
// If Lowest to Highest Priority, reverse the sorting order
for (int i = 0; i < n / 2; i++) {
// Swap burst time
int tempBt = bt[i];
bt[i] = bt[n - i - 1];
bt[n - i - 1] = tempBt;

// Swap process ID
int tempPid = pid[i];
pid[i] = pid[n - i - 1];
pid[n - i - 1] = tempPid;

// Swap priority
int tempPriority = priority[i];
priority[i] = priority[n - i - 1];
priority[n - i - 1] = tempPriority;
}
}

// for calculating waiting time of each process


for (i = 1; i < n; i++) {
wt[i] = bt[i - 1] + wt[i - 1];
}

printf("\n1. Priority-Based Scheduling table\n");


printf("Process ID Burst Time Waiting Time Turnaround Time\n");
float twt = 0.0;
float tat = 0.0;
for (i = 0; i < n; i++) {
printf("%d\t\t", pid[i]);
printf("%d\t\t", bt[i]);
printf("%d\t\t", wt[i]);

// calculating and printing turnaround time of each process


printf("%d\t\t", bt[i] + wt[i]);
printf("\n");

// for calculating total waiting time


twt += wt[i];

// for calculating total turnaround time


tat += (wt[i] + bt[i]);
}
float att, awt;

// for calculating average waiting time


awt = twt / n;

// for calculating average turnaround time


att = tat / n;

printf("\n2. Gantt Chart:\n");


for (i = 0; i < n; i++) {
printf("--------");
}
printf("\n");

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


printf("| P%d ", pid[i]);
}
printf("|\n");

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


printf("--------");
}
printf("\n");

printf("0\t");

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


printf("%d\t", wt[i] + bt[i]);
}
printf("\n");

printf("\n3. Average values:\n");


printf("Avg. waiting time= %f\n", awt);
printf("Avg. turnaround time= %f", att);
printf("\n");

return 0;
}
WITHOUT ARRIVAL TIME:

#include<stdio.h>

/*

at = Arrival Time

bt = Burst Time

rt = Response Time

time_q = Time Quantum

*/

int at=0,bt[100],rt[100],temp[100]; // Global variables

float wait_time=0,turn_time=0;

void main()

int c,j,n,time,r,flag=0,time_q,ltt,i,wt=0;

printf("Enter no.of process:");

scanf("%d",&n); // no. processes

r=n;

for(c=0; c<n; c++) // Take inputs from the user

printf("Enter burst time of p%d: \t",c+1);

scanf("%d",&bt[c]);
rt[c]=bt[c];

temp[c]=bt[c];

printf("\n");

printf("Enter time slice:\t");

scanf("%d",&time_q); // Quantuam time

printf("\n\n\tprocess\tAT\tTAT\tWT\torder\n\n");

for(time=0,c=0; r!=0;)

if(rt[c]<=time_q && rt[c]>0) // rt[] have the same values of burst time S

time=time+rt[c];

rt[c]=0;

flag=1;

else if (rt[c]>0)

rt[c]=rt[c]-time_q;

time=time+time_q;

if(rt[c]==0 && flag==1)

wt=0;

wt = time-at-bt[c];
r--;

printf("\tP%d\t%d\t%d\t%d\t%d\n",c+1,at,time-at,wt,c+1);

ltt=time-at;

wait_time=wait_time+time-at-bt[c];

turn_time=turn_time+time-at;

flag=0;

if( c == n-1)

c=0;

else if(at<=time)

c++;

else

c=0;

j=0;

printf("\n\n\n");

printf("Gantt Chart ");

printf("\n\n\n");

printf("\t");

for (int i=at;i<=time;i++){

printf("--");

printf("--");
printf("\n");

printf("\t");

for(i=at; i<time;)

if(bt[j]>=time_q)

printf("P%d |\t",j+1);

i+=time_q;

bt[j]=bt[j]-time_q;

else if(bt[j]>0)

printf("P%d |\t",j+1);

i+=bt[j];

bt[j]=0;

j++;

if(j>=n)

j=0;

printf("\n");
j=0;

printf("\t");

for (int i=at;i<=time;i++){

printf("--");

printf("--");

printf("\n");

printf("\t ");

for(i=at; i<time;)

if(temp[j]>=time_q)

printf(" ");

printf(" %d\t",i+time_q);

i+=time_q;

temp[j]=temp[j]-time_q;

else if(temp[j]>0)

{ printf(" ");

printf("%d\t",i+temp[j]);
i+=temp[j];

temp[j]=0;

j++;

if(j>=n)

j=0;

printf("\n\n\n");

printf("\nAverage_waiting_time=%.2f\n",wait_time/n);

printf("Average_turn_around_time=%.2f\n",turn_time/n);

printf("\n\n");

}
RESULT :

Thus the codes were successfully executed.

You might also like