Hey, what's up guys, Tanmay here for Simple Snippets and I'm back with another video tutorial on CPU scheduling algorithms. So in this tutorial series, we'll be looking into the different CPU scheduling algorithms we'll be particularly looking into the first come first serve CPU scheduling algorithm also known as FCFS. So basically CPU scheduling algorithms define how a CPU will schedule and process different jobs and processes.
So we won't be getting into a lot of theory. Let's straight away jump into the numerical. So as you can see on the screen, we've been given a table with certain quantities.
So let me just tell you what all details have been given in the question. We have been given with the processes. So, we have four different processes P0, P1, P2, P3. We've been given the arrival times of each of these processes and the burst time. So, the arrival time is the time at which the process entered the queue.
So, for P0 it is 0, for P1 it is 1 and in this case the quantity or the value is in milliseconds. The unit of measure is milliseconds. Now, the burst time is the time required by a particular process to complete itself.
So, when a CPU will start processing P0, it will will require 5 milliseconds to complete that process. Now the remaining three quantities are supposed to be calculated by us. So we have to calculate completion time, turnaround time and waiting time. So in short completion time is the time when the process is finished by the CPU. The turnaround time is the time for which the process was inside the queue.
So the total time spent by a process inside the queue and the waiting time is the time when the process was inside the queue but did not get CPU time or the CPU was processing some other process. So you'll understand that more clearly when we actually go ahead and draw the GAN chart and the GAN chart is sort of like a timeline for when the process came into the queue and when the process completed itself. So since this is a first come first serve algorithm in this case the CPU is assigned to the process that comes first. So P0 came in at time 0 so it came first right so we just have to write down p0 over here and we know that it requires 5 milliseconds to complete so if the timeline started at 0th second we add plus 5 to that and this is how p0 is going to look like on the GAN chart and the CPU is going to process p0 from 0 to 5 seconds.
Now if you see the arrival times of each of these processes at time t equal to 0 only p0 had come inside the queue but at time t equal to 5 by the time process p0 was completely processed by the CPU. all these other processes have come inside the queue right because the arrival times is less than 5. So at time t equal to 5, p1, p2 and p3 all came in this respective order that is p1 came first then after that p2 and p3 but they all were waiting because p0 was still being processed till fifth second. Now since it is a FCFS scheduling algorithm, the process that came first and was waiting inside the queue will be processed next after p0.
So we know after p0, p1 came because the arrival time is 1. so we have to write p1 now we know it requires 3 milliseconds so 5 plus 3 is going to be 8 and this is how p1 is going to be finished by the cpu after p1 we know p2 came into the queue at time equal to 2 so we'll write p2 it requires 8 milliseconds so 8 plus 8 is going to be 16 and this is when cpu will finish process p2 and last process is going to be P3 it requires 6 milliseconds according to the burst time and it will finish processing at 16 plus 6 which is going to be 22 milliseconds. So this is the entire Gantt chart and using this Gantt chart we will now find the completion time. So completion time is the time at which the process was finished by the CPU.
So for P0 we know it finished at 5th second. So I'll just write it down as it is. For P1 the process P1 finished actually at 8th second according to the Gantt chart. So we have to write down 8 over here.
For P2 it is going to be 16 and for p3 it is going to be 22. So up until now it was pretty straightforward. We got all those values just by looking at the Gantt chart. The turnaround time, we have a formula for that.
That is CT, that is completion time minus the arrival time, 80. So we have 5 minus 0, which is 5, 8 minus 1, which is 7, 16 minus 2, that is 14 and 22 minus 3, which is 19. Now, if you don't know this formula, even then you can make out the turnaround time because you know turnaround time is the time spent by a process inside the queue. So just by looking at this process, you know that it arrived at at 0th second and it got completed at 5th second. So the total time spent inside the queue is from 0 to 5 that is equal to 5. For P1, you know that it arrived at 1 second but it completed at 8 seconds.
So 8-1 is going to be 7. 7 seconds is the time it spent inside the queue. Similarly for P2 and P3 also. So you don't really need to learn this formula. Now for waiting time, we again have the formula that is turnaround time minus burst time. So 5-5 is going to be 0. 7-3 is going to be 4. 14-8 is going to be 6. and 19 minus 6 is going to be 13. Another way to calculate waiting time is you can see when it came So for example P0 came in at 0 time and as soon as it came the processor started processing it.
So it didn't have to wait right. So you can see the waiting time is going to be 0. For P1 you can see that it came at arrival time at 1 second but it actually started its process or CPU actually started processing P1 at 5th second. So 5-1 is equal to 4. So this is how you get waiting time. For P2 also you can see that it arrived at time 2 but it actually started at 8th second. That is CPU actually started processing P2 at 8th second.
So 8- 2 is going to be 6 and lastly for p3 it came in at third second but it actually started processing at 16 seconds so 16-3 is again going to be 13. So you can directly make out waiting time from this and you don't really need to buy hard this formula. So now we have all the individual values. So lastly in the question we can actually find out the total turnaround time.
So we have total TAT which is going to be the sum of these turnaround times so 5 plus 7 plus 14 plus 19 which is going to be 45 milliseconds. and we can also calculate average turnaround time which is going to be total TAT that is total turnaround time divided by the number of processes that is 4 which is going to be 45 divided by 4 which is finally going to be 11.25 So this is one thing that can be asked in the question that is total turnaround time and average turnaround time. Another thing that they can ask is total waiting time which is going to be total of these waiting times that is 0 plus 4 plus 6 plus 13 which is going to be 23 milliseconds and again average waiting time which is going to be 23 divided by 4 that is 4 is the total number of processes which is going to be 5.75 milliseconds.
So these are the four different parameters that you might be asked in a numerical which is based on these type of algorithms and yeah this is pretty much it so when you tackle such kind of questions you first calculate the individual parameters completion time turnaround time waiting time using the Gantt chart and then you can calculate the total and average of these different parameters so this is how you solve FCFS scheduling algorithm so that's it for this video guys I hope you understood the FCFS CPU scheduling algorithm If you like this video, give it a thumbs up, share it with your friends and don't forget to subscribe to our channel. Peace!