Vani Os
Vani Os
Aim:- Write a program to implement First Come First Serve(FCFS) CPU scheduling alogorithm.
Theory:- FCFS stands for First Come First Serve. In the FCFS scheduling algorithm, the job that
arrived first in the ready queue is allocated to the CPU and then the job that came second,
and so on. We can say that the ready queue acts as a FIFO (First In First Out) queue thus
the arriving jobs/processes are placed at the end of the queue.
FCFS is a non-preemptive scheduling algorithm as a process holds the CPU until it either
terminates or performs I/O. Thus, if a longer job has been assigned to the CPU then many
shorter jobs after it will have to wait. This algorithm is used in most batch operating systems.
Program:-
#include<iostream> using namespace std; void
wt[i-1] ;
}
void findTurnAroundTime( int processes[], int n,
int bt[], int wt[], int tat[])
{ for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt);
findTurnAroundTime(processes, n, bt, wt, tat); cout
<< "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i]; total_tat = total_tat
Name :- Vani
Enrollment no. :- 12825502722
}
Name :- Vani
Output:-
Aim:- Write a program to implement Shortest Job First(SJF) CPU scheduling alogorithm.
Theory:- The shortest job first (SJF) or shortest job next, is a scheduling policy that selects
the waiting process with the smallest execution time to execute next. SJN, also known as
Shortest Job Next (SJN), can be preemptive or non-preemptive.
Program:- #include
<iostream> using
main() {
process: ";
cout << "P" << i + 1 << ": "; cin >> A[i][1];
A[i][0] = i + 1;
index = i;
A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
A[i][2] = 0;
A[i][3] = A[i][1] + A[i][2]; total += A[i][3]; cout << "P" << A[i][0] << " " <<
A[i][1] << " " << A[i][2] << " " << A[i][3] << endl;
Time= " << avg_wt << endl; cout << "Average Turnaround
}
Name :- Vani
Output:-
P1: 5
P2: 10
P3: 15
P BT WT TAT
P1 5 0 5
P2 10 5 15
P3 15 15 30
Average Waiting Time= 6.66667 Average Turnaround Time= 16.6667
Name :- Vani
Enrollment no. :- 12825502722
Aim:- Write a program to implement Shortest Remaining Time First(SRTF) CPU scheduling alogorithm.
Theory:- This Algorithm is the preemptive version of SJF scheduling. In SRTF, the
execution of the process can be stopped after certain amount of time. At the arrival of every
process, the short term scheduler schedules the process with the least remaining burst time
among the list of available processes and the running process.
Once all the processes are available in the ready queue, No preemption will be done and
the algorithm will work as SJF scheduling. The context of the process is saved in the
Process Control Block when the process is removed from the execution and the next
process is scheduled. This PCB is accessed on the next execution of this process.
int main() {
A[i][1];
A[i][0] = i + 1;
index = i;
index = j;
Name :- Vani
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; for (i = 1; i
< n; i++) {
A[i][2] = 0;
A[i][2] += A[j][1];
total += A[i][2];
total += A[i][3];
cout << "P" << A[i][0] << " " << A[i][1] << " " << A[i][2] << " " << A[i][3]
<< endl;
}
Name :- Vani
Output:-
P1: 10
P2: 12
P3: 18
P BT WT TAT
P1 10 0 10
P2 12 10 22
P3 18 22 40
Average Waiting Time= 10.6667
Name :-Vani
Theory:- Round Robin is a CPU scheduling algorithm where each process is cyclically
assigned a fixed time slot. It is the preemptive version of the First come First Serve CPU
Scheduling algorithm.
• Round Robin CPU Algorithm generally focuses on Time Sharing technique.
• The period of time for which a process or job is allowed to run in a pre-emptive method is
called time quantum.
• Each process or job present in the ready queue is assigned the CPU for that time
quantum, if the execution of the process is completed during that time then the process
will end else the process will go back to the waiting table and wait for its next turn to
complete the execution.
= 0; i < n; i++)
Name :-Vani
12825502722
while (1)
{ if
(rem_bt[i] > 0)
{ done = false;
{ t += quantum;
rem_bt[i]
-= quantum;
} else {
t = t + rem_bt[i]; wt[i] = t
- bt[i];
rem_bt[i]
= 0;
} if (done
== true)
break;
Name :-Vani
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) { for (int i = 0; i < n;
cout
i < n; i++)
{
total_wt = total_wt + wt[i]; total_tat = total_tat + tat[i]; cout << " "
<< i + 1 << "\t\t" << bt[i] << "\t " << wt[i] << "\t\t " << tat[i] << endl;
cout << "Average waiting time = " << (float)total_wt / (float)n; cout
int main()
n, burst_time, quantum);
return 0;
Name :-Vani
Output:-
PN BT WT TAT
1 10 13 23
2 5 10 15
3 8 13 21
Average waiting time = 12