Getting exit status code in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
as a continuation to what we looked at in the last video which you can check up top where we try to just execute a simple program the ping program in our own program while also being able to do something else other than just executing it using Fork so we have here a fork and then in the child process we executed this pink program but in the parent also we just waited for the pink program program to finish its execution and then we did some post-processing now I didn't look at how to check if this program that we launched a actually succeeded okay so if we launch the program how do we check if it actually succeeded or if we got some Arabs well there are two types of Arabs here one is if the actual ping program does not exist is not found or two if the actual ping is found is executed but it gives out an error how can we treat each of the error types here well the first one is very simple what we have to do is remember that exact in fact does basically replace all the code and all the memory of our current process only if it's successful only if it's successful in finding the program that it needs to execute and so on and so forth if it doesn't find it if something's wrong with the name of the program that it does in fact not replace the code and it continues its execution okay so it in fact will return a an integral that represents well whether or not it was successful so here we can say error equals the return value of this exact and if error is negative one which is most likely gonna be I think it can actually return anything other than negative one because well if it's successful just replaced and nothing executes after the exact function so that's what happens but if well we're gonna just be safe and actually check if it's negative one if it is we can just printf would not find program to execute next to shine or something like that and just return return zero I got so as you can see it is actually ping to google.com or K 64 bytes it it took like 15 milliseconds it works and then we do print out these messages after the ping program works that's nice but what if the ping is actually wrongly spelled say I type in ping R for example and if I launch this what's gonna happen then well in that case you'll notice that we do indeed printf could not find a program to execute so in this case it didn't find a program to execute so it did not execute it did not replace the code and it just continued its execution now then what if it is successful in finding the program but instead let's see we don't type in the right domain name and let's say we type in google.com let's say if we try to launch this you're gonna notice that we do get in the pink in the pink program we do get a message saying that the name or service is not known of course doesn't exist it doesn't know how to ping to it but we just say success here even though the ping did not succeed how do we know it's in it how can we find out if it did succeed well that answer lies in the weight function so remember every time we call the weight was with the null here with an old parameter and there was it in fact this this parameter tells us some information about what happened with the child process okay so it is an int pointer so we we actually declare here unpinned let's call it weight status and we pass in a reference to it w status okay and after this has executed something will be populated into here that will tell us what happened but we have to make use of some macros to be able to find out now there are two checks that we have to do with this W status so after it finished execution we have to test if the if the code if the program just simply terminated on its own right so nothing actually occurred there was no external signal for example when you when you for example in the task manager and try to kill the task you wouldn't be a normal termination of the problem we'll just something external would just terminate the process similarly in Linux you can do this you by sending the sync kill signal well you have two signals in a later video but just keep in mind that this sick kill basically if you send it to a process it just tell me the process well not normally so to check if the process terminated normally we can say if and we can call the gif or W if exited macro this is a macro that takes in the W status so if this guy this w if exited returns true or returns something different than zero then we know that what happened here was normal termination so the program finishes execution normally next up we can actually take the status code that that pink program exited with so you can say here int status code equals W exit status of W status so this is going to return our status code what is that status code that I am talking about that is the return value inside the main or the value that it exited with using the exit call so remember this this is our status code this is what we're returning so here we are telling the ER thing ok that program executed properly here up top we're returning one saying that the point did not execute did not finish properly and something bad happens in fact every single Linux program has that has that returned at either in the main function or it has an exit call of one these two are basically the same it's just that you can call exit one whatever you don't have to have it inside the main function anyway in Linux a exists code that is not zero signifies an error so here we have one as an error or here we should probably type in instead of returns or in return to because it is an error in fact and here we have written zero so we can say status code is this so if the status code is zero then we know that it was a success in executing the program otherwise we can say print F failure failure backslash N and yeah that's about it we can leave this printf here why not but this status code should really always be zero for programs that have executed properly if it's not it's an error so we can if we try to run this now you will notice something interesting as you can see it did say that the google.com name or service is not known in ping so that's fine but here we have printed failure this is from our parent process so indeed the status code that we got was not zero we can actually check which status call we got so we can see failure with status code percent D and we can have it here status code and if I try to launch this we should get I think it was a status code - now this - here doesn't really have any intrinsic meaning for ping is just saying that it is just another error if we were if we were to get one I would mean that we didn't get a response from the server this is another error thing that we probably did not actually even get to the server because again google.com with an N and E and does not exist so it doesn't know how to ping that and again instead of co2 from the ping doesn't mean that it's the same error for other programs right so two for ping means other errors but for other programs with your own program that could mean a specific error saying something went wrong with I don't know opening the pipe or as T's for us here something went wrong with executing the child process I would that's the tool at it it's in here but these two shouldn't be confused with this one these two is actually from the pink program this is actually from our own problem okay and of course if we do change this back to Chrome so it says google.com and it is valid if we launched this what we are going to get is a proper success message that means that the status code that we got here was in fact zero and if it's zero everything went smoothly and we did in fact get some pings to google.com there are all sorts of information you can get from this W status I'm going to leave a link in the description if you want to check it out otherwise I hope you got something out of this video if you do have any questions please leave them down a comment below or on our discord server take care bye you
Info
Channel: CodeVault
Views: 7,570
Rating: 4.9798994 out of 5
Keywords: status, code, exit, c (programming language), codevault
Id: DiNmwwQWl0g
Channel Id: undefined
Length: 9min 42sec (582 seconds)
Published: Sun May 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.