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:-
= (9-2) - 5 = 2 So we have to wait for the 2 hours .
You can easily understand by Following code .
Output:-
Recommended post:-
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