You are on page 1of 22

Name: PAMMI JASANI

SAP ID: 60004210117


Batch: B1
Branch: Computer Engineering
Experiment No. 4

( CPU scheduling- FCFS, SJF, RR, SRTF,priority etc)

Non-pre-emptive
Aim- CPU scheduling algorithms like FCFS, SJF

FCFS
Theory-
First Come First Serve paves the way for understanding of other algorithms. This algorithm may have
many disadvantages. But these disadvantages created very new and efficient algorithms. So, it is our
responsibility to learn about First Come First Serve CPU Process Scheduling Algorithms

First Come First Serve CPU Scheduling Algorithm shortly known as FCFS is the first algorithm of CPU
Process Scheduling Algorithm. In First Come First Serve Algorithm what we do is to allow the process
to execute in linear manner.

This means that whichever process enters process enters the ready queue first is executed first. This
shows that First Come First Serve Algorithm follows First In First Out (FIFO) principle.

Characteristics of FCFS CPU Process Scheduling


The characteristics of FCFS CPU Process Scheduling are:

1. Implementation is simple.
2. Does not cause any causalities while using
3. It adopts a non pre emptive and pre emptive strategy.
4. It runs each procedure in the order that they are received. 5. Arrival time is used as a selection criterion
for procedures.

Advantages of FCFS CPU Process Scheduling


The advantages of FCFS CPU Process Scheduling are:

1. In order to allocate processes, it uses the First In First Out queue.


2. The FCFS CPU Scheduling Process is straight forward and easy to implement.
3. In the FCFS situation pre emptive scheduling, there is no chance of process starving.
4. As there is no consideration of process priority, it is an equitable algorithm.

Disadvantages of FCFS CPU Process Scheduling


The disadvantages of FCFS CPU Process Scheduling are:

o FCFS CPU Scheduling Algorithm has Long Waiting Time o FCFS CPU Scheduling favors CPU over

Input or Output operations o In FCFS there is a chance of occurrence of Convoy Effect o Because FCFS is
so straight forward, it often isn't very effective. Extended waiting periods go hand in hand with this. All
other orders are left idle if the CPU is busy processing one timeconsuming order.

Code-
#include<stdio.h>
void main()
{
int
bt[100],at[100],pro[100],ct[100],tat[100],wt[100],n,i,j,k,temp,temp1,temp2
,ttat=0,twt=0;
float attat,awt;
printf("enter number of processes ");
scanf("%d",&n); k=1;
for(i=0;i<n;i++)
{
printf("enter arrival time of process %d ",i+1);
scanf("%d",&at[i]);
printf("enter burst time of process %d ",i+1);
scanf("%d",&bt[i]); pro[i]=k;
k++;
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(at[j]>at[j+1])
{
temp=at[j];
at[j]=at[j+1];
at[j+1]=temp;
temp1=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp1;
temp2=pro[j];
pro[j]=pro[j+1];
pro[j+1]=temp2;
}
}
}
int prevBt=0;
//ct
for(i=0;i<n;i++)
{
ct[i]=prevBt+bt[i];
prevBt=ct[i];
}
//tat wt
for(i=0;i<n;i++)
{
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
}
printf("Output\n");
printf("Process\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
ttat+=tat[i];
twt+=wt[i];
}
awt=(float)twt/n; attat=(float)ttat/n;
for(i=0;i<n;i++)
printf("\n\t%d\t%d\t%d\t%d\t%d\t%d\n",pro[i],at[i],bt[i],ct[i],tat[i],wt[i]);
printf("\nAvg TAT=%f\nAvg WT=%f",attat,awt);
printf("\nGant Chart \n0");
for(i=0;i<n;i++)
{
printf("|P%d| %d|",pro[i],ct[i]);
}
}
Output-
Conclusion-
FCFS is a basic scheduling algorithm that can be effective for simple systems with a small number
of tasks, but it may not be the most efficient or effective approach for more complex systems with
multiple tasks and resources to manage. Other scheduling algorithms, such as Round Robin, Priority
Scheduling, and Shortest Job First, may be better suited for such systems.

SJF
Theory-
SJF (Shortest Job First) is a scheduling algorithm used in computer operating
systems and other systems where multiple tasks or jobs are competing for
resources. In SJF, the task with the shortest expected processing time is executed
first, regardless of when it arrived.
The main idea behind SJF is to minimize the average waiting time of tasks in the
system by prioritizing shorter tasks over longer ones. This can lead to a more
efficient and responsive system, as shorter tasks are completed quickly and
resources are freed up for other tasks.
However, one potential problem with SJF is that it can lead to starvation, where
longer tasks are continually delayed in favor of shorter ones. To address this
issue, some implementations of SJF may use a variation known as Shortest
Remaining Time First (SRTF), where the task with the shortest remaining
processing time is executed next, rather than simply the shortest overall
processing time.

Code-
// SJF scheduling program in c
#include<string.h> int
main()
{
int bt[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10],ct[10];
int totwt=0,totta=0; double awt,ata; char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name, arrival time& burst time:");
scanf("%s%d%d",pn[i],&at[i],&bt[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(bt[i]<bt[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
int prevBt=0;
for(i=0;i<n;i++)
{
ct[i]=prevBt+bt[i];
prevBt=ct[i];
}
for(i=0; i<n; i++)
{ if(i==0)
st[i]=at[i]; else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+bt[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(double)totwt/n;
ata=(double)totta/n;

printf("\nProcessname\tarrivaltime\tbursttime\tCompletiontime\twaitingt
ime\tturnaroundtime");
for(i=0; i<n; i++)
{

printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],bt[i],ct[i],wt[ i],ta[i]);
}
printf("\nAverage waiting time: %f",awt);
printf("\nAverage turnaroundtime: %f",ata); return
0;
}
Output-
Conclusion-
SJF can be an effective scheduling algorithm in systems where tasks have varying processing times
and minimizing average waiting time is a priority. However, it may require additional complexity to
prevent starvation and ensure fair resource allocation for all tasks.

pre-emptive
Aim- CPU scheduling algorithms like RR, SRTF,Priority

Theory-

Shortest Job First (SJF) Preemptive


Shortest Job First (SJF) is an algorithm in which the process having the
smallest execution time is chosen for the next execution. This scheduling
method can be preemptive or non-preemptive. It significantly reduces the
average waiting time for other processes awaiting execution. The full form of
SJF is Shortest Job First.
Characteristics of SJF Scheduling

• It is associated with each job as a unit of time to complete.


• This algorithm method is helpful for batch-type processing, where
waiting for jobs to complete is not critical.
• It can improve process throughput by making sure that shorter jobs are
executed first, hence possibly have a short turnaround time.
• It improves job output by offering shorter jobs, which should be
executed first, which mostly have a shorter turnaround time.
• Shortest Job first has the advantage of having a minimum average
waiting time among all scheduling algorithms.
• It is a Greedy Algorithm.
• It may cause starvation if shorter processes keep coming. This problem
can be solved using the concept of ageing.
• It is practically infeasible as Operating System may not know burst time
and therefore may not sort them. While it is not possible to predict
execution time, several methods can be used to estimate the execution
time for a job, such as a weighted average of previous execution times.
SJF can be used in specialized environments where accurate estimates
of running time are available

In Preemptive SJF Scheduling, jobs are put into the ready queue as they come.
A process with shortest burst time begins execution. If a process with even a
shorter burst time arrives, the current process is removed or preempted from
execution, and the shorter job is allocated CPU cycle. Example:

1. At ( t =0ms ), P1 arrives. It’s the only process so CPU starts executing it.
2. At ( t = 1ms ), P2 has arrived . At this time, P1 (remaining time ) = 5 ms . P2 has 4ms ,
so as P2 is shorter, P1 is preempted and P2 process starts executing.
3. At ( t = 2ms ), P3 process has arrived. At this time, P1(remaining time) = 5ms,
P2(remaining time ) = 3 ms , P3 = 2ms. Since P3 is having least burst time, P3 is
executed .
4. At ( t = 3ms ), P4 comes , At this time, P1 = 5ms, P2 = 3ms, P3 = 1ms, P4 = 3ms. Since
P4 does not have short burst time, so P3 continues to execute.
5. At ( t= 4ms ),P3 is finished . Now, remaining tasks are P1 = 5ms, P2 = 3ms, P4 = 3ms.
As ,P2 and P4 have same time, so the task which came first will be executed first. So,
P2 gets executed first.
6. At ( t = 7ms ),P4 gets executed for 3ms.
7. At ( t = 10ms ), P1 gets executed till it finishes.
Waiting time = Completion time – Burst Time – Arrival Time

1. P1 waiting time = (15-6-0) = 9ms


2. P2 waiting time = (7-4-1) = 2ms
3. P3 waiting time = (4-2-2 )=0ms
4. P4 waiting time = (10-3-3)= 4ms

The average waiting time is ( 9 +2 + 0 + 4)/4 = 15/4 = 3.75

Priority Scheduling (Preemptive)


In Priority Scheduling, processes are assigned Priorities and the process with
the highest is executed first. Each process is assigned a priority & processes
are given CPU time according to their priority.
Preemptive Priority Scheduling Algorithm are those algorithm where if a new
process having higher priority arrives than the process currently executing ,
CPU stops the current executing process and executes the newly arrived
higher priority process.
Disadvantages:
Like FCFS and SJF , a process having lower priority can be indefinite blocked
or starved. To prevent this, we do aging where the priority of the process is
increased as it waits in the queue. So, a process which has been in a queue for
a long time will reach high priority, hence it won’t be starved.
Example:
At time t = 0
Only P1 has arrived and since its only process currently even though has
lowest priority it will get processing time and has burst time of 1ms.
At time t = 1
At time t = 1 P2 has arrived and no other process thus it will get processing
time
At time t = 2
P2 is still running but, P3 has also arrived.
P3 has priority of 3 while P2 has priority of 6. Thus –
P2 will stop and P3 will get execution time.
P3 has execution time of 3ms in this time 3 more processes arrive namely –
P4, P5 and P6. But all of them have lower priority than P3, thus these can’t
preempt the process and P3 will complete its execution time. Till time t = 5 P3
will run and complete its processing
At time t = 5
There are P2 (Priority = 6), P4 (Priority = 5), P5 (Priority = 4) and P6 (Priority
= 10) that have arrived, out of which P5 has the highest priority as it has
lowest value. Thus P5 will continue.
P5 has execution time of 5 ms. In this time P7 will have also arrived but.
That also has lower priority than P5 thus P5 will continue its execution time.
Till time t = 10 P5 will run and complete its processing.
At time t = 10
P2 P4 and P6 have arrived and P7 which will arrive at 15 ms.
Thus P4 will run as it has lowest priority of 5 and has burst time of 6 ms.
However, at t = 15ms P7 has arrived but, still priority of P4 is higher. It
will continue to run. At time t = 16 ms
All processes have arrived, thus there is no need for any type of preemption.
It will follow normal priority procedure.
Currently processes are P2, P6 and P7. P2 has lowest priority of 6 and then P7
has next of 9 and finally P6 of 10.
Thus P2 (Burst time = 7 but 1ms was served previously, so 6ms) -> P7 and
P8 will run in sequence and complete the whole process. At t = 45 ms
process will complete.

Round Robin
In Round Robin Scheduling,

CPU is assigned to the process on the basis of FCFS for a fixed amount of
time. This fixed amount of time is called as time quantum or time slice.
After the time quantum expires, the running process is preempted and sent to
the ready queue.
Then, the processor is assigned to the next arrived process.
It is always preemptive in nature.
Advantages-
It gives the best performance in terms of average response time.
It is best suited for time sharing system, client server architecture and
interactive system.
Disadvantages-
It leads to starvation for processes with larger burst time as they have to repeat
the cycle many times.
Its performance heavily depends on time quantum.
Priorities cannot be set for the processes.
Note-01:

• With decreasing value of time quantum,


• Number of context switch increases
• Response time decreases
• Chances of starvation decreases
• Thus, smaller value of time quantum is better in terms of response time.
Note-02:

• With increasing value of time quantum


• Number of context switch decreases
• Response time increases
• Chances of starvation increases
• Thus, higher value of time quantum is better in terms of number of
context switch.
Note-03:
• With increasing value of time quantum, Round Robin Scheduling tends
to become FCFS Scheduling.
• When time quantum tends to infinity, Round Robin Scheduling
becomes FCFS Scheduling.
Example:
Time quantum = 2 unit

CODE:
1) SJF:

#include<stdio.h> struct process


{ int
WT,AT,BT,TAT; };
struct process a[10];
int main() { int n,temp[10]; int count=0,t=0,short_P; float
total_WT=0, total_TAT=0,Avg_WT,Avg_TAT; printf("Enter the
number of the process\n"); scanf("%d",&n); printf("Enter the
arrival time and burst time of the process\n"); printf("AT WT\n");
for(int i=0;i<n;i++)
{ scanf("%d%d",&a[i].AT,&a[i].BT);
temp[i]=a[i].BT;
}
a[9].BT=10000;
for(t=0;count!=n;t++)
{ short_P=9; for(int
i=0;i<n;i++)
{ if(a[i].BT<a[short_P].BT &&
(a[i].AT<=t && a[i].BT>0))
{
short_P=i;
}

} a[short_P].BT=a[short_P].BT-1;
if(a[short_P].BT==0)
{ count++;
a[short_P].WT=t+1a[short_P].ATtemp[short_P];
a[short_P].TAT=t+1a[short_P].AT;
total_WT=total_WT+a[short_P].WT;
total_TAT=total_TAT+a[short_P].TAT;
}

}
Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;

printf("Id WT TAT\n"); for(int i=0;i<n;i++)


{ printf("%d\t%d\t%d\n",i+1,a[i].WT,a[i].TAT);
} printf("Average waiting time of the process is %f\n",Avg_WT);
printf("Average turn around time of the process %f\n",Avg_TAT);
}
Output:

2) Priority:

#include<stdio.h> struct process


{ int WT,AT,BT,TAT,PT;
}; struct process a[10];

int main() { int n,temp[10],t,count=0,short_p;


float total_WT=0,total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of the process\n");
scanf("%d",&n); printf("Enter the arrival time ,
burst time and priority of the process\n");
printf("AT BT PT\n"); for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PT);
temp[i]=a[i].BT; } a[9].PT=10000;
for(t=0;count!=n;t++)
{ short_p=9; for(int
i=0;i<n;i++)
{ if(a[short_p].PT>a[i].PT && a[i].AT<=t &&
a[i].BT>0) {
short_p=i;
}
} a[short_p].BT=a[short_p].BT-1;
if(a[short_p].BT==0)
{ count++;
a[short_p].WT=t+1a[short_p].ATtemp[short_p];
a[short_p].TAT=t+1a[short_p].AT;
total_WT=total_WT+a[short_p].WT;
total_TAT=total_TAT+a[short_p].TAT; }
}

Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;
printf("ID WT TAT\n"); for(int
i=0;i<n;i++)
{
printf("%d
%d\t%d\n",i+1,a[i].WT,a[i].TAT);
} printf("Average waiting time of the process is
%f\n",Avg_WT); printf("Average turn around time of the
process is %f\n",Avg_TAT); return 0;
}

Output:
3) Round Robin
#include<stdio.h> struct process
{ int
id,AT,BT,WT,TAT; };
struct process a[10]; int
queue[100]; int
front=-1; int rear=-
1; void insert(int n) {
if(front==-1)
front=0;
rear=rear+1;
queue[rear]=n;
}
int delete()
{ int n;
n=queue[front]; front=front+1;
return n;
} int main() { int n,TQ,p,TIME=0; int temp[10],exist[10]={0};
float total_wt=0,total_tat=0,Avg_WT,Avg_TAT; printf("Enter the
number of the process\n"); scanf("%d",&n); printf("Enter the
arrival time and burst time of the process\n"); printf("AT BT\n");
for(int i=0;i<n;i++)
{ scanf("%d%d",&a[i].AT,&a[i].BT); a[i].id=i;
temp[i]=a[i].BT;
} printf("Enter the time quantum\n");
scanf("%d",&TQ);
insert(0); exist[0]=1;
while(front<=rear)
{ p=delete();
if(a[p].BT>=TQ)
{ a[p].BT=a[p].BT-TQ;
TIME=TIME+TQ;
} else
{ TIME=TIME+a[p].BT;
a[p].BT=0;
}
for(int i=0;i<n;i++)
{ if(exist[i]==0 && a[i].AT<=TIME)
{ insert(i);
exist[i]=1;
} }
if(a[p].BT==0)
{ a[p].TAT=TIME-a[p].AT;
a[p].WT=a[p].TAT-temp[p];
total_tat=total_tat+a[p].TAT;
total_wt=total_wt+a[p].WT;
} else {
insert(p); }
}

Avg_TAT=total_tat/n;
Avg_WT=total_wt/n;
printf("ID WT TAT\n"); for(int
i=0;i<n;i++)
{ printf("%d %d
%d\n",a[i].id,a[i].WT,a[i].TAT);
} printf("Average waiting time of the processes is :
%f\n",Avg_WT); printf("Average turn around time of the processes
is : %f\n",Avg_TAT); return 0;
}
Conclusion – Pre-emptive scheduling algorithms were implemented

You might also like