Uploaded by zamanarooba

LAB-CPU Scheduling (1)

advertisement
PRACTICE LAB-07: CPU Scheduling
Topic to be covered
● CPU Scheduling Algorithms
○ FCFS
○ SJF
○ SRTF
○ ROUND ROBIN
○ Priority Scheduling Algorithm
Objectives
● Students will be able to understand working of CPU Scheduling algorithms and
implementation in C language.
Note:
We are given with the n number of processes i.e. P1, P2, P3,.......,Pn and their corresponding
burst times. The task is to find the average waiting time and average turnaround time using
FCFS CPU Scheduling algorithm.
What is Waiting Time and Turnaround Time?
•
•
Turnaround Time is the time interval between the submission of a process and its
completion.
Turnaround Time = completion of a process – submission of a process
Waiting Time is the difference between turnaround time and burst time
Waiting Time = turnaround time – burst time
1. FCFS:
a. What is FCFS Scheduling?
First Come, First Served (FCFS) also known as First In, First Out(FIFO) is the CPU
scheduling algorithm in which the CPU is allocated to the processes in the order they
are queued in the ready queue.
Algorithm:
Start
Step 1-> In function int waitingtime(int proc[], int n, int burst_time[], int wait_time[])
Set wait_time[0] = 0
Loop For i = 1 and i < n and i++
Set wait_time[i] = burst_time[i-1] + wait_time[i-1]
End For
Step 2-> In function int turnaroundtime( int proc[], int n, int burst_time[], int wait_time[], int
tat[])
Loop For i = 0 and i < n and i++
Set tat[i] = burst_time[i] + wait_time[i]
End For
Step 3-> In function int avgtime( int proc[], int n, int burst_time[])
Declare and initialize wait_time[n], tat[n], total_wt = 0, total_tat = 0;
Call waitingtime(proc, n, burst_time, wait_time)
Call turnaroundtime(proc, n, burst_time, wait_time, tat)
Loop For i=0 and i<n and i++
Set total_wt = total_wt + wait_time[i]
Set total_tat = total_tat + tat[i]
Print process number, burstime wait time and turnaround time
End For
Print "Average waiting time =i.e. total_wt / n
Print "Average turn around time = i.e. total_tat / n
Step 4-> In int main()
Declare the input int proc[] = { 1, 2, 3}
Declare and initialize n = sizeof proc / sizeof proc[0]
Declare and initialize burst_time[] = {10, 5, 8}
Call avgtime(proc, n, burst_time)
Stop
Implementation in C:
See program in codes folder: FCFS.c
Sample Output:
Task01:
a. Understand the algorithm and implementation and run with different inputs.
b. Verify the output by solving the algorithm on page. Submit the solved example.
Is the code working correctly?
c. Advantages and Disadvantages of FCFS?
2. Shortest Job First (SJF)
a. This is also known as Shortest Job Next, or SJN
b. This is a non-preemptive, pre-emptive scheduling algorithm.
c. Best approach to minimize waiting time.
d. Easy to implement in Batch systems where required CPU time is known in
advance.
e. Impossible to implement in interactive systems where required CPU time is not
known.
f. The processer should know in advance how much time process will take.
g. Working og SJF
Algorithm:
Start
Step 1-> Declare a struct Process
Declare pid, bt, art
Step 2-> In function findTurnAroundTime(Process proc[], int n, int wt[], int tat[])
Loop For i = 0 and i < n and i++
Set tat[i] = proc[i].bt + wt[i]
Step 3-> In function findWaitingTime(Process proc[], int n, int wt[])
Declare rt[n]
Loop For i = 0 and i < n and i++
Set rt[i] = proc[i].bt
Set complete = 0, t = 0, minm = INT_MAX
Set shortest = 0, finish_time
Set bool check = false
Loop While (complete != n)
Loop For j = 0 and j < n and j++
If (proc[j].art <= t) && (rt[j] < minm) && rt[j] > 0 then,
Set minm = rt[j]
Set shortest = j
Set check = true
If check == false then,
Increment t by 1
Continue
Decrement the value of rt[shortest] by 1
Set minm = rt[shortest]
If minm == 0 then,
Set minm = INT_MAX
If rt[shortest] == 0 then,
Increment complete by 1
Set check = false
Set finish_time = t + 1
Set wt[shortest] = finish_time - proc[shortest].bt -proc[shortest].art
If wt[shortest] < 0
Set wt[shortest] = 0
Increment t by 1
Step 4-> In function findavgTime(Process proc[], int n)
Declare and set wt[n], tat[n], total_wt = 0, total_tat = 0
Call findWaitingTime(proc, n, wt)
Call findTurnAroundTime(proc, n, wt, tat)
Loop For i = 0 and i < n and i++
Set total_wt = total_wt + wt[i]
Set total_tat = total_tat + tat[i]
Print proc[i].pid, proc[i].bt, wt[i], tat[i]
Print Average waiting time i.e., total_wt / n
Print Average turn around time i.e., total_tat / n
Step 5-> In function int main()
Declare and set Process proc[] = { { 1, 5, 1 }, { 2, 3, 1 }, { 3, 6, 2 }, { 4, 5, 3 } }
Set n = sizeof(proc) / sizeof(proc[0])
Call findavgTime(proc, n)
Stop
Implementation in C:
See the program in codes folder: SJF.c
Task02:
a. Understand the algorithm and implementation and run with different inputs.
b. Verify the output by solving the algorithm on page. Submit the solved example.
Is the code working correctly?
c. Advantages and Disadvantages of SJF?
3. Shortest Remaining Time First (SRTF)
It is pre-emptive version of SJF.
Explanation:
• At the 0th unit of the CPU, there is only one process that is P1, so P1 gets executed for
the 1 time unit.
• At the 1st unit of the CPU, Process P2 arrives. Now, the P1 needs 6 more units more
to be executed, and the P2 needs only 3 units. So, P2 is executed first by
preempting P1.
• At the 3rd unit of time, the process P3 arrives, and the burst time of P3 is 4 units
which is more than the completion time of P2 that is 1 unit, so P2 continues its
execution.
• Now after the completion of P2, the burst time of P3 is 4 units that means it needs
only 4 units for completion while P1 needs 6 units for completion.
• So, this algorithm picks P3 above P1 due to the reason that the completion time of
P3 is less than that of P1
• P3 gets completed at time unit 8, there are no new processes arrived.
• So again, P1 is sent for execution, and it gets completed at the 14th unit.
As Arrival Time and Burst time for three processes P1, P2, P3 are given in the above
diagram. Let us calculate Turn around time, completion time, and waiting time.
Algorithm:
Approach:
• Traverse until all process gets completely executed.
• Find process with minimum remaining time at every single time lap.
• Reduce its time by 1.
• Check if its remaining time becomes 0
• Increment the counter of process completion.
• Completion time of current process = current_time + 1;
• Calculate waiting time for each completed process.
•
wt[i]= Completion time – arrival_time-burst_time
• Increment time lap by one.
• Find turnaround time (waiting_time + burst_time).
Implementation in C:
See the program in codes folder: SRTF.cpp
Task03:
a. Understand the algorithm and implementation, convert it to C language and run
with different inputs.
b. Verify the output by solving the algorithm on page. Submit the solved example.
Is the code working correctly?
c. Advantages and Disadvantages of SJF?
4. Round Robin (RR)
Algorithm
Step 1: Organize all processes according to their arrival time in the ready queue. The queue
structure of the ready queue is based on the FIFO structure to execute all CPU processes.
Step 2: Now, we push the first process from the ready queue to execute its task for a fixed
time, allocated by each process that arrives in the queue.
Step 3: If the process cannot complete their task within defined time interval or slots because
it is stopped by another process that pushes from the ready queue to execute their task due to
arrival time of the next process is reached. Therefore, CPU saved the previous state of the
process, which helps to resume from the point where it is interrupted. (If the burst time of the
process is left, push the process end of the ready queue).
Step 4: Similarly, the scheduler selects another process from the ready queue to execute its
tasks. When a process finishes its task within time slots, the process will not go for further
execution because the process's burst time is finished.
Step 5: Similarly, we repeat all the steps to execute the process until the work has finished.
Example: Time Quantum=6
Implementation in C:
See the program in codes folder: RR.cpp
Task04:
a. Understand the algorithm and implementation and run with different inputs.
b. Verify the output by solving the algorithm on page. Submit the solved example.
Is the code working correctly?
c. Advantages and Disadvantages of RR?
Download