C program of the priority scheduling (Preemptive ) algorithm in operating system (OS)
In this post I am going to explain you all things related to the priority scheduling . What is priority scheduling , what is the characteristics of the priority scheduling , what is the drawbacks of the this algorithm . all that points.
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.
Drawbacks:-
- 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:-
= (time spend in the bank ) – (Total work time)
= (9-2) – 5 = 2 So we have to wait for the 2 hours .
You can easily understand by Following code .
Output:-
Recommended post:-
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Data structure:-
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
Key points:-