Professional Documents
Culture Documents
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.
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.
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-
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
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:
CODE:
1) SJF:
} 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;
2) Priority:
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