Shortest Job First (or SJF) CPU Scheduling Python Program
In CPUs the scheduling methods chooses the sequence in which the processes will be executed hence managing the waiting time. One of such methods is known as the "shortest job first" (SJF) or "shortest job next". This algorithm gives the process the least execution time to run first. Another method known as Shortest Job Next (SJN), often used as SJN, can also be used.
Characteristics of SJF Scheduling:
Shortest Job First can be implemented using a Greedy Algorithm.
This algorithm's plus point is that it has the lowest average waiting period of all the available CPU scheduling algorithms.
If, however, there is a situation where the processes with shorter execution times continue to be produced, it can cause starvation. This particular issue can be resolved by implementing the idea of aging.
Since the operating system could not be aware of burst timings, it is almost impossible to categorize them. Although the complete execution time of the processes cannot be predicted, it may be determined using various techniques, such as calculating the weighted-average of previous execution periods.
SJF can be employed in specialized settings when precise running time estimations are available.
Turn Around Time = Completion Time of any particular Process - Arrival Time of any particular Process.
Waiting Time = Turn Around Time of the Process - Burst Time of the Process
Segment Trees are a data structure that may be used to construct the non-preemptive types Shortest Job First method.
There is an assumption to be kept in mind before proceeding. The arrival time is considered to be zero, which means that the turnaround and completion times of processes will be the same.
Implementing SJF Algorithm in Python
Enter the number of processes: 10 Enter the Burst Time of the processes: P1: 4 P2: 3 P3: 7 P4: 9 P5: 1 P6: 10 P7: 14 P8: 11 P9: 5 P10: 16 P B_T W_T TA_T P5 1 0 1 P2 3 1 4 P1 4 4 8 P9 5 8 13 P3 7 13 20 P4 9 20 29 P6 10 29 39 P8 11 39 50 P7 14 50 64 P10 16 64 80 The average Waiting Time for the whole sequence of processes is = 22.8 The average Turn Around Time of the processes is = 30.8
Time Complexity: The time complexity of this SJF algorithm is O(n^2). The time complexity is non-linear because we run a nested loop to sort the burst time and process ids.
Auxiliary Space: This approach takes O(n) extra memory space. We are storing arrays of the process.
Advantages of Shortest Job First Algorithm:
Disadvantages of Shortest Job First Algorithm:
Shortest Remaining Time First (SRTF)
The preemptive version of the Shortest Job First (SJF) algorithm is called Shortest Remaining Time First (SRTF). This algorithm allocates the CPU to the task that is nearly finished.
This technique cannot be used in an interactive system since it needs a sophisticated understanding of how much CPU time is needed to complete the task. However, the SRT method is utilized in batch systems when it is essential to prioritize small jobs.
However, SRT has additional expenses than SJN since it necessitates regular OS context switching and needs to monitor the CPU time of the processes in the queue.
The SRT method executes more quickly than the SJF algorithm for the same collection of processes. However, in this case, the operational costs, or the time needed to execute context switching, have been disregarded. All of its processing data of the tasks that are considered preemptive must be stored in its corresponding PCB for whenever it is to be resumed, and the data of the other process's PCB, that is, the process to which the Operating System is switching, are written into what is commonly called as memory registers. This is the process of Context Switching.
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week