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

assignmnt

The document discusses four scheduling algorithms used in operating systems: First-Come-First-Serve (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin (RR). Each algorithm has its own strengths and weaknesses, with FCFS being simple but potentially leading to long wait times, SJF minimizing average wait time but requiring burst time knowledge, Priority Scheduling focusing on urgent processes, and RR ensuring fair CPU time distribution. The choice of the most efficient scheduling algorithm depends on system requirements and process characteristics.

Uploaded by

csfrockz
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)
10 views

assignmnt

The document discusses four scheduling algorithms used in operating systems: First-Come-First-Serve (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin (RR). Each algorithm has its own strengths and weaknesses, with FCFS being simple but potentially leading to long wait times, SJF minimizing average wait time but requiring burst time knowledge, Priority Scheduling focusing on urgent processes, and RR ensuring fair CPU time distribution. The choice of the most efficient scheduling algorithm depends on system requirements and process characteristics.

Uploaded by

csfrockz
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/ 14

FCFS, SJF, Priority, and Round Robin are scheduling algorithms used

in operating systems to manage the execution of processes in a multi-


tasking environment.
1. First-Come-First-Serve (FCFS): This is the simplest scheduling
algorithm, where the processes are executed in the order in
which they arrive in the ready queue. The CPU is allocated to
the first process in the queue, and it continues to execute until it
completes or it enters a waiting state. The main advantage of
this algorithm is its simplicity, but it can lead to long waiting
times for processes that arrive later.
2. Shortest Job First (SJF): This algorithm schedules the process
with the shortest burst time first. The idea is to minimize the
average waiting time for all the processes in the queue.
However, this algorithm requires knowledge of the burst time of
each process, which may not be available in real-time systems.
3. Priority Scheduling: This algorithm assigns a priority to each
process based on its importance or urgency. The CPU is
allocated to the process with the highest priority, and if multiple
processes have the same priority, FCFS scheduling is used. This
algorithm is useful for real-time systems where some processes
require immediate attention.
4. Round Robin (RR): This algorithm allocates a fixed time slice to
each process in the queue, and the CPU switches between
processes after each time slice expires. The time slice is
typically small, between 10-100ms, and this ensures that no
process monopolizes the CPU for too long. The disadvantage of
this algorithm is that short processes may have to wait for their
turn, and long processes may be interrupted frequently, leading
to increased overhead.
The most efficient scheduling algorithm depends on the specific
system requirements and workload. Generally, priority scheduling is
preferred for real-time systems, while SJF or Round Robin is
preferred for general-purpose systems. SJF scheduling minimizes the
average waiting time, but it requires knowledge of the burst time of
each process, which may not be practical. Round Robin scheduling is
simple and fair, but it may not be suitable for systems with long-
running processes.

However, I can provide a general explanation of each of the four


scheduling algorithms and their strengths and weaknesses.

First-Come-First-Serve (FCFS):
In FCFS, the process that arrives first is executed first. This
scheduling algorithm is simple and easy to implement, but it can
result in longer wait times for processes that arrive later.

Shortest Job First (SJF):


In SJF, the process with the shortest estimated running time is
executed first. This algorithm minimizes the average wait time and
turnaround time, but requires accurate estimates of the running time
for each process.

Priority Scheduling:
In priority scheduling, each process is assigned a priority, and the
process with the highest priority is executed first. This scheduling
algorithm is useful when some processes are more important than
others, but can result in lower priority processes waiting for long
periods of time.

Round Robin:
In Round Robin, each process is given a fixed amount of time to
execute before being preempted and allowing the next process to run.
This algorithm ensures that each process gets a fair share of the CPU
time, but can result in longer turnaround times for processes with
longer burst times.

In order to determine the most efficient scheduling algorithm for a


specific system, it is important to consider the characteristics of the
processes being scheduled, such as their arrival time, burst time, and
priority. Additionally, the performance metrics that are most
important for the system, such as average wait time or response time,
should be considered. Based on these factors, a scheduling algorithm
can be chosen that minimizes the desired performance metric for the
system.

Assumptions:

 The processes are defined by their arrival time, priority, and


burst time.
 The priority of a process ranges from 1 (lowest) to 10 (highest).
 The quantum for Round Robin algorithm is fixed at 2 units.
 If two processes have the same priority or burst time, FCFS is
used as a tiebreaker.

First Come, First Served (FCFS):


#include <stdio.h>
#include <stdlib.h>

typedef struct Process {


int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
} Process;

void fcfs(Process* processes, int num_processes) {


int current_time = 0;
for (int i = 0; i < num_processes; i++) {
Process* p = &processes[i];
if (current_time < p->arrival_time) {
current_time = p->arrival_time;
}
p->waiting_time = current_time - p->arrival_time;
p->turnaround_time = p->waiting_time + p->burst_time;
current_time += p->burst_time;
}
}

int main() {
Process processes[] = {
{1, 0, 10, 0, 0},
{2, 2, 5, 0, 0},
{3, 4, 2, 0, 0},
};
int num_processes = sizeof(processes) / sizeof(Process);
fcfs(processes, num_processes);
printf("FCFS Scheduling:\n");
for (int i = 0; i < num_processes; i++) {
printf("Process %d: Waiting Time=%d, Turnaround
Time=%d\n", processes[i].pid, processes[i].waiting_time,
processes[i].turnaround_time);
}
return 0;
}
Output
/tmp/CjgLvtuEQt.o
FCFS Scheduling:
Process 1: Waiting Time=0, Turnaround Time=10
Process 2: Waiting Time=8, Turnaround Time=13
Process 3: Waiting Time=11, Turnaround Time=13

C program for implementing the Shortest Job First (SJF)


algorithm:
#include<stdio.h>

int main() {
int n, i, j, temp, total_time = 0, avg_waiting_time = 0,
avg_turnaround_time = 0;
int burst_time[20], process[20], waiting_time[20],
turnaround_time[20];
printf("Enter the number of processes: ");
scanf("%d", &n);

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


printf("Enter the burst time for process %d: ", i + 1);
scanf("%d", &burst_time[i]);
process[i] = i + 1;
}

// Sorting the burst time in ascending order using bubble sort


for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
if(burst_time[i] > burst_time[j]) {
temp = burst_time[i];
burst_time[i] = burst_time[j];
burst_time[j] = temp;
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}

waiting_time[0] = 0;
for(i = 1; i < n; i++) {
waiting_time[i] = 0;
for(j = 0; j < i; j++) {
waiting_time[i] += burst_time[j];
}
total_time += waiting_time[i];
}

avg_waiting_time = total_time / n;
total_time = 0;

printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround


Time\n");
for(i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
total_time += turnaround_time[i];
printf("%d\t\t%d\t\t%d\t\t%d\n", process[i], burst_time[i],
waiting_time[i], turnaround_time[i]);
}

avg_turnaround_time = total_time / n;

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


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

return 0;
}

output
Enter the number of processes: 10
Enter the burst time for process 1: 20
Enter the burst time for process 2: 14
Enter the burst time for process 3: 25
Enter the burst time for process 4: 45
Enter the burst time for process 5: 41
Enter the burst time for process 6: 10
Enter the burst time for process 7: 24
Enter the burst time for process 8: 1
Enter the burst time for process 9: 3
Enter the burst time for process 10: 1
Process Burst Time Waiting Time Turnaround Time
8 1 0 1
10 1 1 2
9 3 2 5
6 10 5 15
2 14 15 29
1 20 29 49
7 24 49 73
3 25 73 98
5 41 98 139
4 45 139 184

Average Waiting Time: 41


Average Turnaround Time: 59

C program for Priority Scheduling:

#include<stdio.h>

int main() {
int n, i, j, temp, total = 0;
float avg_waiting_time, avg_turnaround_time;
int burst_time[20], priority[20], waiting_time[20],
turnaround_time[20];

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


scanf("%d", &n);

printf("Enter burst time and priority for each process:\n");


for(i = 0; i < n; i++) {
printf("Process %d: ", i+1);
scanf("%d %d", &burst_time[i], &priority[i]);
}

// Sort the processes based on priority using bubble sort


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 = burst_time[i];
burst_time[i] = burst_time[j];
burst_time[j] = temp;
}
}
}

waiting_time[0] = 0;
for(i = 1; i < n; i++) {
waiting_time[i] = 0;
for(j = 0; j < i; j++) {
waiting_time[i] += burst_time[j];
}
total += waiting_time[i];
}

avg_waiting_time = (float)total / n;
total = 0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround
Time");
for(i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
total += turnaround_time[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d", i+1, burst_time[i],
waiting_time[i], turnaround_time[i]);
}

avg_turnaround_time = (float)total / n;
printf("\n\nAverage Waiting Time: %.2f", avg_waiting_time);
printf("\nAverage Turnaround Time: %.2f\n",
avg_turnaround_time);

return 0;
}
Output
Enter the number of processes: 2
Enter burst time and priority for each process:
Process 1: 2
1
Process 2: 2
1
Process Burst Time Waiting Time Turnaround Time
P[1] 2 0 2
P[2] 2 2 4
Average Waiting Time: 1.00
Average Turnaround Time: 3.00

Implementation of the Round Robin scheduling algorithm in C.


#include <stdio.h>

#define MAX_PROCESSES 10
#define TIME_QUANTUM 2

struct Process {
int pid;
int arrivalTime;
int burstTime;
int remainingTime;
};

int main() {
struct Process processes[MAX_PROCESSES];
int numProcesses, i, j, time, totalBurstTime = 0;
float avgWaitingTime = 0;

// Read in the number of processes


printf("Enter the number of processes: ");
scanf("%d", &numProcesses);
// Read in the arrival time and burst time for each process
for (i = 0; i < numProcesses; i++) {
printf("Enter the arrival time and burst time for process %d: ",
i+1);
scanf("%d %d", &processes[i].arrivalTime,
&processes[i].burstTime);
processes[i].pid = i+1;
processes[i].remainingTime = processes[i].burstTime;
totalBurstTime += processes[i].burstTime;
}

// Execute the processes


for (time = 0; time < totalBurstTime;) {
for (i = 0; i < numProcesses; i++) {
if (processes[i].arrivalTime <= time &&
processes[i].remainingTime > 0) {
if (processes[i].remainingTime <= TIME_QUANTUM) {
time += processes[i].remainingTime;
processes[i].remainingTime = 0;
printf("Process %d finished at time %d\n",
processes[i].pid, time);
avgWaitingTime += time - processes[i].arrivalTime -
processes[i].burstTime;
} else {
time += TIME_QUANTUM;
processes[i].remainingTime -= TIME_QUANTUM;
printf("Process %d executed for %d units at time %d\n",
processes[i].pid, TIME_QUANTUM, time);
}
}
}
}

avgWaitingTime /= numProcesses;
printf("Average waiting time: %.2f\n", avgWaitingTime);

return 0;
}
Output:-
Enter the number of processes: 1
Enter the arrival time and burst time for process 1: 2

You might also like