Process IDs in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now if we looked at how to create multiple processes and wait for one another I think the next step would be to learn what our process IDs so what are they well as the name implies they are just IDs so identification numbers for processes and this applies to all processes in Linux basically every single every single process has an ID a number that is unique for that process in this case what we can do to get that ID is first include a hazard file called include here C's slash weight dot H in here we're just working the current processor if you get two processes and if we for example do something like print F % D and we call a function called get PID so that's just gonna get the process ID for our for our current process that we're in and this guy is very simple it simply returns a PID underscore T that usually an int so we can just use a % D here and it doesn't take in any parameters so if we call this let me actually add a backslash n here so it's gonna go alright if I launch this we should get of course two IDs here on the screen we get three seven two seven and three seven two two okay we can probably do that this is the child process that was created from fork and this is the parent process that we have launched with our ID here now another thing you can do is get a parent process ID so you can say say current ID current ID and then parent ID % D mexicana and these here and the second function if we're gonna take a look at this called get P PID and that's basically the same as this one except it returns the parent process ID so now if I try to run this you'll notice I'm gonna get both the current ID and the parent ID so we're gonna see that three seven eight four here each parent is three seven seven eight and here is the free surface of an eight process so this is the main process that has launched this guy this three seven eight four and each each parent ID is three seven seven two basically every process in the system has a parent process ID that has launched that process except the process with ID 0 that is the main process that launches everything so in our case our three seven seven eight has been on five three seven seven two which is I'm not exactly sure what process that is probably created by the editor now one of an interesting thing that we can do is well remember in the video in the previous video we talked about waiting from for one process to wait for another in there we in the parent process we have waited for the child process right because that's usually how processes should terminate so you always the child processes should terminate first before the parent process terminates if it's the other way around something more interesting happens and let's try and do that what we can do here is well remember the ID is zero for the child process what you can say is if ID is zero instead of doing what the parent process does also wait like I know one second I think one second should be not so that basically what's going to happen is both a timer start at the same time but since the child process is going to wait the pan processes contaminate okay it's going to get to return zero in Terminator process completely they're going to get to this print statement that's gonna also give the pattern I D now the question is what's the parent idea of that process so if I launch this you'll notice that after the act is actual and aligned by the editor I get the last line here the first line is for the parent process right the parent process has 3/8 for free and its parent that is 3/8 3/7 okay and the child process here is its ID is 3/8 4/8 so it's cleared up the child process because it's ID is bigger than this guy but it's not necessarily the case that it's always the case but for now I think we can conclude that and the parent ID is actually 1077 as you can see here because well the parent ID was 3843 but it actually terminated so because it terminated with having still child processes running well a new parent process has been assigned to that child process so that no so that we don't have processes that don't have a process ID or have their processes actually terminated because our parent process died before the child could terminate its execution it got assigned a new parent process ID and that is for my distribution of Linux from my implementation was 1077 that's actually the systemd process but in some cases is going to be of course different ID in our case is actually going to be the process with ID 1 and so on and so forth the main idea is that you're going to get a different parent process ID if you do that and it's considered a zombie process because if you try to if you try to kill it with a sink kill command it's not going to actually free the memory okay so you act you have to really wait for those that are your child processes otherwise the memory is not going to get freed and you're going to have a memory leak so really what you want to do in all your programs that use the fork function is actually wait for them for the child processes to finish before you terminate the main process right so here if I D is different than so if we are in the parent process then please wait I'm gonna call here wait oh no I know in the previous videos I mean I didn't actually pass in a parameter here but one parameter that you can pass and I have to pass it because I actually included way too dot H is a pointer to something that tells us what's what's happened with that with that process now interestingly enough you can actually call weight without even checking if it's a parent because if it's not a parent like this it's still going to properly finish execution of the problem if I do launch this it's still going to wait and finish the program accordingly so I'm gonna get here the child process 4064 and parent 4,000 57 which is here okay so you can wait because what this is gonna do is going to check if there is any child process to wait for and if it isn't it's gonna just return I think negative one so if I see here if this is equal to negative one then let's say printf no children to wait for and back second and if I run this I'm going to get this line of text once here it says no children to wait for because there really wasn't any to wait for and it just tell me it like so but interestingly enough this wait call actually does return something pretty useful aside from just negative one if it errors if it doesn't error let me tell you else properly we can actually take in that result so I can say let's not actually the result therefore result equals weight of null and I'm gonna move this file here so if res is negative one like we had before it's going to still print this on the screen but if it's not negative one then what I'm going to say is percent D finish exact you son and backslash n I'm gonna plop in here the res result here and if I try to launch this you're gonna notice that I do get still these two lines me and I say no she won't wait for that's probably for the child process and the parent process said 4179 finished execution so really what you get from the wait called here is the process ID you actually waited for right and this is very useful especially when you're dealing with multiple child processes right so do keep in mind that you also get a process ID here or negative one if you don't have anything to wait for and it's what I wanted to show you in this video really just about process IDs every process has an ID every process has a parent ID and how it gets switched if the parent I did or the parent process doesn't exist anymore okay so do keep in mind this and usually you should actually call wait at the end of the program if you heart forking it anywhere okay thank you so much for watching I hope you got something out of this video if you do have any questions do leave them down comments below or on our discord server take care bye you
Info
Channel: CodeVault
Views: 15,209
Rating: 4.9925928 out of 5
Keywords: process, getpid, getppid, c (programming language), codevault
Id: PZrQ4eGm-hM
Channel Id: undefined
Length: 10min 14sec (614 seconds)
Published: Tue Apr 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.