Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views11 pages

CSE3134 Assignment 01

This document presents an assignment comparing three CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF - Preemptive), and Round Robin (RR). It evaluates their performance based on average waiting time and turnaround time through simulation, concluding that SJF is the most efficient. The document includes detailed algorithm steps, C program code for each algorithm, and suitable scenarios for their application.
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)
17 views11 pages

CSE3134 Assignment 01

This document presents an assignment comparing three CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF - Preemptive), and Round Robin (RR). It evaluates their performance based on average waiting time and turnaround time through simulation, concluding that SJF is the most efficient. The document includes detailed algorithm steps, C program code for each algorithm, and suitable scenarios for their application.
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/ 11

School of Science and Technology

B.Sc. in Computer Science and Engineering

Assignment : 01

Assignment on CPU Scheduling Algorithm Comparison Based on


Performance Analysis

Submitted By Submitted To

Name: Md. Zubaer Ahammed Prof. Dr. Md. Asraf Ali


Student ID: 20-0-52-801-006 Associate Professor (Computer
Course Title: Operating System Science),
Adjunct Faculty,
SST, Bangladesh Open University
Course Code: CSE3134 Gazipur-1705

Date of Submission: 31 May 2025


Introduction:
CPU scheduling is an essential task of an operating system for managing the execution of processes.
The performance of a CPU scheduling algorithm directly affects system responsiveness and
throughput. This assignment evaluates three widely-used CPU scheduling algorithms: First Come
First Serve (FCFS), Shortest Job First (SJF - Preemptive), and Round Robin (RR). Through
simulation, we compare their average waiting time and turnaround time to understand which
performs best under different conditions.

Objectives:
To analyze and compare the performance of three CPU scheduling algorithms using simulation and
evaluate their effectiveness based on average waiting time and turnaround time.

Input (Assumed Custom Test Case):


Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

We will analyze the performance of:

 FCFS (First-Come, First-Served)


 SJF (Shortest Job First) Preemptive (SRTF)
 RR (Round Robin) with Quantum = 4ms

1. Comparison of Average Waiting Time and Turnaround


Time
A. FCFS Scheduling

Gantt Chart:
P1 | P2 | P3 | P4
Start–End: 0–8 | 8–12 | 12–21 | 21–26

Process Waiting Time Turnaround Time


P1 0 8
P2 7 11
P3 10 19
P4 18 23

 Avg Waiting Time: (0+7+10+18)/4 = 8.75 ms


 Avg Turnaround Time: (8+11+19+23)/4 = 15.25 ms
Algorithm Steps for FCFS:

1. Start
2. Input number of processes and for each process: arrival time, burst time, and name.
3. Sort processes based on arrival time.
4. For the first process:
o Set start time = arrival time
o Waiting time = 0
o Turnaround time = burst time
5. For the next processes:
o Start time = finish time of previous process
o Waiting time = start time - arrival time
o Turnaround time = waiting time + burst time
6. Calculate average waiting time and turnaround time
7. Stop

C Program Code for FCFS:

#include <stdio.h>

int main() {

int n, i;

int arrival[10], burst[10], start[10], finish[10], wait[10], tat[10];

char pname[10][10];

int total_wait = 0, total_tat = 0;

printf("Enter number of processes: ");

scanf("%d", &n);

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

printf("Enter Process Name, Arrival Time & Burst Time: ");

scanf("%s %d %d", pname[i], &arrival[i], &burst[i]);

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

if(i == 0)

start[i] = arrival[i];

else

start[i] = (finish[i - 1] > arrival[i]) ? finish[i - 1] : arrival[i];


wait[i] = start[i] - arrival[i];

finish[i] = start[i] + burst[i];

tat[i] = finish[i] - arrival[i];

total_wait += wait[i];

total_tat += tat[i];

printf("\nPName\tArrival\tBurst\tStart\tFinish\tWaiting\tTAT\n");

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

printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\n", pname[i], arrival[i], burst[i], start[i],


finish[i], wait[i], tat[i]);

printf("\nAverage Waiting Time: %.2f", (float)total_wait/n);

printf("\nAverage Turnaround Time: %.2f\n", (float)total_tat/n);

return 0;

Output:
B. SJF Preemptive (SRTF)

Gantt Chart (based on arrival and shortest remaining time):


P1 (0–1) → P2 (1–5) → P4 (5–10) → P1 (10–17) → P3 (17–26)

Process Waiting Time Turnaround Time


P1 9 17
P2 0 4
P3 15 24
P4 2 7

 Avg Waiting Time: (9+0+15+2)/4 = 6.5 ms


 Avg Turnaround Time: (17+4+24+7)/4 = 13 ms

Algorithm Steps for SJF::

1. Start
2. Input number of processes and for each: arrival time, burst time, and name
3. Initialize remaining times = burst times
4. At each unit of time:
o Select the process with the shortest remaining burst time among those that have
arrived
o Execute for 1 time unit
o Decrease remaining time of selected process
o If a process finishes (remaining time = 0), record finish time
5. Calculate waiting time = finish time - arrival time - burst time
6. Calculate turnaround time = finish time - arrival time
7. Compute average times
8. Stop

C Program Code for SJF:

#include <stdio.h>

int main() {

int at[10], bt[10], rt[10], finish[10], wt[10], tat[10];

int n, time = 0, done = 0, shortest, min_bt = 999, i;

float total_wt = 0, total_tat = 0;

char pname[10][10];

int complete[10] = {0};

printf("Enter number of processes: ");

scanf("%d", &n);

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


printf("Enter process name, arrival time & burst time: ");

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

rt[i] = bt[i];

while (done != n) {

shortest = -1;

min_bt = 999;

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

if (at[i] <= time && rt[i] < min_bt && rt[i] > 0) {

min_bt = rt[i];

shortest = i;

if (shortest == -1) {

time++;

continue;

rt[shortest]--;

time++;

if (rt[shortest] == 0) {

done++;

finish[shortest] = time;

wt[shortest] = finish[shortest] - at[shortest] - bt[shortest];


tat[shortest] = finish[shortest] - at[shortest];

total_wt += wt[shortest];

total_tat += tat[shortest];

printf("\nPName\tArrival\tBurst\tFinish\tWaiting\tTAT\n");

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

printf("%s\t%d\t%d\t%d\t%d\t%d\n", pname[i], at[i], bt[i], finish[i], wt[i], tat[i]);

printf("\nAverage Waiting Time: %.2f", total_wt / n);

printf("\nAverage Turnaround Time: %.2f\n", total_tat / n);

return 0;

Output:
C. Round Robin (Quantum = 4)

Gantt Chart (approximated):


P1 (0–4) → P2 (4–8) → P3 (8–12) → P4 (12–16) → P1 (16–20) → P3 (20–24) → P1 (24–
25) → P3 (25–26)

Process Waiting Time Turnaround Time


P1 17 25
P2 3 7
P3 15 24
P4 7 12

 Avg Waiting Time: (17+3+15+7)/4 = 10.5 ms


 Avg Turnaround Time: (25+7+24+12)/4 = 17 ms

Algorithm Steps: (Time Quantum = 4ms)

1. Start
2. Input number of processes, burst times, and time quantum
3. Initialize remaining time = burst time for each process
4. Repeat while any process is incomplete:
o For each process:
 If remaining time > time quantum:
 Execute for time quantum
 Subtract time quantum from remaining time
 Else if remaining time > 0:
 Execute for remaining time
 Set waiting time = current time - burst time
 Set remaining time = 0
5. Calculate turnaround time = burst time + waiting time
6. Compute average times
7. Stop

C Program Code for Round Robin (Quantum = 4):

#include <stdio.h>

#include <string.h>

int main() {

int n, i, ts, time = 0, done;

int bt[10], rt[10], wt[10] = {0}, tat[10] = {0};

char pname[10][10];

printf("Enter number of processes: ");

scanf("%d", &n);

printf("Enter time quantum: ");


scanf("%d", &ts);

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

printf("Enter process name and burst time: ");

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

rt[i] = bt[i];

do {

done = 0;

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

if(rt[i] > 0) {

if(rt[i] > ts) {

time += ts;

rt[i] -= ts;

} else {

time += rt[i];

wt[i] = time - bt[i];

rt[i] = 0;

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

if(rt[i] > 0)

done = 1;

} while(done);
float total_wt = 0, total_tat = 0;

printf("\nPName\tBurst\tWaiting\tTAT\n");

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

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

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

total_wt += wt[i];

total_tat += tat[i];

printf("\nAverage Waiting Time: %.2f", total_wt / n);

printf("\nAverage Turnaround Time: %.2f\n", total_tat / n);

return 0;

Output:
2. Best Algorithm Based on Test Case
Best in terms of average waiting and turnaround time: SJF (Preemptive)

 It gives the lowest average waiting time (6.5 ms) and turnaround time (13 ms).
 It is optimal for reducing delay in short jobs, as seen in Lecture 6: “SJF is optimal – gives
minimum average waiting time”.

3. Suitable Scenarios for Each Algorithm


Algorithm Best Used When Reason
Simplicity is preferred; batch Easy to implement, but may cause long waits
FCFS
systems (convoy effect).
SJF Short tasks are frequent and Minimizes waiting time; may cause starvation
(Preemptive) known of long jobs.
Time-sharing systems with Fair CPU allocation; good response time but
Round Robin
interactive processes may increase waiting time.

You might also like