Illustration on fork() system call

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to Tector calm so in last lecture we understood how fox system call is used to create child processes okay and what are the various things which you should know in order to solve the problem on fork system call so we also took an example so if you are watching this video first I recommend you to go back to the speed the original video with which I started and then come back to this so in this video we will just do the illustration on how this system call works okay so this is a piece of code given and we have two for calls you can see but one of them has been wrapped into the condition right so here the last point or the point which we were talking about that fork returns the process ID to parent but it returns zero to the child right so that thing will be used here so you should watch this carefully so first of all ah this is a whole code I will just write it down in you know short form so this main main has this four call so whatever is returned it is stored into P and then it is compared that whether P is greater than zero if yes then another call otherwise print tected okay so basically this code is we have this for call okay then we have if condition if if this let me write it on P equal to so if this P okay if this P is greater than zero then we have another fork we have then print statement then this closes here then we have this another print fine so this is the whole thing al this is my process parent process I am calling it p1 okay now let's start executing and we will see how it works so first this fork is executed as we know this will create a child process so we will also create a child process so this child will be copy of the parent okay but but it will start executing from the next line right so both parent and child process starts execution from the next instruction okay so here this is fog okay fork return something which is assigned to P okay now as we were talking that it will start executing from the next line and we have talked that fork returns process ID of the child to the parent so if this is the child this is c1 we have three four seven let's say this is the child so for this for this fall call what will be returned to this parent it will be returned here this process ID that is 347 but for the child what will be returned it will return zero that's what we have learned right so fork returns process ID to the parent process ID of the child to the parent but it returns zero to the child process okay so now the point is we have two instances of P in both the processes in this instance the value of P is now zero which has been assigned from this Fork because in this fork this is a fork inside the child and child the fork returns zero to the child okay so for this P we have value zero okay but in the parent fork returns process idea of the child which has been newly created so in this P the value of this process ID will be there okay I hope this is clear now so now from both the process and parent and child it will start executing from the next instruction fine as we discussed in this parent for this P we have value this 347 so this will be true here right but for this P here as we have zero which we have got from this fork in the child okay so here this P is not greater than greater than zero so it will be false here right so as this is false for child these instructions are not at all executed right as this is false okay so for this child it will direct a directly execute this print statement and it will print what dicted right but for this parent we still have to do something because for this parent this this is true here so once this is true it will start executing the it inner code of this this if statement so again we have here fork okay so as we have fork here again this will again create a new process which will have so after this fork we know that both parent and child this will start execution from the next instruction okay so here you will have one print statement that is tected okay and after closing of this if statement again it will print dicted okay so this is this print statement will execute in the parent itself here also this print will execute okay so this print will print tected and again this print will execute okay so now you can understand that how this process ID creates different so on the basis of this process ID you can apply some conditions and you can apply what portion of the code you want to execute fro for a child process which has been newly created or for a parent okay so on the basis of this process ID and condition checking you can differentiate between child in parent process okay now there can be a question write that what can be the order of this print statement okay which process will execute first whether this will execute child like I executed first I finished this child then I went back to this parent then I came here then again parent so what will be the order right so as I am expecting you have already learned this scheduling process scheduling so this is a process this is a process this is a process as these are you know four creates processes for parallel execution so now these three processes are are parallel e being executed okay and depending on the resources available any one of the process can be scheduled over CPU for execution fine so you cannot be certain about the execution order it can be machine dependent so this will completely depend completely depend on the kind of processor you are using and how it is being internally scheduled over CPU these processes okay so there should not be any confusion over the order of execution and you won't be able to ask about it you will only be asked that how many print statements will be here or how many times this particular string will be printed okay so well this is all about this video I hope you understand how this process executes when fork system call is being used so if you still poses any doubt you can of course ask your question and we will be happy to answer so see in the next lecture thanks for watching
Info
Channel: Techtud
Views: 72,568
Rating: 4.9374371 out of 5
Keywords: GATE, IIT, Techtud, Engineering
Id: _kUiH8DG-Ao
Channel Id: undefined
Length: 9min 56sec (596 seconds)
Published: Tue Jan 10 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.