Executing commands in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in a previous video we looked at how to execute any program in our system using the exact family of functions that video you can check up top with which we could just execute any program giving its path its arguments and of course we can specify if it could use the path variable environment variable and if we wanted we could also give it some environment variables ourselves ok so that was amazing there was a really big caveat though and that was that whenever you would execute that exact function not only your program would like stop executing your process would be basically replaced with whatever you have called right so it would be non-existent so let's let's say here we want to call a simple ping right so we want say exec I want a list of arguments to pass in I want it with the path environment variable ok so exactly LPFM I use I just want to give it a simple pin here and as I said the first argument should be the ping then we want to painlessly to google.com and the last one should be not the last element inside this list now because I'm on Linux I don't want this thing to go forever so I'm gonna actually limit the number of counts so I'm gonna actually pass in another set of arguments so means - see here that tells it how many times it should be google.com and say here this is just three times that's that's all we need ok so now we should be able to execute this simple pink using order C program so if I launch this well we should be able to see as you can see down here ping google.com three times as you can see that was eight min second or 15 milliseconds pretty fast it's fine okay so that works but what if let's say we wanted to print here and I say printf success so if we know on the console that okay well everything smoothly we executed this program and then we wanted to do maybe some post-processing right so after the program has finished we have to do something with whatever it did behind the scenes can we do that if we launch this well you'll notice I get the ping so one two three but as you can see I don't get anything else on the screen so this success which I should actually add an exception here at the end did not print out okay so if I don't execute this it is going to print out right because it's just a simple printf and sure enough we see it now and this is what I mean the exec functions what they do is they not only just stop execution then return back nicely no they actually replace your whole process so all the memory is going to be replaced by its own memory the execution line is going to be replaced by its own line you are left with nothing myself after this exact function well we were done so this was a pretty big caveat now we know how to create new processes we know how to fork a process using this information we can actually call this and also execute something after it if we create another process for it so the main idea is to first fork so I'm going to say here let's say int pids from process ID equals fork and well I'm gonna arrow check of course process ID equals negative one if that happens just return on and we know that something bad happen after which we should have two processes executing the same line of code we have a parent and a child now it's important which one we choose to execute the exec function on because remember we've processes it is important to wait for the child processes right but if we if we call exec on the parent process well the parent process gets replaced and it can no longer wait for the child process that may do some post-processing so that cannot happen you cannot actually call exec on a pair process and expect it to work properly the charge process is going to be actually a long be processed at the end it's not going to be its memory read so because of that we should call exact on our child process and inside the parent process what we have to do is wait for that process to finish in this this way at least this case will be waited for and the parent process can include something after it how to do that very simple so first we check if process ID is zero that means that when were in the child process file in here what we do is just pull exactly so in the child process whatever we do here after the exact does not matter gets replaced but now we have another process where the parent process that still executes code it did not execute this exact but because it speed was something different than zero so on the else branch here with the parent process what we can simply do is say receive weight of null so I want to wait for all any child to finish its execution we all have one childhood fine and let's say print F some post-processing toes here max of n and if we try to execute this now maybe yeah maybe we can also copy this success here why not just to have it be everywhere in the video why not if we try to run this you will notice that we ping so we get the pings we get 3 pings here of 64 bytes is fine says okay well those are the statistics and this is where the the pink process ends so once it ends the parent process comes on and says okay well we have waited for you you finished your execution so now we print out success and some post-processing goes here and there should be some possible that's going on here that you might want to do let's say well a more useful application of this would be let's say to create a sort of special file you could technically if you wanted to you could create a fee for using this you could say just make FIFO in here and just pass in the name right so you can do here and that and sum or something and that would create a fee for using just Linux commands instead of C functions right now something really important to take away from this is that the child process do you have some some magic properties in which it kept on executing the child process got replaced by this pink process okay so because of that replacement because of that replacement we no longer have a child process that we can program our own we again have our execution line and our memory replaced by exact so everything after this execution is still going to not work so if I print up here say this should not print on the terminal say black session if I launch this you will notice that I don't get this message on the screen that's because the exact did actually replace everything in the child process so here in the childhood got replaced but because we didn't execute the same code in the parent process we actually just waited for the child process it did wait and we were able to do some post-processing here that's all I wanted to show in this video in later video we're going to take a look at how to communicate with these processes these commands so if we do ping how can we get its result if you do a certain command how can we get its output so we know what's what's going on or how can we send an input to a Linux command through AC program that's what we're going to take a look in later videos I hope you got something out of it if you do have any questions do live internal comments below or on our Dysport server by you
Info
Channel: CodeVault
Views: 10,698
Rating: 4.9698114 out of 5
Keywords: exec, fork, commands, terminal, c (programming language), codevault, execute, executing
Id: uh2FqejQRk8
Channel Id: undefined
Length: 9min 2sec (542 seconds)
Published: Fri May 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.