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

Scheduling Algorithms

The document discusses and provides code for implementing different CPU scheduling algorithms: 1) Round Robin scheduling: Code takes input for number of processes and their arrival/burst times. It then calculates waiting time, turnaround time, and average times. 2) FCFS scheduling: Similar to above but processes in order of arrival without time quanta. 3) Shortest Job First: Sorts processes by burst time and calculates waiting and turnaround times. 4) Priority scheduling: Takes priority as additional input and sorts processes by priority to calculate times. Output for each shows calculations and averages.

Uploaded by

2021cse.rl1
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)
44 views

Scheduling Algorithms

The document discusses and provides code for implementing different CPU scheduling algorithms: 1) Round Robin scheduling: Code takes input for number of processes and their arrival/burst times. It then calculates waiting time, turnaround time, and average times. 2) FCFS scheduling: Similar to above but processes in order of arrival without time quanta. 3) Shortest Job First: Sorts processes by burst time and calculates waiting and turnaround times. 4) Priority scheduling: Takes priority as additional input and sorts processes by priority to calculate times. Output for each shows calculations and averages.

Uploaded by

2021cse.rl1
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
You are on page 1/ 12

Round Robin:

#include<stdio.h>

#include<conio.h>

void main()

// initlialize the variable name

int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];

float avg_wt, avg_tat;

printf(" Total number of process in the system: ");

scanf("%d", &NOP);

y = NOP; // Assign the number of process to variable y

// Use for loop to enter the details of the process like Arrival time and the Burst Time

for(i=0; i<NOP; i++)

printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);

printf(" Arrival time is: \t"); // Accept arrival time

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

printf(" \nBurst time is: \t"); // Accept the Burst time

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

temp[i] = bt[i]; // store the burst time in temp array

// Accept the Time qunat

printf("Enter the Time Quantum for the process: \t");

scanf("%d", &quant);

// Display the process No, burst time, Turn Around Time and the waiting time

printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");

for(sum=0, i = 0; y!=0; )
{

if(temp[i] <= quant && temp[i] > 0) // define the conditions

sum = sum + temp[i];

temp[i] = 0;

count=1;

else if(temp[i] > 0)

temp[i] = temp[i] - quant;

sum = sum + quant;

if(temp[i]==0 && count==1)

y--; //decrement the process no.

printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);

wt = wt+sum-at[i]-bt[i];

tat = tat+sum-at[i];

count =0;

if(i==NOP-1)

i=0;

else if(at[i+1]<=sum)

i++;

else

i=0;
}

// represents the average waiting time and Turn Around time

avg_wt = wt * 1.0/NOP;

avg_tat = tat * 1.0/NOP;

printf("\n Average Turn Around Time: \t%f", avg_wt);

printf("\n Average Waiting Time: \t%f", avg_tat);

getch();

Output: Total number of process in the system: 2


Enter the Arrival and Burst time of the Process[1]

Arrival time is: 3

Burst time is: 4

Enter the Arrival and Burst time of the Process[2]

Arrival time is: 4

Burst time is: 3

Enter the Time Quantum for the process: 5

Process No Burst Time TAT Waiting Time

Process No[1] 4 1 -3

Process No[2] 3 3 0

Average Turn Around Time: -1.500000

Average Waiting Time: 2.000000

FCFS:
#include<stdio.h>

int main()

int n,bt[30],wait_t[30],turn_ar_t[30],av_wt_t=0,avturn_ar_t=0,i,j;
printf("Please enter the total number of processes(maximum 30):"); // the maximum process that
be used to calculate is specified.

scanf("%d",&n);

printf("\nEnter The Process Burst Timen");

for(i=0;i<n;i++) // burst time for every process will be taken as input

printf("P[%d]:",i+1);

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

wait_t[0]=0;

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

wait_t[i]=0;

for(j=0;j<i;j++)

wait_t[i]+=bt[j];

printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

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

turn_ar_t[i]=bt[i]+wait_t[i];

av_wt_t+=wait_t[i];

avturn_ar_t+=turn_ar_t[i];

printf("\nP[%d]\t\t%d\t\t\t%d\t\t\t\t%d",i+1,bt[i],wait_t[i],turn_ar_t[i]);

av_wt_t/=i;
avturn_ar_t/=i; // average calculation is done here

printf("\nAverage Waiting Time:%d",av_wt_t);

printf("\nAverage Turnaround Time:%d",avturn_ar_t);

return 0;

Output: Please enter the total number of processes(maximum 30):2


Enter The Process Burst TimenP[1]:2

P[2]:3

Process Burst Time Waiting Time Turnaround Time

P[1] 2 0 2

P[2] 3 2 5

Average Waiting Time:1

Average Turnaround Time:3

Shortest job first :

#include <stdio.h>

int main()

// Matrix for storing Process Id, Burst

// Time, Average Waiting Time & Average

// Turn Around Time.

int A[100][4];

int i, j, n, total = 0, index, temp;

float avg_wt, avg_tat;

printf("Enter number of process: ");

scanf("%d", &n);

printf("Enter Burst Time:\n");

// User Input Burst Time and alloting Process Id.

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


printf("P%d: ", i + 1);

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

A[i][0] = i + 1;

// Sorting process according to their Burst Time.

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

index = i;

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

if (A[j][1] < A[index][1])

index = j;

temp = A[i][1];

A[i][1] = A[index][1];

A[index][1] = temp;

temp = A[i][0];

A[i][0] = A[index][0];

A[index][0] = temp;

A[0][2] = 0;

// Calculation of Waiting Times

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

A[i][2] = 0;

for (j = 0; j < i; j++)

A[i][2] += A[j][1];

total += A[i][2];

avg_wt = (float)total / n;

total = 0;

printf("P BT WT TAT\n");

// Calculation of Turn Around Time and printing the

// data.
for (i = 0; i < n; i++) {

A[i][3] = A[i][1] + A[i][2];

total += A[i][3];

printf("P%d %d %d %d\n", A[i][0],

A[i][1], A[i][2], A[i][3]);

avg_tat = (float)total / n;

printf("Average Waiting Time= %f", avg_wt);

printf("\nAverage Turnaround Time= %f", avg_tat);

Output: Enter number of process: 2


Enter Burst Time:

P1: 3

P2: 1

P BT WT TAT

P2 1 0 1

P1 3 1 4

Average Waiting Time= 0.500000

Average Turnaround Time= 2.500000

Priority scheduling:
#include<stdio.h>

// structure representing a structure

struct priority_scheduling {

// name of the process

char process_name;

// time required for execution

int burst_time;
// waiting time of a process

int waiting_time;

// total time of execution

int turn_around_time;

// priority of the process

int priority;

};

int main() {

// total number of processes

int number_of_process;

// total waiting and turnaround time

int total = 0;

// temporary structure for swapping

struct priority_scheduling temp_process;

// ASCII numbers are used to represent the name of the process

int ASCII_number = 65;

// swapping position

int position;

// average waiting time of the process

float average_waiting_time;
// average turnaround time of the process

float average_turnaround_time;

printf("Enter the total number of Processes: ");

// get the total number of the process as input

scanf("%d", & number_of_process);

// initializing the structure array

struct priority_scheduling process[number_of_process];

printf("\nPlease Enter the Burst Time and Priority of each process:\n");

// get burst time and priority of all process

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

// assign names consecutively using ASCII number

process[i].process_name = (char) ASCII_number;

printf("\nEnter the details of the process %c \n", process[i].process_name);

printf("Enter the burst time: ");

scanf("%d", & process[i].burst_time);

printf("Enter the priority: ");

scanf("%d", & process[i].priority);

// increment the ASCII number to get the next alphabet

ASCII_number++;

// swap process according to high priority


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

position = i;

for (int j = i + 1; j < number_of_process; j++) {

// check if priority is higher for swapping

if (process[j].priority > process[position].priority)

position = j;

// swapping of lower priority process with the higher priority process

temp_process = process[i];

process[i] = process[position];

process[position] = temp_process;

// First process will not have to wait and hence has a waiting time of 0

process[0].waiting_time = 0;

for (int i = 1; i < number_of_process; i++) {

process[i].waiting_time = 0;

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

// calculate waiting time

process[i].waiting_time += process[j].burst_time;

// calculate total waiting time

total += process[i].waiting_time;

// calculate average waiting time

average_waiting_time = (float) total / (float) number_of_process;


// assigning total as 0 for next calculations

total = 0;

printf("\n\nProcess_name \t Burst Time \t Waiting Time \t Turnaround Time\n");

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

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

// calculating the turnaround time of the processes

process[i].turn_around_time = process[i].burst_time + process[i].waiting_time;

// calculating the total turnaround time.

total += process[i].turn_around_time;

// printing all the values

printf("\t %c \t\t %d \t\t %d \t\t %d", process[i].process_name, process[i].burst_time,


process[i].waiting_time, process[i].turn_around_time);

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

// calculating the average turn_around time

average_turnaround_time = (float) total / (float) number_of_process;

// average waiting time

printf("\n\n Average Waiting Time : %f", average_waiting_time);

// average turnaround time

printf("\n Average Turnaround Time: %f\n", average_turnaround_time);

return 0;
}

Output: Enter the total number of Processes: 2


Please Enter the Burst Time and Priority of each process:

Enter the details of the process A

Enter the burst time: 3

Enter the priority: 1

Enter the details of the process B

Enter the burst time: 5

Enter the priority: 3

Process_name Burst Time Waiting Time Turnaround Time

------------------------------------------------------------

B 5 0 5

-----------------------------------------------------------

A 3 5 8

-----------------------------------------------------------

Average Waiting Time : 2.500000

Average Turnaround Time: 6.500000

You might also like