Handling signals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in a previous lesson we looked at how to send a process to the background and then back to the foreground in today's lesson we'll take a look at how to handle signals and namely how to prevent the process to be sent to the background at all how can we actually learn first we have this program right like we had in a previous lesson it's the same one we just have an X we print out a prompt we read it and then we print a result so again if you want to refresh your memory I can just type in 12 here I'm just gonna get results 12 times 5 equals 60 so I guess that's obvious now how can we make it so that whenever I do something like this whenever I launched it from the terminal whenever I launch it here and say control Z it doesn't say has stopped and doesn't do anything or does whatever I want it to because remember when saying when hitting control Z that we send a signal to our process and we want to handle that signal differently how can we change the way we're treating signals first let's include signal of age that's where most of the signal related functions and structures are so the first step is to instantiate here a struct struct called SIL action and we're gonna just turn in it as a for short and inside this SIG action we can tell it how to handle the signals so first what we want is to set up the function that is going to be called whenever we receive a certain signal so I'm gonna say here s a dot s a underscore Handler and this guy is going to be a pointer to a function so we don't get it give it a reference to our well not yet created handle let's say CGS DP and of course this doesn't exist yet so we're gonna also create it and it's the signature is very simple we have void it doesn't return anything we have the name and then it takes in the signal that it receives we actually handle that's all this does and suppose what I want to do here is well here we are handling that sigdi STP signal that we should be receiving to actually stop the process but if we are changing the way we handle it we are actually no longer stopping the process so let's print something like stopped not allowed why not and the backslash n here and next up is we have to hook this SIG action to the proper signal because we did not specify here in any way the signal so that's where you have to call the CV action function so cg action it's a function it takes in the signal so here we give it 60 STP and then it is the cv action strong that we have created up here it wants a reference to it because it does set certain stuff in it so we want to give it a reference to that as a and then the next one is if we had another C action set beforehand it would save it inside the third parameter so that you would have the old cg action which is kind of neat because that provides you a way to restore the handler if you need to but in our case we don't want to we don't care about it so we just set it to no and one last thing and this doesn't really have to do with all the signal handles you just have to do with this one specifically I want to set inside this SIG action struct s a dot and there are a few more options that you can set in here what I want is to set the SI flags parameter to say equals si underscore restart and that is just because we're using the combination of 6t STP right here and Kenneth okay and because they are doing something very strange behind the scenes it doesn't work well if we don't provide this flag all right now what we can do is to try to run this so again I'm gonna run it from the termina I don't want to run it from the vs code because mine is set up to run gdb and that's the debugger I don't care about it right now so I'm just gonna build and in here just gonna run me right I'm gonna get input now if I type in here 12 I 4 so I'm gonna get a proper result nice but what if I run main and then hit control Z basically sending this 60s TPS signal to our process what failed at well you will notice that it says stop not allowed in fact that we executed our function over here and if I hit it multiple times you'll notice that it keeps doing this so we cannot have send this process to the background anymore because we have handled this this signal in our own way by just printing that message on the screen that's all now well we cannot send it to the background so we might as well just execute it with a 12 and then it's gonna finish each execution and that's really all there is to it if you want to handle signals in your own way basically as we have seen here with customize the way this signal works by not actually stopping we're just putting something on the screen you can do that in C and it is very nice I think using a struct like this and there's a lot of configurations you can do with this SIG action and then of course you can get the previous cg action that was set inside this third parameter now before I finish I want to take a look at another functions very similar to this this is CG action there's another function called signal and this is probably more likely that you have learned about by your teacher at school or something like that so this function is signal and only does it takes in a signal so our 60s TP and then the handler so our handle 60s TP and that's it but we don't have to create any struct and if we try to launch this if I build it first and launch it in Maine then if I hit control Z it does exactly the same thing now see why would I not present this function to you and instead show you the longer way of doing it that is because of the manual page so here's the manual page for signal for the signal function right and in here the first line the description says the behavior of signal varies across four unix versions and has also varied historic across different versions of Linux avoid it use use CG action instead so for portability reason is basically for having your code run on any version of UNIX you should actually use CG action instead of this signal function and in my mind it's not too much of a hassle to actually do this and it is in fact more convenient because well if you want to restore the previous functionality of the of the signals handlers it's gonna be much easier with this function because it takes in this third parameter which is nice now let's actually play around this we have this allows the process to actually be stalled let's allow it to be stopped all right so let's let's comment this out let's not bind to any handlers and of course if I build it and launch it again with main and I say control Z it's going to stop and if I say FG is going to appear here now what if I want when I say FG when I bring it back from the background to the floor four grounds to execute in to print out something what if I want that prompt again right because that prompt is gone as you can see it's actually been replaced here and it's no longer here and I would like to know what it's asking me for I don't know it's a number it's a string it's what in fact you actually do that with signal handlers so we can actually change this so instead of doing this print was stopped not allowed for CTS TP we can set this source less truly let's make another function say C Conn's so you can handle sig cone because that is what is called when you use the FG command and instead of printing this let's print out that input input enum sentence so the same one that we have down here now okay and you don't really need to have the same exact name as your signals that you're handling so this is just my convention you can use whatever names you want I just like to have them this way because it's easier to know usually gonna have one function per signal and here I have to change this to see cond right so the same so different Handler and I want this you still be 60s TP I want this to be seed haunt and one thing I missed is well just to make sure the line is printing on-screen I'm gonna actually flash the standard output so it does print this input number right when we execute this and doesn't wait for a back session or something strange like that now let's try to run this so it's for the build it so shift B and if I try to run this now we get input number nice name if I say twelve of course is going to work but if I run this and then put it in the background it's gonna say main has stopped but when I bring it back to say FG you'd see this prompt again this input number and this input number comes from this these two lines okay so this is saving very nice and now I know what I need to print this is nice I can say learn okay 20 and voila we get the result and the program continued its activation now you might notice a difference here we've sick conned well with sixty STP when we change the handler it did no longer stop right but with this guy it continues its execution why is that well that's just because see quantity sort of special in the sense that while you can block it you can handle it differently it's still going to continue the execution of the process so it's still going to do the same thing as before except is gonna do some extra stuff that we added here and you'll see that for certain signals is going to do that for other signals it's actually you're actually not allowed to handle them yourself and yeah that's that's just for things things are in lyrics so I hope you got something out of this video if you do have any questions please leave them down the comments below or on our discord server thank you so much for watching take care you
Info
Channel: CodeVault
Views: 18,247
Rating: 4.9805355 out of 5
Keywords: signal, sigaction, sigstsp, sigcont, c (programming language), codevault
Id: jF-1eFhyz1U
Channel Id: undefined
Length: 11min 44sec (704 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.