JOB SCHEDULING ALGORITHMS (FCFS, SJF, ROUND ROBIN, PRIORITY BASED) PYTHON

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
subject 18 cs2017 operating systems lab equipment number four cpu scheduling algorithms so uh the first one here we see is first come first algorithm so uh we'll start from the function call below this is the function call and now we have a parameter that is burst times which is nothing but the uh it's nothing but a list of all the birth stamps for each process so first time nothing but the time for which a process resides in the cpu right so uh we have a list of all those stamps for each process and now we are passing that one to the function above so this is the function fine now we are printing a heading called process burst time waiting time and also turnaround time which is nothing but uh those are the columns which we will be filling uh using code fine so we'll be calculating waiting time and turnaround time since burst time is already given by the user and process uh we can name it as p0 p1 p2 right and now we'll be calculating waiting time and turnaround time so i have set them initially to 0 and i is equal to 0 that is nothing but the value pointer now uh we'll talk about these two lists empty list later now uh inside the while loop we have a condition called i less than length of bus time that is nothing but uh length of the bus time is nothing but the number of processes right so uh for each process this iteration should run now uh inside the loop inside the loop this loop we are incrementing turnaround time by the corresponding uh burst times fine uh in first pass we'll be uh having the first uh we'll be having the burst time of the first process into the turnaround the second uh iteration we'll be having the burst times of the sorry the sum of the burst times of the first two processes so that's how we calculate the turnaround time right so turnaround time is nothing but uh the waiting time for the process uh added added to the uh bus time for the same process that is nothing that is about the turnaround time right now uh after that we are doing a print statement called we are printing the process name that is p uh and the corresponding number for this number and we are printing the burst time square of burst time and we are printing the weight waiting time along with the turnaround type at last so waiting time initially zero so zero will be printed for the first process since the first process doesn't have to wait so the waiting time though of the first process will be zero but for the second uh here we can see in the 13th line for the second process the waiting time uh should be the burst stem of the first process right so version of the first process in this case is 24 so uh the second process has to wait for 24 time units so we are incrementing wait time by uh the corresponding uh burst time value in this list and after that we are incrementing this i value such that this condition will be met and this while loop will be terminated this is how uh we'll calculate the waiting time and turnaround time and each time when you pin print this waiting time and turnaround time we are adding those values to two empty lists this is for the waiting list and this is sorry waiting times and this is for the turnaround time so after the uh after the all the iterations we are returning a tuple object that is mt1 and np2 that is waiting times list and turn on time switch so this function uh in this variable we'll be having this triple object right so after that we are calculating the average waiting time that is empty one uh sorry this will have the double right so under the index 0 we'll be having the waiting times right so we're calculating the sum of all the waiting times divided by the number of waiting uh times that is number of processes that is taken by the length and after that we are printing the average turnaround time uh by just giving the index one here so that means that in the and then at the index one we'll be having the turnaround time list right so we are adding all those values and dividing by the number of processes and printing this turnaround time so now quickly we'll run this and check for the output enter the number of processes let's give it as five for example and the first time for the process one is let us assume it has five second one is three third one is one two and three and that's it now we can see the first time uh i have been listed here in the waiting time and turnover on time the waiting time for the first process is zero very time the second process is the first time of the first process that is five so waiting for waiting time for the process three is the sum of the burst time for the uh previous processes that is process one and process two that is eight so in this way we'll be calculating this waiting time and turnaround time using the first come first of all algorithm now the second algorithm here is the shortest job first algorithm so in this algorithm the highest priority is given to the shortest job so that will be uh given the cpu first and uh here is a function now we'll start from the function call below so here's the function call so there are two attributes we are passing to this function call that is sorted of burst and converse terms so uh we are doing a sorting operation here so such that the shortest uh burst the processes that those uh having the shortage bus stamps will be in in the first position of the list and this bus ramp is a normal bus time that is uh without sorting unsorted normal list fine now here we are receiving those parameters and we are printing this heading process burst time waiting time and turnaround time so in this algorithm we'll be calculating waiting time as well as the turnaround time right so uh that's it now this is nothing but the value pointer now we're coming to the waiting time and turn our own time since uh we need to calculate these two we'll set it initially both these variables to zero fine now inside the while loop we have a condition called i less than then of course term similar to the first one first uh algorithm this is nothing but the uh the uh we'll be having the number of passes that is that is equal to the number of processes right so that's why i've given length of version now we are incrementing turnaround time by the corresponding uh value in the burst time list and after that i'm printing uh the value that is uh x or index of burst times of i plus one so in the first come first algorithm uh we just had to print uh the sequential order that is p one p two p three p for np5 but in this case uh it's not uh like that we need to print the process number uh which has a shortest boston right so that's why i'm using this index function which gives index of a particular object in a list uh index of uh the particular burst time of i in the unsorted list and this x is nothing but the unsorted list here in unsorted list what is the index of the current burst for example uh if you have uh some values in the first times list and if you sort it the positions will change right so we need a reference for the previous list also so that's why i've taken x and after that i'm printing the burst time so whatever the first time that will be printed uh beside that this one and after that i'm printing the waiting time waiting time is initially set to zero that will be zero itself and turnaround time which we have incremented just now by five sorry uh the first item in the first time after that uh i'm giving x dot index of first time phi is equals to minus one such that uh for example if there are any duplicates in uh the list burst terms list so index function uh will give the index of the first uh item itself so for if if you have three and three so index will be given that function returns uh uh index is zero it didn't grows the second current slide so uh we are uh replacing the value with some uh impossible value that is minus one and minus one cannot be included in both terms right so we are replacing the value with -1 such that if the duplicate item is encountered in any iterations so then index function will return the correct position of that item that is the main reason behind this now i'm appending this waiting time and turnaround time to this empty list given here just to calculate the average of uh the waiting and turnaround times fine now i'm updating the waiting time incrementing this by the burst time of uh the first the corresponding bus time so in the first iteration it prints waiting time as zero and in second iteration it prints the waiting time as the first time of the first item so that is what happening here uh now i'm incrementing the value pointer after all iterations now we are returning these two list which contains waiting uh times in the first list and the turnaround times in the second list so we are receiving that one here and we're calculating the average by giving using some function and dividing by the length function so in index 0 we'll be having the waiting uh times list and in the index at the index 1 we'll be having the turnaround times list now let's quickly run this and check for the output number of processes let us assume it has four first time for process seven nine three eight for example now we can see the shortest job uh shortest uh first time is here process three that is taken first and the first time of that particular process is three and uh the first time of the next shortest process that is eight right so the process four is selected now such way uh we'll be calculating all the burst times and waiting times and also we'll be calculating the average times using this mt1 and mp2 list now the next algorithm here is the round robins algorithm so uh in this algorithm what happens is each process gets an equal time share equal share of the time uh which you call as a time quantum value uh once the time coded value is finished what happens is the information of the current process will be saved into the process control block and uh the next uh process will be uh sorry the cpa will be given to the next process for what uh for the same time quantum value uh so this process is also called as context switching and uh this happens in the drop-in uh algorithm right now we start from the function call below we have the function call here we are uh passing the two parameters that is burst time and time quantum value those stamps is nothing but the burst times of each all the processes yeah and the time quantum value is the amount of amount of time uh restricted for each process to be created when uh you're passing those two parameters to this function now here we have this function now uh we are taking a three copies of the burst line so i'm using this slicing uh funk uh pricing method uh just to ensure that if you change any values in this copy in this copy list the original value doesn't get affected so if you don't use this if you just give it like this what happens is the original value it just becomes a reference to this original object right right uh if you change anything any value in this the original value that inverse times uh list will be affected and uh i've taken the three copies of uh this was times list and uh initially what i'm doing is i'm using this while loop what i'm doing is i'm calculating the order of the processes right by uh simply uh what you call by simply subtracting the each first time with the time quantum value and uh the value that is left over i am storing that one is in k and uh in that way if k is less than one that means that uh if the process has been finished that means that if the burst time has that first time is one and the time control value is two in that case it will be less than one the process will be finished in the first uh uh allocation right so that's why i'm giving it is none i'm updating the value there and using none this with this none when or else i'm uh keeping the remainder value at that particular location so uh that way i'm checking condition also here if w is equal to length of copy to minus one so in that case uh if uh if w uh that is the uh that that is used to access the uh list using the index right w is index here so if that one is equal to the length of copy to minus one that is uh if it if it has been uh so if it has uh across the list if it has traversed across the list what we're doing we are resetting the value uh that is w to zero that means that uh the list will be uh given again to this again consider for this while loop and at the same process continues until uh here we can see uh we'll be having a break condition here that is copy two dot count of none is it not equal to length of copy 2 once the count of none incorporated that means the done will be replaced uh with the original burst times value only if all the processes have been finished if all the processes have been finished that that means that k is less than one right so then i will be having none in all all the positions in this copy to list so if that count is none is equal to length of copy that means that if all all the objects in the length sorry in the list have got none then this process finishes that's how we calculate the order of the processes using this value now uh under this i have another value uh using this while loop what i'm doing is i'm calculating the waiting time fine uh it just calculates the sequential uh waiting times the uh in the record we have seen uh some time slots right so those time slots values will be calculated using this waiting time so the same process is uh here we can see we are separate and subtracting the burst time value with the given time quantum value if that even is greater than or equal to 1 then we are appending that event we are appending the s value this value is initially set to 0 which is meant to calculate the varying time actually so we are appending this s value before that we are incrementing this value by the time quantum value uh which indicates that it is the process is not yet finished it is the difference is still greater than or equal to one that's why we are appending this s to the waiting times list and we are replacing the particular value in the burst term with the difference that is k after that uh this is uh i'm incrementing this j just a minute uh and after that uh i'm checking another condition that is uh here less than one that means that process has been finished if so if the process has been finished we are incrementing the s uh by the burst time itself so the time quantum it's full time condom need not to be added to this bus yes right so if the process has been finished what are the first time uh it has it will be updated with s now we are appending that one to the waiting times list now incrementing j and after that we are setting the current position in the bus time as none indicating that the process has been finished now uh just a minute we are incrementing this i so once uh i is equals to length of copy three that means once uh one iteration is passed i is again reset to zero and j to one and uh after that uh we have a breaking condition here that is alif burst time start count of nine is equals to the length of puppet the similar one uh if the count of none in the first time is equals to the the number of processes length of copper can also be taken as number of processes right so uh if that even is true then we'll be breaking this while loop the value will be terminated now after that i'm using this loop function to uh to map each object of the processes to the waiting time so what it does is it creates a double object uh with uh two values together so the first process will be having uh the waiting time as well so in that way i'm converting that to list and storing that one is got variable using this while loop i'm actually calculating the uh waiting time for for each process and these are the variables i've said before this is uh empty list and i have a condition here while q lesser length of copy three plus one so we will see it later the condition and uh inside this we can see [Music] we start from this for loop first and we'll see uh about the remaining conditions above so in the in this range there's a length of the seq then uh i have set if max of acq of iz equals to zero that means that uh a cq of i is nothing but a table object which we have zipped right above using this zip function uh we have uh taken we have connected that one into list and stored that into it variable i'm using this while loop i'm calculating the uh positions of uh positions of the processes orders processes which are stored in dvrt like uh initially the position started from one right so i've said q is equal to one so uh in zeroth location what we have is the process number uh in in this devotee file sorry it's totally list and uh if that one is equal to queue then sort of is don't append to five so what what we're doing uh basically is this while loop is obvious uh we're just creating another list that is eq and we are storing some information that is the or indices of the processes that is if the first process is at the indus one and index five then uh one comma five will be appended into this list likewise uh and if the second process is at some index two and three so two comma three will be given into this list so that's what we are doing using this while loop and after that here here uh using this uh for loop i'm actually calculating the waiting times between uh the consecutive two processes right uh i'm using this for loop uh in the range of line of sequence that means that uh the number of processes we can assume it like that so i am taking these two variables total equals zero j is equal to zero and if maximum of scq of i is equals to is equal to zero that means that it uh in a sequel we'll be having the starting index index and the ending index right so the last index so uh the last index uh is obviously it will be the maximum if the maximum of the c c of i is equal to zero that means that if if it is no nowhere repeated in the list so what we're doing is we are inserting uh we are taking a list called uh waiting time up to that variable that is i we are using the slice smith sizing function here and uh we are inserting zero to this uh we're inserting zero to this a list at zeroth index and after that uh we are reversibly we are iterating in the reverse order in this range length of a minus one up to zero and we are updating we are calculating the total waiting time like the waiting time of the uh particular particular uh uh tuple value in the list minus the uh weight minus the waiting time of the uh item towards its left side that is k management right uh and after that i'm up uh i'm uh appending this total to this saan empty list right after that i'm uh deleting that particular record from the tot list after that i am using a while loop which uh which is this is an exceptional condition if maximum of seq of is equal to zero then this condition will be draw otherwise we'll be executing this while loop uh as default now while j less than maximum of sequence for example if the value is spread across spread until 5 in xy so this will be 5 right so until 5 will be calculating and so j starts from 0 here so and if maximum of acq of i is 5 then until five will be calculating the corresponding uh sorry uh consecutive uh waiting times that is j plus one of i minus j of uh one j plus one of one minus dot of j of one so uh in that we will be calculating the total waiting time uh and we are incrementing the j as well and uh if it if it is equal to i plus one uh if we have encountered the same same uh process number then what we are doing we are just incrementing the j plus one so that we're ignoring that one we will not be calculating including that one and we'll just ignoring that one ending incrementing the j pointer so if j is equals to maximum of a k of i minus one if all the additions have been finished then we are checking if i is greater than 0 then if i is still greater than 0 then what we are doing we are we are incrementing total plus is equals to waiting times of i i minus 1 so that uh and after that we are appending this total value into this sen so this what this means is we are uh calculating the uh waiting time for each process in each iteration right after that we are breaking this loop so in this way we will calculate the waiting times of uh for each process now we are printing the heading that is process first time waiting time and using this for loop uh i'm actually printing the values that is the processbook name the policy and burst terms are initially given by the user and we need to calculate just waiting time and turnaround time so uh to do that uh i'm uh i'm using uh i'm i'm just printing the values from the uh waiting time and burst time for the turnaround time now uh i'm just returning some values that is average to wait time waiting time and turnaround time the average variant time happens to the sum of the sand uh then some of all the items in the sam list are the length of that list now the uh this average of 20 time turnaround time will be we just have to make a list of both these added values right so i'm using a map function here uh which takes the values from the uh uh two list that is copy one and send copy one is nothing but uh for waiting times right so you just have the sorry it it has the burst times and scn has the waiting times and it takes two inputs from this to list and gives to the sum function which will be stored into this list and we are dividing by the length of that list and in that way we will be calculating the turnaround time after that we're returning this double value to this here lis and we will be printing average waiting time and average turnaround time so we'll quickly uh just run this and check for the output the number of processors that that'd be five first time for first one is five let us take it as five three one two three and the time quantum value let it be two now we can see uh the first time waiting time and turnover on time and average waiting time is 7.4 and the average turnaround time is 10.2 the next algorithm here is the private scheduling algorithm so where we schedule the uh sorry processes based on their priorities so we'll be taking the priorities of the each process as an input from the user so we'll be uh scheduling them based on this uh there's no priority that is uh based on the highest priority now we start from the function call here so we have function here priority and uh we have a function call here that is uh assuming so here we have functional that is priority we are passing two parameters for that one first one is the first time the second one is the priorities so the first time happened to the first time of each process the priorities happen to be the priority priorities associated with those first times fine now uh we will look into the function here now i'm printing up heading called process burst time and watching time and and also we have a turnaround time so process and verse terms both will be given by the user and what we need to focus on is waiting time and turnaround time so since we need to calculate both uh this waiting time and run on time initially we set those variables to zero and this i is nothing but uh the value pointer here so uh we'll see about these two lists later uh now happens now coming to the condition in the value that is i less than length of boston length of boston is nothing but the number of processes right so this loop runs uh for all the processes for each of each process now we are finding the minimum of the priorities so if the pri if the priority of uh particular policies is one so uh i have taken like uh it has the highest priority right so i'm calculating the minimum of the current um the list that is priorities now i'm calculating the turnaround time by just i'm just incrementing this turnaround time by uh the burst time and i'm using the index function this pri uh first time of priorities dot uh index of k so k is the minimum of this priorities right so uh i'm just getting the index of that minimum value from the priorities list and uh i'm giving that one as the index to this birth stand such that so that will be uh will be able to get the actual burst time of the of the uh highest priority process so i'm incrementing the turnaround time by that so initially it will be set it was set to zero now it will have the uh sorry uh it will have the uh first time of the first uh uh i mean the highest priority process now i'm printing the uh i'm using a print statement to actually print the priorities uh sorry uh the burst time uh waiting time and around time so i'm just taking p and um this is str function which is which i'm using to give the process a number so process number should be the index of the uh the process in the priorities list right so i'm just using the index function to get the index of that one and after that i'm using i'm giving a space and after that i'm printing the first step that is square of burst time of priorities dot index of k so i'm just taking the index of that uh minimum priority and i'm giving that one to the burst stamp index variable so uh what it will do is it will uh actually take the burst time value of that particular priority after that i am printing the waiting time so we can see htr of waiting time which is certainly initially it is set to zero after that i'm printing a turnaround time internal random which was uh actually incremented just now that will be printed here and after that i'm appending the waiting time and turnaround time to these two empty list which is nothing but uh just we are calculating the uh just to calculate this average and average time for the both waiting time and unknown time after that i am incrementing this waiting time by the uh by the of the price of the process that has been selected for that but that means the process with highest priority we incremented the waiting time by the priority store index of k and uh i'm giving that one as an index variable for this burst time that means that the first time of that particular process will be selected incremented to this waiting with time which is a variable that is already set to zero i am incrementing this while loop a condition that is such that this condition will be false at least once when um and after that in this priority list and what i'm doing is i'm just at that particular index i mean the priority highest priority index i'm just giving some uh impossible value that is maximum priorities plus one so maximum priorities plus one will be the value uh which is not present in the particular list like so i'm just replacing that one while that value with some other value so in order to do in order to avoid the duplicate values in the list now after that i am returning these two values that is mt1 and m2 this both these lists will be received here that is lis and i'm just calculating the sum and length of this that list and converting that to sjr and appending with this for concatenating with this string rest of this thing and now let us run this code run and we'll check for the output now enter the number of processes let us give the number of five first time for the process one five eight two three six and uh we need to enter the priorities as well so uh under the five priorities let us assign priority three two five five means it has the least priority now for price four let us give it as a first priority and process five let us give it as four now we can see the process that has the first priority that is p4 is has been selected first and now the next priority is given to the process 2 that has been selected next so in such way in such way all the processes will be scheduled based on the priorities i hope all the outputs are correct and thanks for watching
Info
Channel: KensTV
Views: 102
Rating: 5 out of 5
Keywords:
Id: WzBq3xxtvTQ
Channel Id: undefined
Length: 27min 36sec (1656 seconds)
Published: Fri Aug 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.