So let's start.......
What is the priority scheduling:-
As it is clear with the name that this scheduling is based on the priority of the processes. The process which have the higher priority will get the CPU first. whereas jobs with equal priorities are carried out on a round-robin or FCFS basis.
This scheduling is of two types:-
1. Non preemptive
2. Preemptive
characteristics:-
- It schedules the process based on the priority of the processes.
- Lower the number higher the priority.
- If the two or more processes have the same priority then we schedules on the basis of FCFS.
- Major problem with priority scheduling is problem of starvation.
- Solution of the problem of the starvation is aging ,where aging is a technique of gradually increasing the priority of the processes that wait in the system from long time.
- Major problem with priority scheduling is problem of starvation.
- Solution of the problem of the starvation is aging ,where aging is a technique of gradually increasing the priority of the processes that wait in the system from long time.
Code:-
logic:-.
- First we copy the burst time of the process in a new array temp[] because in the further calculation we will be going to decrease the Burst time of the process but we will have to need the real burst time of the process in the calculation of the waiting time .(If you confused then don't worry you will be able understand after going through code)
- we initialize the priority of a process with the maximum (you can take any maximum value). and we will use 9th process because we assumed that there will not be more than 10 process but you can use any number.
- In this code we are going to use a loop which executed until all the processes are completed. for checking how many processes are completed we use count .Initially it's value is 0 (i.e no processes are completed yet).
- In each cycle we will find the process which have highest priority(lowest priority number like 1 have high priority than 2) and arrived at time t and burst time of the process is not equal to zero.
- After doing this we will decrease the burst time of the process by 1 in each cycle of the time.
- And if the process will be complete (Burst time =0) then we will increase the value of the count by 1 (i.e one process is completed)
- For calculating the waiting time we will use a formula (WT= time- arrival-Burst time) let's understand by an example :- Lets say we have any work in the bank for 5 hours . and we go at the 2 pm and we will come at 9 pm from the bank then waiting time in the bank is:-
= (9-2) - 5 = 2 So we have to wait for the 2 hours .
8.For calculating turn around time we simply use TAT= completion time - arrival time.
You can easily understand by Following code .
#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);
// copying the burst time in
// a temp array fot futher use
temp[i]=a[i].BT;
}
// we initialize the burst time
// of a process with maximum
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 any process is completed
if(a[short_p].BT==0)
{
// one process is completed
// so count increases by 1
count++;
a[short_p].WT=t+1-a[short_p].AT-temp[short_p];
a[short_p].TAT=t+1-a[short_p].AT;
// total calculation
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;
// printing of the answer
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("Avg waiting time of the process is %f\n",Avg_WT);
printf("Avg turn around time of the process is %f\n",Avg_TAT);
return 0;
}
Output:-
Enter the number of the process
3
Enter the arrival time , burst time and priority of the process
AT BT PT
0 3 3
1 5 1
2 2 2
ID WT TAT
1 7 10
2 0 5
3 4 6
Avg waiting time of the process is 3.666667
Avg turn around time of the process is 7.000000
Recommended post:-
codechef problems:-
Wipro :-
- Update the booking ID | Wipro previous year question paper solution
- Pages in PDF
- Find the location id
- Find the odd digits
- Find the Product ID
Infytq :-
Key Points;-
Hackerrank:-
- Python : missing characters : hackerrank solution
- Python : string transformation | Hackerrank solution
- Active Traders certification test problem | Hackerrank Solution
- Usernames changes certification test problem | Hackerrank Solution
- string Representation of objects certification test hackerrank solution
- Average Function | hackerrank certification problem solution
C-tutorial:-
- Micros in C
- Pointer in c
- Function declaration
- Types of user define function
- return type of function
- 2D array
- c program to convert specified days into years weeks and days
- Print Reverse Hollow Pyramid
- Update the booking ID | Wipro previous year question paper
- Pages in PDF | Wipro previous year question paper
- Sparse Matrix in data structure
- Find the location ID | Wipro previous year Coding question
- find the odd digits | Wipro Coding question
- Find the product id | Wipro Coding question
- Difference between static and dynamic memory allocation
- What is asymptotic Notation
0 Comments