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

Process States

Uploaded by

kesariapna123
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)
21 views11 pages

Process States

Uploaded by

kesariapna123
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

Process States

• A Process is a program which is currently in execution.


• A process changes its state during its execution. Each process may be in any
one of the following states: new, ready, running, waiting and terminated.

Note that only one


process will be running on
a processor at any instant.
Other processes will be in
ready & waiting states.

• New: A new process is being created.


• Ready: The process is waiting to be assigned to a processor.
• Running: Instructions are being executed.
• Waiting: The process is waiting for some event to occur (such as an I/O
operation or reception of a signal).
• Terminated: The process has finished execution.
1. First Come First Served (FCFS): The process that requests the CPU
first is allocated the CPU first. Jobs are executed on first come, first
serve basis.
• When a process P1 enters the queue; It’s PCB is linked to the tail of the
queue. When the CPU is free, it is allocated to the process, which is at the
head of the queue.

• It is a non-preemptive algorithm.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
• The average waiting time is quite long.
For eg., The processes. P1. P2. P3 with burst time:
24, 3 and 3 respectively are given.
In a Gantt chart,

• Waiting time for P1= 0; P2=24; P3=27


• Average Waiting time: (0 + 24 + 27)/3 = 17

• Turnaround time for P1= 24; P2=27; P3=30


• Average Turnaround time: (24 + 27+30)/3 = 81/3 = 27.

Drawbacks:
i) Long waiting time.
ii) Convoy effect – All the processes waiting for one big process to get off
the CPU. This results in low CPU utilisation and low device utilization.
Write C program to simulate the following CPU scheduling algorithms: FCFS

#include<stdio.h> tt[i+1]=tt[i]+bt[i+1];
void main() w1=w1+wt[i];
{ t1=t1+tt[i];
int i,j,bt[10],n,wt[10],tt[10]; }
float w1=0,t1=0,awt,att; awt=w1/n; //calculating Avg. Waiting Time
printf("Enter the no. of Processes:\n"); att=t1/n; //calculating Avg. Turn-around Time
scanf("%d",&n); printf("\nBT\t WT\t TT\n");
printf("Enter the CPU Burst-time of Processes: :\n "); for(i=0;i<n;i++)
for(i=0;i<n;i++) printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
scanf("%d",&bt[i]); printf(“Avg. Waiting time=%f\n Avg. Turn-around time=%f\n",awt,att);
for(i=0;i<n;i++)
}
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
OUTPUT:
Enter the no. of Processes:
3
Enter the CPU Burst-time of Processes:
10
8
12
BT WT TT
10 0 10
8 10 18
12 18 30
Avg. Waiting time =9.3333333
Avg. Turn-around time =19. 3333334
2. Shortest-Job-First (SJF) Scheduling: When the CPU is available,
it is allocated to the process with the smallest CPU burst time.
SJF can be either non-preemptive or pre-emptive(SRTF). SJF is optimal,
i.e., gives min’ average waiting time for a given set of Processes.

Order of Execution <P4, P1,P3,P2>

•Waiting time for P1 = 3; P2 = 16; P3 = 9, P4 = 0


•Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
•Turn-around time for P1=9, P2=24, P3=16, P4=3
•Average turn-around time = (9+24+16+3)/4 = 13
Write C program to simulate the following CPU scheduling algorithms: SJF

#include<stdio.h>
void main() for(i=0;i<n;i++)
{ { wt[0]=0;
int i,j,bt[10],t,n,wt[10],tt[10],w1=0,t1=0; tt[0]=bt[0];
float awt,at; wt[i+1]=bt[i]+wt[i];
printf("Enter no. of processes:\n"); tt[i+1]=tt[i]+bt[i+1];
scanf("%d",&n); w1=w1+wt[i];
printf("Enter the burst time of processes:\n"); t1=t1+tt[i];
for(i=0;i<n;i++) }
scanf("%d",&bt[i]); awt=w1/n;
for(i=0;i<n;i++) at=t1/n;
{ printf("\nbt\t wt\t tt\n");
for(j=i;j<n;j++) for(i=0;i<n;i++)
if(bt[i]>bt[j]) //Comparing the burst-time of processes and choosing the process printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
with shortest burst time.
printf("awt=%f\nat=%f\n",awt,aTt);
{
}
t=bt[i]; bt[i]=bt[j]; bt[j]=t;
}
}
printf("Order of Execution(SJFS):");
for(i=0;i<n;i++)
printf("%d,",bt[i]);
OUTPUT:

Enter the no. of Processes:


3
Enter the CPU Burst-time of Processes: :
10
4
6

BT WT TT
10 0 10
4 10 14
6 14 20

Avg. Waiting time=8.000000


Avg. Turn-around time=14.666667
3. Priority Scheduling: This algorithm associates each process with a
priority and the process with highest priority will get the CPU first.
• If there are two processes with the same priority, then FCFS scheduling
is used to break the tie.
• Priorities are generally some fixed range of numbers, such as 0 to 7 or 0
to 4095.
• Depending on the O/S, we represent the high priority number, either as
lowest number or highest number.
• Here. Let us assume lowest number represents the high priority.
• Priority scheduling can be either Preemptive or Non-preemptive.
• Priorities can be defined either internally or externally.

• Problem: Indefinite blocking or Starvation – low priority processes may


never get a chance to execute.
• In a heavily loaded system, a steady stream of higher-priority processes
can prevent a low-priority process from getting the CPU permanently,
this is known Starvation.
• Solution: Aging is the technique of gradually increasing the priority of
processes that wait in the ready queue for a long time.
Consider the following set of processes, assumed to have arrived at
time 0 in the order P1, P2, P3, P4 and P5.

Order of Execution <P2, P5,P1,P3, P4>

Using priority scheduling, we would schedule these processes according


to the following Gantt chart:

•Average waiting time =(6+0+16+18+1) / 5 = 8.2


•Turn-around time for P1=16, P2=1, P3=18, P4=19
•Average turn-around time = (16+1+18+19)/4 = 13.5
Write C program to simulate the following CPU scheduling algorithms: Priority Scheduling
#include<stdio.h> wt[0]=0; tt[0]=bt[0]; wt[i+1]=bt[i]+wt[i];
void main() tt[i+1]=tt[i]+bt[i+1]; w1=w1+wt[i]; t1=t1+tt[i];
{ awt=w1/n; at=t1/n; }
int i,j,pno[10],prior[10],bt[10],n,wt[10],tt[10],w1=0,t1=0,s; printf(" \n job \t bt \t wt \t tat \t prior\n");
float awt,at; for(i=0;i<n;i++)
printf("Enter the no. of Processes: "); printf("%d \t \t %d\t \t %d\t\t %d\t\t
scanf("%d",&n); %d\n",pno[i],bt[i],wt[i],tt[i],prior[i]);
for(i=0;i<n;i++) printf("awt=%f \t at=%f \n",awt,at);
{ }
printf("Enter the Burst-time of Process %d: ",i+1);
scanf("%d",&bt[i]); OUTPUT:
printf("Enter the Priority of Process %d: ",i+1); //Assign priorities Enter the no. of Processes: 3
scanf("%d",&prior[i]); Enter the Burst-time of Process 1: 12
pno[i]=i+1; Enter the Priority of Process 1: 3
} Enter the Burst-time of Process 2: 21
for(i=0;i<n;i++) Enter the Priority of Process 2: 5
{ for(j=0;j<n;j++) Enter the Burst-time of Process 3: 12
{ if(prior[i]<prior[j]) //Compare priorities & decide order of execution Enter the Priority of Process 3:4
{ job bt wt tat prior
s=prior[i]; prior[i]=prior[j]; prior[j]=s; 1 12 0 12 3
s=bt[i]; bt[i]=bt[j]; bt[j]=s; s=pno[i]; 3 12 12 24 4
pno[i]=pno[j]; pno[j]=s; } } } 2 21 24 45 5
for(i=0;i<n;i++) awt=12.000000 at=27.000000
{

You might also like