Linux Exec System Call

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I'm dr. Brian Frazer and in this video I'm going to be showing you how to use the exec function call or system call in Linux done through a C program so in general what is the exec well it allows you to replace the currently running program with a brand new process so let's have a look at kind of how we can walk that through just get us started I've created a program here called hello exec which when called we passed a number of arguments it's just going to print out its process ID notice the pit the PID and via this call here to get PID and then I'm going to print out all of the arguments to the program and then simply exit so we can run that I'll make that first so I can build it with that command and then I can run it with hello exec and let's just run it without any arguments to start with it'll tell us that my pit is 57 222 90 and the only argument was actually the name that I use to execute it with that's a convention we use for our calling a program so I can put in some other things here one two so the two arguments hello and yo so we can see that it now gets all the arguments passed to it so let's use that for exec so what I'm going to do is I'm going to write another program that is going to exec to run this hello exec so let's start with that so I'm going to create a new file this is all inside of Eclipse see a previous video for how to set that up and let's call this exec demo let's see so here it is we're going to include stdio.h and hash include' I'm going to include stdlib.h as well for some of the functions I could use so we get vote int main number of arguments and then an array of our key a vector of arguments for the actual characters so I return it's good so what we're going to want to do here is call that other program so to do so I want to use the exact system call so they're going to use exec VP is the system call I'm going to use let's have a look at what that does so I can do man exec and it gives me the entire family of the exact system calls now as it turns out there's only one real system call here and all of these functions are just sort of wrappers that end up calling into it and we can see what each of them does for us to begin with let's just start using this one that it's going to do what I need and then we'll come back and talk about what it is the difference between these ones so let's set this up I need to pass in to this function I look over here I pass in the path which is to say whatever it is I wish to execute and then a series of arguments so the path is simply going to well it is a more simple way so I need to come up with the arguments first so it's a an array of character pointers so args and in this array it's got a few properties the first thing is its null terminated so at the very end I need a null that tells it when to stop now the first argument is by convention the name of the function or the probably the name of the process that we're calling or the file on disk so we're going to run hello exec is the thing we're going to run and then let's pass in something like hello and well let's go with world that's my two arguments so now I can call exec P I could put it in for example with hello exec is what I wish to execute and then I pass in args here and so while this is going to do is execute that other program st di di o and we'll build that so this is a very simple program that's simply going to run another program so if I look on disk oh yeah I'm not yet building that so I got to go into my make file and I'm going to add exec demo do the list of targets that I'm building as provide make file ok let's go back and have a look at this I'll build go on fine except here we got a problem implicit declaration of exec VP ok let's go back to a man file man exec and it tells me here this is in STD probably uni STD so I selected that I'll middle click and it'll paste it in for me rebuild now we're fine so here are my programs if I run just exec demo nothing happens hmm so let's figure out what happened here and I ran my exec demo it comes through it runs my code here it goes looking for my hello exec and then it didn't actually do anything for me so let's try dot slash hello exactly there we go and I'll just for completeness update this so the issue there was if I don't specify the dot slash then Linux is going to start looking in the sort of the normal path for where to find this well it turns out in Linux the current folder is not in the path that's to prevent you from accidentally running a Trojan virus or something that was named LS so here I need to specify that I'm interested in running hello exec in the current path so previously it was returning an error message I wasn't catching but we can see it looks like we ran hello exec just when we tarde started to run exact demo and it did that because the first thing we do is replace ourselves with someone else so let's put in some code that actually shows that we're running some this code here I got a printf exec demo and my PID is you'll print it out here and then I can call get PID so let's build that and rerun so here an exact demo my PID is 57 475 and then exec C when I've actually ran hello exact down here my other program it maintains the same PID so what has happened is on the command line when I execute exact demo that spawns a new process and Linux gives that process well in this case it was 57 475 when I call exec I simply replace the currently running process with a new image as it were so it swaps out my exact demo with hello exec so that's the whole purpose of the exec system call now we can ask what happens after this so let me go down here ah farewell cruel world let's rerun this rebuild its cruel world make make and then will rerun exact demo but we never saw that we could ask why why did we never see this well exec never returns unless as an error this is the last function my program is going to execute because as soon as it does this it replaces everything that's in my currently running image memory space the code the variables everything gets replaced by the program that I'm now trying to run which is my hello exec one little shorthand here I want to show you is that we have hello exact listed twice it's both argument zero here and I have to pass it in as the first argument here so I can just simplify my life and say I want to do args sub-zero and args so it'll be exactly the same as before exactly the same behavior okay so that's the basics there let's look at the different execs that we can execute so if I go back here to man exec we see that there's quite a family of them note that they all start with exec and they either have an L I so here are the first three have ELLs in them and the second three have P Ivy's pardon me in them I've doesn't used the v1 so let me just write here in a comment what we've got just so everyone can see it so with an L it means that we're going to do comma comma separated arguments and if I do it with V it's a vector ie an array of strings so as I pass in the arguments if I did a V here I'm passing in an array of strings if I wanted to use exec L for example then I would pass in something like exec L args so zero and hello world this you might use if you wanted to pass in values directly I like to use the array because it allows me to create these arrays programmatically so my code can actually generate the array for me and the only thing to mention is when you start to look at the P so with a P a few of these have a P these last two here and this one so the P is for path include normal search path for executable that means that if it's somewhere in say slash user slash slash bin it will automatically search for the appropriate locations this allows you to run say things like LS and so forth if you know it's going to be in the current folder you can get rid of the P so I could just run exec and it'll work the same way so that's a brief introduction as to the differences between the exec calls ok so now let's start to actually show what goes on with exact there's some subtle things here that we might have be able to overlook I mentioned that we replace the current memory space so let's create a global variable here I'm going to call it int fave num 42 and I'm going to copy that go into my hello up here I'm going to put in the same variable but let's change the number here let's say 84 and then on the first line I'm going to print out fave Nome is % d fave num so now whenever I run let's get out us make hello exec it's going to print out its favorite number but now the question is what happens between these if I have a favorite number here and then I exec does that carry forward so if I now run exact demo the answer is no it doesn't carry forward because my favorite number from my exec my parent or my initial image gets completely wiped out when I do the exec it doesn't matter what I do in here in fact all variables will get wiped out I can create an a local int fav num 152 it just doesn't matter I could then before I go through and update this I can say fave num which is going to be my local variable increment it no that's going to matter of course is bad style I've got the same variable name twice but when I exec I keep getting the same 84 so there's nothing I can do in my memory space it's going to pass through so those are all unique values in the different areas now we could ask well what about if I wanted to return a value could not my hello exec returned a value back to exec demo well the answer is they can't because exact demo no longer exists it ran and while running it decided to replace itself with another image from disk and when it did so it ceased to exist sure the process ID continued which is to say Linux the way it tracks it continue to exist but the actual code the memory space is gone there's nothing to return to so down here in my hello version when it returns this does not go back to exec demo this return actually kills the process it kills that whole process that Linux had open if you're thinking about returning a value or to turn back to whatever process forked this process but that's in a separate tutorial ok so one last thing I wanted to show is what would happen if we exact ourself so let's break a new new file and try that so let's call this one self exec dot see folder we want to put it in here file name self exec and I'm going to go and I'm going to copy my exact demo and we'll start with this add it to my make so here if I changed this to self exec well we're going to get down to here we're going to say replace me with myself and then go from there so you might be thinking well okay we'll replace me with me that's the same as me and so we'll carry on to here that's not going to be quite what happens what sorry save numb and let me go back into exec demo give her that fave number there you go so now if I do self exec we get an infinite loop now we're not looping there's no for loop here in fact we've got the same PID even let my PID stays the same but we are continuously replacing ourself with another copy of ourself we never ever hit our farewell cruel world because by the time we hit the exec command we replace the current running image with a new image it starts at main just like a brand new program and starts to execute down the line now I can do some kind of cool things with this let's get rid of some of these comments here I can pass myself a value now how do I get information into that new program that's running well use the command line and so what I can do is I can pass myself some information on the command line so let's do that so what I'm going to do is I'm going to hurry to a program that's going to count down so let's change this comment here to be what we are we're exec self exec I can check to make sure I've got the right number of arguments so if our C is less is not equal to two I can printf pass one argument as an integer it's always a good idea to check the count here before I access the array otherwise it might be accessing a invalid area of the array for the arguments and then exit one so let's get that number int N equals a two I I can convert from ASCII to integer and I'm going to our V sub 1 so that's going to give me the first argument pass the name but the real argument so let's do printf print the number and I'm going to put a line feed here I might not want to put a line feed normally but as it turns out the line feed is going to flush the buffer to the screen it'll actually see this otherwise the buffer can get clobbered first and it out and I'll print the number here if you didn't want to put the /n you might want to do a flush with an F flush to flush the file that's just FYI okay so now if we're in this sort of as recursion let's make sure we bottomed out the recursion so if n does not equal zero we're actually in our curse and here if I'm going to recurse I need to pass some value in so let's create a new I need to create the argument that I'm going to pass in in order for me to do that I need to create a string so char n minus one and let's make it a string like ten I need to get n minus one into that string so I'm going to do s printf to write it into a string so that's n minus one I'm going to write it in with a percent D and that is n minus one so that builds my string now I can do this in a number of ways I can actually switch here from V to L just to demonstrate what it is so we're going to call ourselves so let's just do Arg V Sub Zero so what am I using I'm accessing this one up here I'm going to then pass in n minus 1 and let's check the men page just for fun man exec and it'll tell us here the exec L and so forth we thought of those arguments list of arguments must be terminated by a null pointer all right so we'll pass in a null at the end here which indicates that we are done we only wanted to pass those first to end and now let's run this let's see what's going to happen what I do run it it's going to come in print out our own pid' if I don't have enough arguments it quits otherwise it prints the argument it was given I then generate a string that is the argument minus 1 so n minus 1 and I pass that in to myself how do I know it's myself because I used Arg V sub 0 our V is what I was passed and sub 0 is my name on the file system so let's try that make I'll pass in no arguments and it tells me I should pass in a number let's pass on the number 2 just to start with it prints out 2 and then it says pass 1 argument as an integer okay so we failed to pass in the value there let's go back and check out why so I created n minus 1 as my argument I called exec L let's print it out just see what we got so our to pass present s line feed it and n minus 1 our past is all one and pass in one argument as an integer that's curious well I'm not really sure why that's not working we call method up for a moment let's switch over to using the one that I have been using already let's create the argument array so I'm going to create an array called args I don't need to give it the size because it's going to automatically take the appropriate size I want to pass in to begin with Arg V sub 0 oh I know I just occurred to me this is the name that's going to get executed I then need to pass in its name on the file system so these are the arguments and this is simply what I'm calling let's try that again so self you know passing a 1 so that's good account it from one down and it's passed into three so now we can see what happens is I go from 3 to 2 to 1 to 0 each time I am Reax acute in the code we can see here we print out the very first line self exec we print out the pit it's always the same pit so I can then count down so you can imagine what happens when I call one hundred only one process has ever created and that process continuously execute new processes in fact all that's actually changing is we're changing the argument when we call ourself now you can say well what happens I have a global variable here let's call this into height equals 52 51 each time I'm going to print out printf height % D and height and just for fun I'm going to decrement kite here hei height will decrement it here so if it gets wiped out every time we're going to see 51 s the entire way through on the other hand if it stays around behind we're going to see that it counts down with us let me make self exec oops let's try passing in some number let's pass in 10 and we can see that it stays 51 every time one fact is not really staying 51 it goes from 51 down to 50 with this height minus minus when we get down here to exec we exact a complete new copy the entire thing gets reloaded a brand new program starts running in the same place and height gets initialized to 51 so it wipes out absolutely everything is associated with the process with the exception I believe of maybe open files and a few other small little details okay so that's all I wanted to show here the main part is to understand that exec will wipe out the current process and load a new process in its place and we've got a number of ways of interacting with that but it's pretty much just the command-line we can use we could pass anything we like on the command line maybe a handle to a pipe or something but generally all we've got is coming in through here everything else we had like the global variables are all wiped out as we execute it thank you for watching
Info
Channel: Brian Fraser
Views: 82,308
Rating: 4.9645667 out of 5
Keywords: System Call, Exec, GNU/Linux (Operating System), Software (Industry)
Id: mj2VjcOXXs4
Channel Id: undefined
Length: 24min 17sec (1457 seconds)
Published: Fri Feb 06 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.