Transcript for:
Understanding the Wait System Call

can you see the screen yes sir okay so in this class uh i'll talk about the weight system call which is basically the extension of i mean this weight system call uses the date of child signal the sick child in fact in our last class i discussed with the exit system call which a process calls to get terminated and what we found related to the death of child process that any process if it executes exit it will be terminated even if it is not executing exit by default when it returns from main the exit system call is executed by the kernel okay so in some form we found that the exit system call is executed whenever a process terminates and if you can recall there are two signals involved in this exit algorithm one is the sig hup the sig hub that is to reset the process if it is a leader process So to reset that Leader Process ID, let us not discuss in details about that. But the other important signal is the death of child signal, the SIGCHILD. And during the exit system call in the last step, almost in the last step, the child process or the process which is terminating sends this death of child signal to the parent, to its immediate parent. So, in this class we will see that how this death of child signal can be utilized in various ways by the parent during its execution. So, that's why the topics are death of child process and which involves the use of sick child signal and then the wait system call. We will see that why and in what situation weight is required and before that we will see that can can we do the same thing without using weight also so the necessity for this weight and then we consider the pseudo code for the weight algorithm and some various uses of the weight weight system call we'll talk about that okay so this exit and weight algorithm you will find it in the morris buck book in pages 212 to 270 both the algorithms and the discussion so let us let me just show the codes directly can you see this code is the code visible can you see the code in the screen but it will be better if you uh turn it to full screen okay full screen just let me see now is it visible yes so just focus on this code uh just try to read the code what is uh there so all it says that in the main we have assigned this handler so this is handler for the sick child And it has assigned to the function F1, which just says that it has received the death of child. So the death of child signal, what happens? There are two points to note before I continue with this code. One thing is this death of child signal. What is the default action? The default action is ignore. OK, so. As it is written here, the sick child signal is sent by the child to the parent just before it terminates by explicitly or implicitly calling exit. So this part we know. And the default action defined for sick child is to ignore. So who is receiving the sick child? The parent is receiving. What is the default action? It is ignore. So hence the parent generally ignores it and the child remains zombie and we know that the child after it's sending it it remains zombie So in this case if I if I just set it to if I just comment it just let's not do anything about it So let's focus of the on the code. So we have fought so the parent has fought another child and if this is greater than 0 means this is the the parent part and this is the child part then the parent part what it does it goes into an infinite loop in every after every two seconds it displays the parent id and in the child part it just clips for two second and it displays the child id child process id and it exits okay so what we expect is a parent once it starts it forks a child and the child waits for two seconds displays its id and it exits however the parent continues to sleep for two seconds and display its id so that that way it goes on so let's see the outcome of this code so this is parent child exit so see within child pro within child id and within the parent id within parent and it like this so what do we expect if i say yes sir i don't know about others but for me your screen is freezing so screen is so it freezes for about 10 seconds constantly means you cannot see the screen No sir, just I get one frame and then about 10 seconds it freezes on the same frame sir. Okay, what about others? Is it his problem? I can see the code only sir. Only the code is visible? Yes sir. Sir, you can share the desktop or else you have to navigate to every different windows you select. You can share the desktop directly. Okay, okay, yes. so desktop sharing what is the option screen one initial meeting so screen one screen one is sharing now what is the where is the desktop sharing option desktop sharing windows screen one itself will do the work screen one now now are you getting things it will share everything that is on your screen have you got the sharing so terminal yes now is uh are you getting the terminal now yes sir yes sir we are getting the problem uh now everything which is on the foreground it will be visible to us now it is a code what's the problem there is full screen sharing what is the where is option so your full screen is shared sir if you change tabs we can see it so this is already now is it are we able to see the terminal now yes sir we are able to see the terminal now if you if you switch to the sublime text editor then we will see the sublime text editor that is whatever Yes sir, now it is visible. Now it is visible. Yes sir. Yes. And can you see the presentation also right now? No sir, you will have to switch to the presentation. Whatever on your desktop is happening, it will be shown to us. Okay. Anyway, so you are able to see the terminal, right? If you switch sir, if you switch to the terminal, yes sir, now it is visible sir. Visible. so this is the problem sir there is latency and when you start typing it freezes so okay so for a healthy internet connection there is like 200 milliseconds of latency but if there is a network drop then it will be like four to five second also depending on the internet so is it the problem with my internet or all are facing the same problem the frame frame rate is too low Sir, I cannot tell about this but for me it's like 1 to 2 seconds or 3 seconds. So I think that... Sir, for me also sir, there is an issue. For me also sir. Sir, try to type something in sublime sir, it will be obvious now. You see, I'm typing in sublime. Now it is visible sir. right now right now it is visible so like almost four to five seconds delay is there sir yes because of uh internet delay from both the sides from our sides also and from your side also you see i am recording it so let us uh let us not i mean try to focus on the on my words and i understand maybe if i switch from one screen to another it might have some delay i'll try to repeat and speak with some pause otherwise uh there is no other option i guess you said that should be good okay so let me yes let's talk a bit slowly and then in any case it will be recorded so you'll be able to see it later on also right so whenever you are switching this up to switching the screens and that time you can wait for one to two seconds and then yeah actually the problem is if you if you they try to incrementally you know pass on the frames so if they find that there's little bit of change in the frame probably they don't pass the whole frame so but if you change the frames then there is a lot of things to information to send because there is a abrupt change so that might be one reason so if i stay there for a while then things becomes better gradually yes that might be one reason so the difference between two frames that probably is passed on to save some bandwidth that that that may be the reason i don't know anyway so i think right now the sublime text is editor is visible you So what we found is the parent forks the child and the parent continues to sleep for two seconds and display its ID. And the child just stays for two seconds and displays his ID and then it exits. What we want to see is now because the child has exited and the parent should have received the sick child signal, right? And once the parent receives the sick child signal, how it reacts? the default is just ignore it so if it ignores what do you expect will the parent will the child remain zombie or it will be because it has sent the signal to the parent it will be removed from the process table what do you expect tell me the answer what do we expect here so remain in the process table zombies okay let us see so just focus on this within child process i think you can see then within parent within parent like this is goes on and if i open another i open another screen ubuntu another terminal i think you can see this can you see the defunct process the zombie process 252 yes sir okay so that means say it has terminated it has sent the death of child but the parent has opted to it's not opted the parent by default ignores it so nothing happens with the reaping of the child process so the child process is not reaped okay and then if i press ctrl c then what do you expect if i press ctrl c which process will be stopped the parent process will be stopped what will be the fate of the child will it remain it will it remain a zombie or will it so what will be the fate of it what do we expect it will be removed from the table process table and it has indeed been removed so why who removed it the parent has not removed it the parent received the jet of child signal it is ignored it by default so who has removed the zombie child of this parent what has happened see the point is when the parent dies so till the point the parent was alive the child was zombie but when the parent dies if you can recall the exit system call the steps it says that if it has some if the particular process has got some zombie process which is yet to be ripped then what it does you it sends on behalf of that zombie process the death of child signal to the init process. Can you recall that? So that means when this parent is going to die it will send on behalf of this zombie child a death of child signal sick child signal to the init process okay and then the init process that init process will repeat automatically. So this reaping means removing of the zombie process from the process table. We prefer to call it as reaping the child process. This is done by the init process. Now how this can be done? It has got a special system call which is called wait. So if a process executes wait system call, that wait system call actually removes. This removal of the zombie process is done. So generally the init process always. waits for any, so if it receives a death of child signal, so it will always, you know, remove it, it will read that particular process. So through that, this parent as well as the child both are ripped, and you will not find the entries of any of them in the process table. Is that fine? So if the parent has not ripped it, rip its child, So the child will be anyway ripped by the init process when the parent dies. This is the first thing. Next, if I catch this signal, seekChild, catching means what? By default it is ignored. Now if I, the default action for the seekChild is this. Now the parent can define, as you can see here, the parent can define the custom defined action, a signal handler for this seekChild. so if i do this so if i uncomment it so i activate this that means whenever the parent process receives sick child it will invoke this f1 so now what do we expect if i run this what do we expect so it and you can can you see the can you see the message here Received signal 17 death of child can all of you see so I it has happened So the child has done the same thing. It has terminated after two seconds displaying its ID Within child like this parent earlier ignored the signal but now I ask the parent or the parent opts for handling the signal by calling a phone and what if one says just displays the signal ID and the Like that. So received this signal, this death of child signal. So it gives this prompt. So it gives a prompt. So it somehow handles the signal death of child. Does it mean the parent will read the child handling the death of child? Does it mean the child process, the zombie child will be removed by the parent now? What do we expect? Run it once again and see. Now the child has died. It has become zombie and the signal has been handled. So does it mean that the parent has reaped the child? What you can see here? Did the parent reap the child? Removed it? No. So can you distinguish between the actions? So sending sick child to the parent, the parent may ignore it. The parent may handle it. but that does not mean that it is going to repeat so how this how this child process can be reaped or removed the only way to do this is to execute a completely different system call that is called the weight w-i-t okay so we'll see that how how that is done and how it is achieved so now so this part is fine next let us let us consider this this particular call uh this particular code so what is this so what i want is if i let me get into this okay the seek child sent to the parent when the child terminated called calling explicitly exit system call this this is ignored so this is in two terminals we can see i am showing the slides can you see that yes yes sir okay good then the problem is resolved i guess somehow so now in this case what i show i have disabled this comment and this example also i see that this is still So now the question is this, the parent wants to proceed only if the child has terminated. Got the point? So we are no longer using wait. So we don't know wait yet, the wait system call. So the point is the parent now wants to proceed only if the child has terminated. So there is a condition. So here, if you can see here that this is x greater than 0, then sleep 200. So the. parent wants to sleep for 200 and then it goes into an infinite loop and now there is a little bit of change in the in the code so here so the parent is sleeping for two seconds and each time displaying the parent id but this repeatedly displaying its parent id this it will do only if after it makes sure that the child that it has forked has terminated what the difference So the thing is here the parent is going to do the same thing repeatedly as like the previous one here The only thing is it will do it after sleeping for 200 seconds Means, I don't know when the child will die the parent may not know so it waits sufficiently sleeps sufficiently and after that and during that time it will expect that child will die and it will be able to resume It will be able to continue here So that's the code. So if I run this code, what do we expect? So parent sleeps for 200 milliseconds and in between for 6 milliseconds the child process sleeps and it displays this prompt and it exits. So what do we expect? What will be the outcome of this? Say for example, this I comment first. and these two and we write sleep 100 that's that's a code and it is parent child exit wait i think you can see the execution what happens why it stops for a while earlier it started showing the displays now once i start as you can see here once i start nothing is coming what it what the parent is doing the parent is waiting for the child to terminate and the child will take six uh six millisecond six seconds so it pauses for that period of time and then when the child dies it receives a signal and then it enters into the for loop where it keeps displaying its id. Have you got the output? Have you got the difference in the output? Hello? yes sir so the point is now now the problem here is so so the only i mean the the objective is what so if the child process uh if the child process dies only then the parent can start but how the parent knows whether it will set it to 200 or say 2 or 1000 how it knows can the parent know prior because this is the child process so the child process might generate a random number also and it will sleep for a random amount of time. So the so the can the parent guess it? It will be a wild guess right but as you can see what is the what is the function of this sleep system call? So the sleep system call anywhere it is called it forces the process to get into the blocked state but whenever there is a signal that that the that the process receives the process immediately wakes up. and this has happened here also here you wrote sleep 200 so this parent is not sleeping for 200 seconds rather it's just sleeping for six seven seconds and once the child process dies as you can see here it receives a death of child sick child signal and immediately it wakes up and it wakes up and it starts doing its infinite loop this this printing got it so if the if the requirement is something like this that the parent wants to proceed only if it has its child has terminated we can somehow manage it by calling sleep but what are the problems what are the issues here so the so this can be done and we can take the analogy of the assignments that i discussed about in my last class where say for example the parent it has a very long array and it has fought say five children to scan through one-fifth of the array in a non-overlapping fashion and if any of the child process or maybe it has just fucked one child say for example one child for simplicity and the child is supposed to find out an element within that array if the element has been found i mean till the element is found the parent sleeps it cannot proceed so if the child has found that then only the parent can proceed to the next step and the parent does not know when the child will find will be able to find that element in that array it may have to scan through the whole array it may be found in in the middle in the beginning it doesn't know so what is the main hurdle what is the main problem here if i want to implement so here the parent is synchronizing with the child's death right so that is the issue so if i if i would like to synchronize the parent's execution with the child's death Simply by using sleep and in this fashion. What is the problem here? What is the problem? This can be done This is done also here. But what is the problem? So you have hard-coded the time it may take less time And the point is you cannot wildly guess it because it may be 200 now you may say that okay Let me give you infinitely large time, but that is also not there is no point of doing this so we this can be done in a much better way intention is known now this can be done in a better way and that is what is done by calling the weight system call so the parent wants to proceed only if the i hope everybody can see the slide so the parent wants to proceed only if its child has terminated but a better way of doing this is by calling the weight system call okay i think you all can see this by calling the weight system call so What is the change here? So instead of that sleep, as you can see sleep 200, now I just called this wait system call and this wait system call it accepts one input argument, it returns one output argument. So this parent wants to proceed further only if the child has terminated. So this is how it is done. So once this is done, this will have almost similar effect. The only thing is you don't have to make any wild guesses. At the same time you have some additional benefits also. what is that you can so this weight actually returns the id of the child for which i mean it has been with which it has synchronized right so this is one point at the same time i think we are skipping one point for long that is this exit whenever some process exits some value can be passed as an input in our algorithm also exit while discussing about exit algorithm we discussed about it so the child can pass on some value integer value meant for the parent's access so this is how we can fetch that value by what this weight in this input argument as you can see this is what this child exit code this is basically integer value integer variable and it is we are supposed to pass not the integer variable directly but the Address of it. So if you pass the address of some variable within the function or within the system call it can Copy some value in that particular address, right? So in an indirect way, it is basically this weight is Providing you the exit code passed by the child during its termination by calling exit Okay, are you all getting this? so this wait has got multiple benefits one is you don't have to guess how long it has to wait you get in return the child id for which it has seen with which it has synchronized and it also will provide you the exit code that the child has passed while terminating if it has passed something if it has done it implicitly then you will not get i mean you will get some default value but otherwise it can pass something here So that's why it says that it takes the address of an integer to pass the child exit code. So any integer you write, you pass on the address, ampersand that. And it returns the child process ID. So in some form, in some sense, you can say that wait actually provides two values once it is executed. Because it cannot, any function cannot provide two values. So one, it accepts as an address of an integer and one, it returns directly. So that is what it does. so you see rest of the code is exactly the same so if we run this code let's see how it works so now let us comment this sleep the you know the bad way of doing things and let us do it in a proper way now if i compile and run it okay so still waits for a while for six seconds for the child to die and then it receives it says received signal this death of child let me stop it so what are the outputs that we can see here this receive this so definitely first of all it waits for this amount of time and whenever the child dies it sends us seek child and the seek child as you can see here the seek child is caught and this message has been prompted what is that where it is from this is because whenever it receives seek child because we have activated this handler instead of default ignore so it has displayed this and then after doing that it has came came to this printf statement what it prints the child ID and the exit passing the exited pass passcode what is the exit passcode what the child provided while it died what is what is the value that has passed in the exit it's one and what it displays here are you getting any problems here what it is supposed to display in this statement child id 299 that's fine Exit exited passing the code 256 did it pass 256? What did it pass? Hello 1 1 1 1 so but it is it so let us see if I if I pass to what happens If I pass to what happens? and it waits so it's doing all the same but you don't have to care about how long it has to wait so it will be done automatically now what it says if i want now can you say what what is happening here why it is so what is the reason if i pass three so can you guess what is happening there is a there's a formula i mean through which it so it is not that randomly it's displaying the passcode so the passcode is one it says 256 if it's two it's 512 if it is three it says 768 what happens 256 into the number yes so this is a rule that actually the designers has defined because any integer generally it is assumed to be occupying 16 bits right so there is a lower order bit there is a higher order bit so when it passes this exit code for some reasons they have decided to store the bits corresponding to that one or two or three whatever in the higher order bits what the point if you have got 16 let me see oh there is no i cannot write here i think no this is not possible so you can you can guess this so if the child has if the child has passed one at three here so three what is the code of three zero one one so this zero one one will not be placed in the lower lowest order bits of the 16 bits rather it will be copied in the higher order 8 bits of an integer so if we do so that means you are basically you know shifting it 8 bits so every shift means it's multiplied to multiplied 2 that means 2 to the power uh so that's why it is multiplied by 256. got the point is that fine yes sir so the the best way to see the exact value that it has passed is just to divide it by 256. if you divide it by 256 it will so this is in that case what is so what if we uh pass a value greater than 256 definitely it will be only copying a part of it like if you so it will be overflowing because it is allowing you to specify only us only in terms of 8 bits right higher order means higher order 8 bits so if you have a larger value it will be discarded so you will get you will be reading basically garbage in that case if you have not made the provision so the thing is so that's why i mean if the child wants to pass some value to the parent on its termination uh that is not too flexible i mean it's just for uh i mean it has got a very limited range of information that it can pass on so you can pass up to values say 0 to 256 like that 255 okay and if you do so it will be then multiplied by 256 and it will be displayed so It can simply pass codes. That's why it is called the exit code. It is not just values. If you add something, if you get some sum or you get the mean, you cannot pass on floating point values. So it is not meant for passing information from the child to the parent. Rather the child can. So this mechanism is used by the designers to pass on some exit code. Mostly the status of the death. So the death has been normally done. It is fine. Or you can sometimes you do like. there can be three four cases within the child and the child can pass on that okay this is a case five that has terminated me or case six and the parent knows what these cases are so that way it can it can happen so these are basically status codes and this will be when it displays it is 256 multiplied that so we can divide it by 256 during the displaying okay so the point is uh this use of weight system call as you can see it has got several advantages as you as we see here and when it okay so we just missed one thing when it is now what do you expect so if i execute weight okay so this has died now if i type ps aux what do we expect here will you be able to see the defunct zombie process child process or or what will happen here also the parent will rape the child in this case yes and that that has happened you see now there's so that is the that is the main use the main purpose of using weight so that's why uh so there are several advantages as i mentioned one is you don't have to wildly guess the time of waiting so but both is a blocking both wait and sleep is a blocking type uh system call they actually blocks or suspends the process it gets blocked until unless you receive a signal okay so this weight is actually triggered only when this death of child signal is triggered its main purpose is that but if it is sleep any signal can you know reactivate the process resume the process another thing is if you execute weight the The parent process will see in the pseudo code of the weight. shortly that it will read the child process and you can see that although the parent is still running but you see the child by the time has been read so it's a much better way to do this and if it is not done then it will be done after the termination of the parent by the init process so you can assume that the init or you can consider right now that the init process always executes a wait system call to synchronize with any teth of child signal So whenever, so that's why these defunct processes, even the parent process, the foreground parent process never remains in the process table because the init process always executes wait to read them. It does not keep things pending. Okay, so that is how it is done. And this wait is the only way to read things, read the zombie process or defunct process in the system. Okay, so this part is done. and as i mentioned this exit code we can use it judiciously and as as it is required to pass on important you know indications to the parent that what has happened and given on the application you can try it to use intelligently but you don't have too much flexibility also that you have to recall that you have to remember okay so now so this is what we using the weight system so this part we have discussed so just to summarize the points allows this weight system call allows the parent to synchronize with the death of child signal and here And here synchronization means allows the parent to remain blocked till its child sends sick child due to its termination So this is what synchronization means so maybe something like I have given assignments one assignments sheet So I want if any of the student submits a computer assignments Assignment sheet then only I will release the next like that. Okay, so I just but till then i just sleep i do whatever i want like that or maybe i say that i give you some assignment and the the child or the student is doing it so i will not take the next class until unless you solve it and return it to me and there is no deadline so if you do it early i'll start the class early if you do it if you take two months i'll take the next class after two months something like this so the best way to do this by is calling wait so something similar to this so it allows the parent to read the child process return the id of the child process which has been which it has synchronized with and also returns exit code in form of i into 256 so you just divide it by 256 to get the actual value so now there are so we'll talk about some variants of this weight system code so now what happens say if if the parent has got multiple child processes so what do we expect you if the parent has got multiple child processes what do you expect then so do you expect the parent to execute multiple wait statements each one to synchronize with each of the child process or how how things should be done getting my question hello have you got my question so we are talking about just one parent one child if the parent has got multiple children so it's just like its immediate hierarchy so there are five children and there's a only parent so the parent is a parent of all the children's like that we know how to create it so this code actually illustrates that so the point is now i want to use weight so that the parent waits for so there are two cases now So if that thing happens, so you will be interested probably that the parent should wait for any of the child. So it's like I have got 150 students in this class. I give an assignment set and I want that if any of you submit, I'll start the class and then I ignore whatever others are doing. This can be one policy. The other policy is I would expect, I would ensure that each and every student submits assignment only then I start the class. So the thing is the parent wants to proceed only if. all its child has terminated in this term it was a highlighter so if it is only if all its child has terminated or it can be if all of the child has child has terminated so if only if then how to do it so this is the code first of all let us Let us focus in this code as you can see. So first we ignore this part. So just focus on this part. What this part does for high cost to 0 i less than 5 i plus plus. So it's a 5 iterations in this for loop. If fork equals for whatever value fork returned is equal to 0. That means what? Then you do this. So who does this? Child. So it's basically exclusive. It is exclusive to the child code. So how many forks, how many times forks are called? Five times. Who calls it? The parent. But within this, the fork returns something. And if this return value is greater than zero or I mean non-zero, definitely it is non-zero. So non-zero means, so this is a parent. But if it is zero, that means definitely it is a child. So this is a child code. So each and every child will execute this part after they get created. and they will basically print something their pid they will sleep for a while depending on the value of i where from they get the value of i they get it from the parent itself copied okay so the first child the value of i will be zero the second child the value of i will be say one two like this during the time of the creation the value of i in the stack segment will be simply copied from the parent to the child so each one each one of the child will sleep for different amount of time and then immediately after waiting for a while they will exit and they will pass on different exit codes okay so this is one two three four five like this and once they exit there is no point of following this because none of the child process will ever look into this part because they start here they end here however the parent after forking all this after forking they will fork they will come again again fork they will come again because they will not enter into this part of this code so what the parent will do the parent after forking it will come directly from here to here okay from here it will come to here this part and where what the parent is doing it is executing the weight system call again with the same piece of code there is a status code the the integer value whose address has been passed the child id is returned and we just write this okay so now just just one point if i if i write this piece of code what do we expect will it will it actually so if i just execute this piece of code what do we expect will it wait for all the children to terminate or will it wait for one child to terminate what will be the case see the the thing is the difference is very simple if i if i remove it what i remove this handler so if i don't uh if i don't set this handler sick child equals to seek ignore then it basically will do it for only one child process termination if i remove it if i activate this that means if i ignore if i set this sick child signal to ignore and if i run this code then it will actually it will terminate after all the processes gets terminated all the child processes gets terminated okay so the point is if all the child process gets terminated then this weight generally it will return minus one because there is no point of returning a particular child id so the thing is in that case this wait system call it will it will keep continuing here itself till the parent process is not having any more child running or in the zombie state so the thing is having this activated if i run this code it will make sure that the parent ensures the termination of all its child if i want to make it only one of its Say termination after one of the child process gets terminated. So I have to comment it Okay, so then this becomes this only if it gets terminated So what can be the use of this code if you look back into the assignments that I gave that assignment where? again in that large array there are five processes which are searching part of the array and the parent is waiting for some outcome that some of the child process must be able to find some uh the element that need to be searched if i receive one search successful search i'll be happy the parent says so then this piece of code will work the way it is written here so the only thing is the only change what will be the only change within the child process we have to basically do the search if this is this in a while loop you have to search it and every child need to know what where from it will start and where from it will till which point it will stop so maybe this value of i can be used as an indication of that so if it is zero so uh you know the whole size of the array is n n divided by five because there are five processes so accordingly if i goes to zero that means you are starting from the beginning if i goes to one uh you skip say n by five elements and you start searching like that and whoever receives the positive i mean a successful search it will immediately exit that's all but the point is here the the child process is not able to provide the index of the array where it has been found it is not possible maybe it can dump it in a file because there is no other uh communication in that level that is possible between a parent and child the parent says the child says that okay i have found it now if the parent wants the child can drop it in the file because for the time being you don't know shared memory so otherwise the child would have copied in the shared memory okay so right now let us assume that the child cannot communicate which particular index the value has been found okay but it can say that okay it is found so you can do the rest of it if you want but uh it may be it may happen that the the i mean say in case of finding out the mean okay or finding out the prime numbers all possible prime numbers so then the parent wants each of the child process to find out all possible prime numbers so i would i would like to find out how many prime numbers are there in a range from one to say 10 000 if that is the case so every each and every child should count the number of primes and in a file they can dump that i found 5 i found 10 i found 20 like this and the parent after synchronization can count them all and say that okay this is the count so in that case the parent need to wait for all of the child process to come terminate okay are you getting me what i mean how to use the variations of weight okay so weight white is required we mentioned if it is a single child then it is too trivial if it has got multiple child processes then also there are two ways we can do is two variants and there is one more variant also so the parent weight pid is another system called through which the parent can specify that that i want to synchronize only with this particular child and not with any or all okay so there are three variants of it so the parent there can specify a specific child with which it wants to synchronize maybe the parent says that i want to synchronize the last child say for example or whatever okay so there are different variants of this so let us look into the weight system call algorithm so you can i think try those variants and as the assignments as a continuation of the assignments the assignment set to the the second page that i discussed in my last class unfortunately i could not get the whole recording but have shared the slides so in the second set of assignments you will find that this kind of synchronization is required the parent and the child and without knowing weight it will be a bit more cumbersome i mean it will be more tedious and but if you know weight it becomes very simple both creation and so i have given the hints of that so let me see that whether you can solve it by using weight and by using different variants of the weight okay and if you are unable to do this also let me know maybe you can write me the mail or prior to the class you can specify so that i can take it up as a discussion right i can explain that code okay before i end let me uh discuss about the uh talk about the weight system call algorithm so what it what it says the weight algorithm weight input is address of the variable to store the status of the existing code the input that we all found output is a child id and the child exit code so child exit code is passed in the address itself it's copied in the address and child id is explicitly returned now what it does internally if the waiting process means the parent process has no child at all so the parent has not forked anything but it is calling wait then it returns The kernel knows that it's of no use because you don't have a child the child and it wants to synchronize with some child or all child so it's all bogus. So it returns. If it is not the case that means it has got some child processes. Now what can be the possibilities? So in that case it will get into an infinite for loop because it can have multiple child processes also. So this loops until returned from inside the loop. Okay so if so there is a return here there is a return here so only through these two returns you can exit the loop otherwise it it continues and continues like this okay so what it does if waiting process has a zombie child so if any of the child has died so multiple child is spawned or just one child it has spawned doesn't matter so the process finds that it has got a zombie child a defunct child in the process table So whenever the parent executes the wait system call, it will check for some child processes. If it has got some child processes, the system call will continue in this for loop where it will try to find out some zombie child in the process table. If it has got a zombie child, then it will pick up arbitrary zombie child. If it has got multiple zombie child, it will pick up any one of them. It may not be the first one which died or the last one, whatever. it can be anything so maybe it will scan from the top and it will find out some zombie now it adds the cpu child usage to the parent free the child process entry so here it is here it is a ripping so here it reaps as you can see free the child process table entry so this is the only way that the process table entry which has been allocated and blocked for a particular process i mean which has been acquired for a particular process gets freed and this is a point to note that This even in the exit system call this is not done. Everything is done, everything is undone in terms of the resources that the process holds excepting this freeing the child process table entry and this is done only if a wait system call is executed either by its immediate parent or by the init process. Then what it does it simply returns child id and the child exit code. so this is done and definitely before you know removing it it copies the child exit code from the process table entry and all this so this is done so once it returns then it comes out of this folder so this is so this part is executed only uh in case the process is the parent is synchronizing with any one child death not all okay but if i set the signal handler to ignore the sick child i mean sorry yes the sick child death of child signal then it will not continue with this one it will it will check for if no process has if the process has no children so in that case it will it will not consider this part it will not consider this part rather it will come to this part sleep at interruptible priority event child process exists so if we have ignored that then even though there is a zombie zombie child it will remove the zombie and it will start sleeping once again it will again get into the blocked state waiting for some other child state again if second child dies say for example so it will be it will be awaken up and it will this part and it will come to this The process has no children so it will read the zombie and then the process has no children if it has got more children It has got no Chilean to return. That means after two child process death The weight will terminate the weight system call will complete if it has got few more child processes again It will start sleeping in the interoperable priority some other third process dies child process again. It comes here. It is a zombie checks if it has got some more child process to terminate so in that process if we have ignored it so this weight system call keeps on you know iterating in the for loop every time sleeping at an interruptible priority and waiting for some child to die and if all the child process has died then it will come here it will find that no zombie no zombie child is so if the child if the process has no children it will return okay So it will return some error code. So this is how the same wait system call can function in both forms, in both variants, where it waits either for any one of the child or for all the children to terminate. So let's stop the class here because this is the end of the... signal handling and also the discussion of different useful system calls the fork weight exit and as we found that the exit and weight uses in different ways the system the signal handling and we also found and the assignments i would expect you to try and solve those assignments and let us stick to the same assignments just try to do it using weight system call see how it can be done more much easily if i know the weight system call the only thing is the hurdles that we face here is the child is not able to pass on some computed values to the parent this is not possible because the parent and child don't share anything among themselves except the only thing is except the files if the parent has opened some file the children can write on that file and thereby they can communicate but other than that it is not possible okay so how it is possible sometimes we can declare a particular variable as shared shared variable or shared memory some some portion of the memory as shared so that is again a way of uh inter process communication implementing inter process communication okay so we too say we all have nothing in common but say we we create a shared document say what document in google and say let us assume that this will be shared so no matter where you are where i am so we all will be using that shared board for communication so shared memory is like that so once this is there so then the child can write the parent can read the parent can write the child can read like that but without that it is not possible that the parent and child communicates okay so so through the signals they can have some kind of you know some kind of process communication is going on but this is for control synchronization this is one thing and another point to note is when we talk about threads there we have got a much you know easier way to enable communication between the parent and child not process but threads you've got a parent thread and we have got child threads so then parent and the child they share in between the local variables i mean sorry they have got their independent stack but they can share the data segment if you write so data segment is trivially shared by all of them so definitely we'll talk about in details about this later on in the next class i'll be talking about the scheduling algorithms a very simple topic we'll talk about different possible algorithms and you already know about some of the algorithms the different preemptive algorithms round robin we have mentioned in the introductory class we have talked about the priority based scheduling so there are different algorithms and there are different variants also so we'll see that which one is good or bad or what is the performance issue in each of them and we'll also maybe that will be one class and in the other class i'll talk about the fair share scheduling which is actually implemented in linux or unix based systems so how efficiently and how nicely it implements it that is very interesting to see and so that will do and once we do the scheduling then we'll continue with the threads because in the threads in the process you cannot to in use you know you cannot specify your scheduling algorithm because the process is a basic unit that the operating system deals with but when you deal with threads then you have the provision to specify the scheduling algorithm that a thread will be using so that's why i talk about scheduling first and then i continue with the shedding multi-threading okay so that will be the next few topics but again i repeatedly ask you to try to solve the algorithms as a previous one don't assume that you know weight and once you know weight you just try to rewrite it using weight and see how it works so the example that i just now explained here is the parent is there in the top and it has got all the children in the next level hierarchy okay so in the assignment one i asked you to create some hierarchies like this how many of you have tried these assignments till now did any of you try hello hello can you hear me hello am i audible or what happened anyone any of you anyone of you try hello yes nitesh do you try no sir not yet i'm actually delivered out of station so i'll try later on okay don't see uh i can always try to put you you know forcefully set some deadlines and all but i think let us be all honest i mean 7 days is a good amount of time so try something of it and if you face any problems you can ask them okay this part is not done so i would expect this is a class of 150 almost strength and nobody tried it yet that is not good i'm not enforcing mechanisms because there is no session as such so i would expect today is saturday next monday i'll try to take a class if director sir wants he can take i don't know whether he'll be able to take otherwise i'll take so i would expect at least some responses by monday will it be there or not the assignments hello i'm talking to the class the whole class is there any response you may not have all the all of the assignments complete but at least there should be some responses that you have tried this and failed or you have successfully completed this part okay so we will try please try because otherwise there is no point of so if you don't if you think that you will not be not interested in the assignments i can just keep discussing in the class and there's not a problem but at least some of you try and if if we discuss about the algorithms uh about the codes others also will come to know that how it is solved if they have tried also so let's uh and i would expect if you uh solve it just uh you know submit it in my mail so that i can see that what you have done okay so please try it and come up with your problems that you face otherwise it's very difficult to know whether you are getting things okay then i end this class the recording is on so i i think it will be i'll be able to pass it on to you okay thank you