Background and foreground processes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
before we go any further with signals I want to teach you about the concept of putting a process in the background and bringing it back to the foreground to do that we're gonna create a very very simple program that all it does is if toast variable X then it prints a prompt says input number four it reads that variable into X whatever we type in here or the ends of X of course and what we do is just print F a a simple operation so result sleep force entity let's multiply this X by a node by five okay why not and I'm gonna say your percentage it backs a friend no type the whole operation so we know what it is and the first parameter is the X and the second part there's going to be x times fine that's all there is to this program and well if I launch it it's going to be very obvious what it does if I type in here for example 10 we're just going to get the result 10 times 5 equals 50 so simple enough now in a previous video we looked at how to stop and continue a process bringing it to the background bringing a process to the background really means actually stopping the process and to do so in the terminal here so if I go to the terminal and actually manually execute the program say dot slash main that's our executable if I run it of course I'm going to get the same prompt and I can type in 12 and then that's going to work and get 60 but if I run it and instead of actually typing in a number I say oh I have to go on my lunch break so I'm going to actually stop this process I can hit ctrl Z on the keyboard and as you can see says dots - main has stopped now that has stopped doesn't mean the process has terminated there's a big difference between the two termination means that the program reached a an exit call or it reached the return statement inside its main ok so it basically finished its execution is done all the memory as it has been freed and were done but stopping a process is more like closing the process well it's like in a state where it's it's stuck at a certain line of code which ever happens to be and it waits for a signal to continue its execution and we saw in the previous lesson that it is the sig cond signal that it's waiting for so that it can continue so in fact when I hit here control Z a signal was sent from my terminal from what I'm using is here is the fish shell to this process you said okay you're going to stop your execution your very powers your execution and it did indeed say stop so now I can bring it back from that from the state by either sending in a sequence signal or just typing in F G which does the exact same thing if I so if I type in FG here in the terminal and presented with no parameters you'll notice it says send job 1 and the job was dot slash main and it sent it to foreground so now it actually sent the sequence signal to it and it continues execution so now it's actually waiting for us to input a number if I type in for example 10 and hit enter you'll notice actually get a result on the screen saying that the result 10 times 5 equals 50 so this is how you can actually move a process to the background and then bring bringing it back to the foreground let's take a look at what happens behind the scenes with those signals and whatnot so suppose that this is our process this line represents basically a timeline of all the lines of code that we have in the in the program and scanf I marked here with red and this is where the program sort of waits is just kind of he did print on the screen that message with input input number and actually let me move this way to the left and then here a white text so it did print this on the screen and then it's waiting for our input and during the time where the process was waiting for our input it actually received a signal so right here right in this while executing this line of code it received the signal called seed tsdp and this is a sort of it is very similar to the stick stop signal that we have used so don't worry too much about it the only difference that this guy can be worked with you can sort of ignore it or you can handle it in your own way where you get to that but for for your understanding this is basically the same as the sick stop with it did receive this through our own command right in the terminal what I typed in here was actually hit control Z and that is the character sort of sent to the terminal and terminal was like oh okay so you want to send this signal to the currently running process and it indeed send it okay so right now the process is sort of in a state where it does no longer listen for the input it's stopped completely it doesn't do anything until we restarted it until we bring it back to the form at this control Z sends the program to the background so whenever you accidentally use control z thinking that it would undo your changes no that's not what it does it actually changes it so that it in it's running in the background actually sends a seed tsdp signal stops the process remember stops doesn't terminate it so stopping in Linux processes is basically like pausing it and well now it's there it's waiting for it to be restarted to be continued and of course to continue the process what we do is again the sin that sick signal right exactly the same line we left that here is called secant that is the same signal we used in the previous lesson and this will restart the process okay and how did we send it well we sent it through the F G command from the terminal so this is a this is a terminal command that says okay you you should send a secant signal to whatever process you have in background I think that the background processes or like there's a list of breaking processes and it is per terminal instance and to this command well it just takes a look at the last post or stopped process and it restores it back to the foreground but you can actually specify in here it's the process ID or in some shells you can actually specify the job ID as it's called so now right after this is sent it continue continues its execution nicely and then we can well type in the number 12 and it nicely shows us six again if I get back to our code here and if I try to learn and launch it manually because I don't want to the thing is we've launched it manually and launching it through vs code is that vs code actually uses gdb and I don't want it to use gdb I just wanted to launch the program so then just launch main here and of course you're just gonna ask us for a number and if I again hit control Z this will send that that sig tsdp so let's top command it has stopped that doesn't mean again it doesn't mean that it's actually terminated and well now it's there in the background process list and if you take a look at top or H top you will see it still they are not running just sort of stopped and to bring it back we just hit we just use the FG common and as you can see said send job one main two for now so basically sent that seek cond signal to that process and now it's back and it's again waiting for that input and if I want to I can send it back to background so I can hit ctrl Z again it's again in the background and as you can see if I type in 12 what actually is recruiting a terminal command right we it's not listening for our input but if I hit F G again to bring it again back from the background to the foreground it did send us it is send that signal and I can say 12 and voila I get the result on the screen so this is what happens when you hit control Z on your on your processes and this is what this is how you can bring them them back from the background in a later lesson we'll take a look at how to handle these stop signals but for now I think this is enough and I hope you got something out of this video if you do have any questions leave them down comments below or on our discord server take care bye
Info
Channel: CodeVault
Views: 4,199
Rating: 4.8980894 out of 5
Keywords: process, background, foreground, fg, c (programming language), codevault
Id: 7ud2iqu9szk
Channel Id: undefined
Length: 10min 10sec (610 seconds)
Published: Fri May 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.