Gantt Chart Round Robin Scheduling for Process arriving at different Time - algorithm

What will be the gantt chart for round robin scheduling with time quantum ?
Click here for, Process Details
Process Arrival Time Burst Time
P1 0 3
P2 1 3
P3 2 3
Time quantum : 1 units
According to me, following should be the gantt chart. Please verify.
Gantt Chart Image
Doubt :
What happens if P1 ( scheduled) and P2 (new Process) arrives at the same Time T. Which of these will be scheduled next ?
eg. P1 is scheduled from Time T0 to T1.
P2 arrives at Time T1.
Now at Time T1 both P1 and P2 is present to be scheduled. Which one will execute next ?
I read that Process is always inserted at the end of Waiting Queue ?
According to these points what should be the correct answer ?
Please help me in understanding the Algorithm.
Thanks

Following gantt chart depicts the process to be allocated to CPU at each time instant.
Gantt Chart
It may be seen that at time instant 1, two processes are available P1 (just allocated to CPU but with remaining burst time) and P2 (just arrived). P2 will be added to the ready queue followed by P1 at the tail. Same explanation holds whenever there is a conflict giving preference to newly arrived process to be added to tail followed by process which has been just allocated to CPU with remaining burst time.

For each process have a specific time period for execution program , which means 1 unit. each process has 3 units of burst time.
At T0 point P1 is available for execution. When it starts at T0 time and it will execute until T1 time (Because each round has 1 unit of time period).
At T2 time , P2 will be available for execution. After that in T2 time , the P2 process will starts execution.When it starts at T2 time and it will execute until T3 time.
At T3 time , P3 will be available for execution.After that in T3 time , the P3 process will starts execution.
After the P3 , it will directly jumped into the next round of execution.
Let's check about waiting time of each process
P1 => 4 Units
P2 => 5 Units
P3 => 6 Units
Average waiting time = (4+5+6)/3 = 5 Units

Related

Non-preemptive scheduing. Shortest job first

The first process is very long but arrives first with nothing else in the queue so that the CPU will process this. I know in nonpreemptive scheduling you cannot interrupt and then resume a process however could I terminate the first process when the shorter process arrives and then restart the process all over again to reduce wait time?
For example
P1=-50 burst time arrival time -0
P2 -5 burst time arrival time -10
P3 -10 burst time arrival time -20
Could I terminate P1, start P2 and then P3, and then restart the whole of P1 to reduce wait time?
I have tried looking online and I can't seem to understand the answer

How to test banker algorithm and show other ordering has problem

I have found a Python version of the banker algorithm on GeeksForGeeks site here.
However, how to test and show that the safe ordering is correct?
And how to show that other orderings have an error or problem with an example?
https://www.geeksforgeeks.org/bankers-algorithm-in-operating-system-2/
Introduction
Let's consider a very simple example. Let's say there are 2 processes - P0 and P1, and there's only one type of resource A. The system allocates 10 units of A to P0 and 0 to P1, and it still has 1 unit of A left. Moreover, in total , P0 may request up to 11 units during the execution, and P1 - 5.
Let's quickly build up tables and vectors used to determine safe or unsafe sequences for these processes.
Allocation table
Allocation table shows how many resources of each type are allocated to processes. In your example, it looks as follows:
Process
A
P0
10
P1
0
Availability vector
Availability vector shows how many units the system can still offer if it decides so.
A
1
Maximum table
Maximum table shows how many units of A each process may request during the execution (in total).
Process
A
P0
11
P1
5
Need table
Need table shows how many units of A each process may additionally request during the execution
Process
A
P0
1
P1
5
Safe sequence
Now, let's say we ran the Banker's algorithm for our configuration and got the following sequence:
P0 -> P1
Why is it safe?
Case 1 - processes are executed in sequence
P0 starts executing, and demands and receives the remaining 1 unit. So, the system has 0 available resources left. However, once P0 completes, it releases 11 units of A, and it's more than enough to run P1 and for it to complete.
Case 2 - processes are executed in parallel
P0 starts executing, and demands and receives the remaining 1 unit. Then, during its execution, P1 starts too and asks for 5 units. However, its request gets postponed because the system has none. So, the request is put on a waiting list. Later, when P0 releases at least 5 units, P1 finally gets 5. Obviously, no deadlock can happen because if P0 needs resources again, it will either wait for P1 or just ask the system and vice versa.
Unsafe sequence
P1 -> P0
P1 starts executing and demands 5 units from the system. It gets denied and its request is put on a waiting list because the system has only 1 unit. Then, P0 starts and demands 1 unit. It also gets denied because P1 is waiting for 5 units already. The request from P0 is put on the waiting list too. So, we have a deadlock situation because neither of the requests can ever go through.

How to schedule processes in FCFS algorithm using arrival time?

Here is my definition of FCFS (First Come First Serve - CPU Scheduling algorithm):
Process CPU Burst Arrival Time
p1 4 0
p2 5 1
p3 6 2
p4 5 1
p5 4 0
And the sequence of this example is as below
So my question is that in second turn why it doesn't take p5 instead of p4 as its arrival time is also 0?
FCFS is implemented through Queue data structure. So it all depends on the position of processes in the FCFS queue, based on which short term scheduler will select process for execution.
Since arrival time of p5 is less than p4, it will definitely be ahead of p4 in the queue and therefore, it must be executed first. The Gantt Chart you have drawn is wrong.
One of the correct sequence could be:
p1 , p5 , p2 , p4 , p3

Understanding the Shortest Job First Algorithm (Non-preemptive)

The shortest job first algorithm is shown in the following image:
If it is shortest job first/shortest process next, shouldn't the order be:
P1 → P5 → P3 → P4 → P2 ? Since that's the order of lowest to highest service times.
Why does process 2 come second?
I know if we use burst times instead, that would be the order, but I have no idea what the differences between service time and burst times are.
Any help would be much appreciated explaining that graphic.
The image in the question follows the correct order which is:
P1 → P2 → P5 → P3 → P4
Explanation:
P1 is arrived at time = 0 , so it will be executed first. Service Time of this process is 3. So this process is completed at time=3.
At time=3, there is only one process that is arrived which is P2. All other processes arrive later. So this process is now executed. Service time of this process is 6, so this process is completed at time=3+6=9.
Now at time=9, there are three processes which are P3, P4 and P5 (which arrived at time= 4, 6 and 8 respectively). Since the service time of P5 is 2 which is minimum as compared to that of P3 and P4, so P5 is now executed and it gets completed at time=9+2=11.
At time=11, we have two processes which are P3 and P4 (which are arrived at time= 4 and 6 respectively). Since the service time of P3 is 4 which is less as compared to that of P4, so P4 is executed now and it gets completed at time= 11+4=15
At time=15, we have only one process which is P4. So it is executed now. Since service time of this process is 5, so it gets completed at time = 15+5 = 20

Average waiting time in Round Robin scheduling

Waiting time is defined as how long each process has to wait before it gets it's time slice.
In scheduling algorithms such as Shorted Job First and First Come First Serve, we can find that waiting time easily when we just queue up the jobs and see how long each one had to wait before it got serviced.
When it comes to Round Robin or any other preemptive algorithms, we find that long running jobs spend a little time in CPU, when they are preempted and then wait for sometime for it's turn to execute and at some point in it's turn, it executes till completion. I wanted to findout the best way to understand 'waiting time' of the jobs in such a scheduling algorithm.
I found a formula which gives waiting time as:
Waiting Time = (Final Start Time - Previous Time in CPU - Arrival Time)
But I fail to understand the reasoning for this formula. For e.g. Consider a job A which has a burst time of 30 units and round-robin happens at every 5 units. There are two more jobs B(10) and C(15).
The order in which these will be serviced would be:
0 A 5 B 10 C 15 A 20 B 25 C 30 A 35 C 40 A 45 A 50 A 55
Waiting time for A = 40 - 5 - 0
I choose 40 because, after 40 A never waits. It just gets its time slices and goes on and on.
Choose 5 because A spent in process previouly between 30 and 35.
0 is the start time.
Well, I have a doubt in this formula as why was 15 A 20 is not accounted for?
Intuitively, I unable to get how this is getting us the waiting time for A, when we are just accounting for the penultimate execution only and then subtracting the arrival time.
According to me, the waiting time for A should be:
Final Start time - (sum of all times it spend in the processing).
If this formula is wrong, why is it?
Please help clarify my understanding of this concept.
You've misunderstood what the formula means by "previous time in CPU". This actually means the same thing as what you call "sum of all times it spend in the processing". (I guess "previous time in CPU" is supposed to be short for "total time previously spent running on the CPU", where "previously" means "before the final start".)
You still need to subtract the arrival time because the process obviously wasn't waiting before it arrived. (Just in case this is unclear: The "arrival time" is the time when the job was submitted to the scheduler.) In your example, the arrival time for all processes is 0, so this doesn't make a difference there, but in the general case, the arrival time needs to be taken into account.
Edit: If you look at the example on the webpage you linked to, process P1 takes two time slices of four time units each before its final start, and its "previous time in CPU" is calculated as 8, consistent with the interpretation above.
Last waiting
value-(time quantum×(n-1))
Here n denotes the no of times a process arrives in the gantt chart.

Resources