What is waitpid?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're going to take a look at at a function called weight peed and wait what it does it's instead of just waiting for whatever child process to finish execution it's going to wait for a specific child process to finish its execution uh given we have the process id of it so we're going to create here two child processes that's going to be very straightforward and here they are and inside each process what i want to do is in one of them i'm going to wait uh a certain amount of times i'm going to say sleep of let's say 4 right and after that i'm going to actually print out a message let's say finished execution and in parentheses i'm going to print the actual process id so i'm gonna have here pid one or no not one but get because speed one is definitely zero in this case so i'm going to get the process id using get peed and i'll print printed on the screen and same thing here with the second process that i've created down here but i'm gonna make it so that it only slips for let's say one second so it's it's uh one of them is going to finish much faster than the other of course and of course i should wait for them so if i just try to call weight of null twice that's fine so now if we run this we can actually see that first is the process that only takes in one second to execute and then the process that takes four seconds to execute let's print on the screen uh if we print out which process we have waited for using the return uh value of this weight so we can say here uh int bid 1 result equals that and let's say printf weighted weighted 4 percent d backslash and bit 1 result and if we do the same here hit two result instead and say bit to result here we should notice that if we run this first is the process that finished execution first right and then it finished waiting for it right it printed out its process id and then the second one that took four seconds to execute did actually print out here now what if we want it to be the other way around what if we want to first wait for the longest running process which is this one with four seconds how would we do that well that could be done using weight speed instead of weight we can call here weight speed and it's going to return the same thing so don't worry about that but it's going to take different arguments so first things first is the process id the process id that we want to wait for is well in our case speed one so that makes sense we want to wait for this process that takes four seconds to execute okay the second argument is the same as the the argument for weight so that makes sense and then the third argument are some parameters that you can pass in uh here telling it how to wait actually and uh we're not gonna go into that and just passing zero right now and similarly for the weight below but for p2 of course so now if we try to launch this you might notice that the finished execution process does print out but no longer do we wait for it so only after the second finished execution only after this process finished its execution we actually print messages for both of those uh weight speed calls right so this is a bit more different now if you really do care about the order of waiting for those processes then uh this is the function that you have to use some other nice things about this weight fit function is that the p value could be also negative it could be zero it could be negative one so if it's a negative number instead of waiting for that specific process id is gonna wait for uh that specific process group id right and usually most processes have the same group id unless we create it with that fork in that case they would have all the same process group id so that's one way to use weight speed if you're passing negative one is the same as using weight it's gonna wait on any child process uh if you pass in zero it's gonna wait on any child process with the same group id process group id and again if you're passing a number that's higher than zero it's just gonna wait for that process id as we saw before the more interesting thing that you can do with weight speed is that you can pass in here as a third parameter a certain flag and that flight could be w no hang and double no hang uh means it's not gonna wait for the process to finish execution it's just gonna check if it finished execution then well it's fine we're gonna return like we did before but if you didn't at the time of calling this weight peed then it's gonna return straight ahead and it's gonna save the status inside this second parameter that we pass in here telling us that oh this process didn't actually finish execution it just uh we just call it with wno hang so now if i try to launch this you can notice that the main process finishes execution before everything else and as you can see we're gonna get zeros here simply because it didn't wait for anything it just uh uh checked it saw that no process really finished execution yet and it was like okay well then continue and uh there's that so this is one thing one way you can use w no hang or like weight speed wave so if you do weight beat uh negative one no no hang then that's the same as doing weight of no but instead of waiting you're actually going to return straight ahead if there's no process that has terminated in the meantime and that's about it uh with this weight bit function it's very simple it's kind of niche you don't usually need to use it but i got a few questions about it and decided to actually make a video on it so if you do have any questions even down comments below or on our discord server again uh the source code for this lesson is going to be found on our website link in the description below take care bye you
Info
Channel: CodeVault
Views: 6,883
Rating: 4.9766083 out of 5
Keywords: codevault, c (programming language), wait, waitpid, pid, fork, process
Id: kCGaRdArSnA
Channel Id: undefined
Length: 7min 4sec (424 seconds)
Published: Fri Jan 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.