C program on the SJF(Shortest job first) preemptive algorithm in os
Here we are going discuss all things about the SJF scheduling algorithm i.e what is SJF , what are the advantage of SJF , what is disadvantage of SJF ,Code of SJF.
What is SJF(Shortest job First) scheduling:-
As it is clear by the name of this scheduling algorithm the job which have the less burst time will get the CPU first .it is the best method to minimize the waiting time .it is of two type
2. non preemptive
Characteristics:-
- Sjf scheduling can be either preemptive or non-preemptive.
- IN SJF CPU is assigned to the process that has the smallest next CPU Burst time.
- If the next CPU Burst of two process is the same then FCFS scheduling is used to break the tie.
- This process give the minimum average waiting time for a given processes.
Code:-
In this code first we are creating the structure for the process in which we are declaring the waiting time , Arrival time , Burst time ,Priority and turn around time .then after an array of the structure type.
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 burst time 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 shortest burst time and arrive 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:-
- 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:-
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
Must check this:-