Communicating between processes using signals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it really I want to take a look at how you can communicate between processes using just signal so miss blue we looked at how to send signals we looked at how to handle the signals but we only worked with signals that already did something special right they were either terminating the process or stopping the process or continuing it this time I'm going to take a look at a signal it doesn't do anything by default so to start off there's going to be a simple problem that we want to implement a symbol what they want is just a simple question to be asked by the program and the user gonna have to answer it and if they are suspect if they don't I'm sure that's also great but in if it takes longer than like five seconds to answer that question I just want that user to receive a hint how can we do that using two processes first we will create two processes I'm going to use a fork or spork and also do check your prostate if it's negative one then it can return an error code sip enough if we are in the child process then we're going to do some hinting on the side but I'm gonna leave the logic for that for later first let's create that and that question so first I'm going to ask you a question let's say what is the result of an illicit three times five now okay and I'm simply gonna scan of this variable into this variable so scan up the address of X 3% here like that and well if it's if X is 15 then let's print out two right and otherwise the sprint out wrong and that's all the program is basically going to do our parent this is the parent process and I actually say your parent process but I also want that hinting part so I want the process to sort of wait five seconds and then send me a hint how can we do that well we can't really do it here because we're already waiting for this scanf right where the parent was what executor says print out this statement is gonna print this out on the screen and then it's going to wait for our input it cannot it cannot also wait five seconds right so we're gonna have to have our child process do the waiting for us so what we can do is just simply say sleep off five there should be five seconds exactly and after it C sleeps five seconds what I wanted to do is to send a signal to a parent and a signal to a parent saying that oh you should print out a hint well if five seconds have elapsed here that means that we should send a signal to our parent here's I'm gonna say use the kill function you can function takes in the process ID so we want we know we are in the child process right we want to send a signal to the parent process we can get the parent process ID by just saying yet PPID that's going to be the the parent process ID which is this guy and the second parameter is a for the signal that we want to send now the question becomes which signal you should send and here we have a lot of signals well but they all really do something this guy sort of child stopped or terminated it does floating-point exception we don't want to do any of this it says it's an illegal instruction or whatnot but if you look down there are two signals that can be used for user-defined operations so if you're to send these signals without handling them properly they're just going to terminate the process but we can show override these and these are meant to be used only for user-defined operations here these are never used inside the Linux kernel anywhere for specific word for generic process operations right only in applications specifically developed to use these signals you're going to see them use so in this case we can actually make use of these these are sort of blank slates and we can send for example casting the CPU sr1 signal to our parent so here we can say see us r1 and that's that's about it really it's gonna send this see us r1 to the parent process but well the purpose doesn't do anything you just remember there's a default behavior for all the signals and in this case for seniors r1 what it's going to do is just terminate the parent process and that's it so this is not okay we have to we have to also handle this process using that C action that we have used before so to add the handle we simply declare here another struct so it's a sort of type C reaction we'll call it s a and again we need to have inside this s a a few things set up first is the SA flag that has to be as a restart that's just because we're using scanner and one we want to basically restart scanf once it's finished its execution it if you don't use can have when you're actually handling the signal then that's all fine you don't need to set up this blank and to make sure that all the members of the struct have been initialized to z20 for example i'm gonna set it so that it does just that so next up is to set up the handlers I'm gonna have here sa sa I'm just calling Handler and this should be a pointer to our function that we're going to have to create so this is let's say let's call it handle underscore see us are one okay so that's going to be our functions name which we're gonna define up top here so I say void that and that's the signal and lastly what we have to do is actually bind this this SIG us our one signal to our handlers and say cg action now the function sig action that takes in the signal we want to buy which is CG us r1 we want a reference to our s8 or sig action struct and then the last one is going to be null because we don't make use of it the old sig action and that should have bound this whenever we send this it should call this handle see us or one function now now what do we do in here simply we print out a hint hint for the user to read so you can see here they remember that multiplication is a repetitive addition and the back second so that and I think we can actually note it as a hint here and see in in just like that and now that this handle is actually handing see us are one in a special way it's no longer gonna just terminate the process and of course I think I forgot is to add a weight here at the end of the parent process so that it waits for our child process to finish ok so now if we try to launch this by first building the project and then launching it through the terminal again I don't want to launch it through the IDE because I have it set up that it will launch it with gdb and it's gonna cause issues so here we can just say dot slash main after compiling it and you notice it asks me the questions I can answer it 15 and then hit enter and you'll notice that I still get the message on the screen even though I did say 15 here and it did say that it was right it did give me the hint and then it finished executing so how can I stop it from doing that well the quick fix would be to first move the X variable to be global like this initialize it with some value now we have it already initialized with zero because it is global but I'm just gonna make sure and be specific about it and then we'll check if X is actually zero if it is then we know that it didn't actually change so we can print out this message on the screen otherwise we just received a signal and we don't do it anything with it so this way we should be able to not print out this message again and also because I noticed that here see here's our message with the right and then right after is our hint I would also like to add a new line right or like right before the message so we know it's on a new line on its own now if I try to first compile it and run this and clear and then don't start main say 15 and then it should actually print out right and it's no longer gonna print out anything else on the screen but of course if we clear it again and launch it and we wait five seconds it should print out on the screen saying that well we haven't read anything and sure enough we get the message and then we can save 15 and it's gonna say right okay so now we know that this works as you can see this is done by waiting in a child process it's sending these figures or one and a CBS R one is handled in the parent process like so and this function is being called and it's printing out on the screen so this is how you can communicate between processes using just signals but as you can see I didn't send any data from the child process to the parent process because with signals you cannot send any data they are just signals they are just sort of a notification with no information just number so because of that it's not easy to communicate just with signals and another important thing that I left out is that these handles that are handling signals they are really they should be very atomic in the sense that they should really modify global variables they shouldn't be read or write to files or even print out on the screen something remember you can actually have this function executed in between function calls and really anything could happen there so it is recommended that you actually pay close attention whenever you're using these handouts in our case it's fine because we're just using C us or one and it's our own little project here we're just trying it out but for the most part they have to be atomic operations okay that's about it for this video I hope you got something out of it if you do have any questions again there are comments below or on our discord server you can join and ask in there thanks much for watching take care bye you
Info
Channel: CodeVault
Views: 13,182
Rating: 4.9881306 out of 5
Keywords: signal, sigaction, sigusr, sigusr1, sigusr2, c (programming language), codevault
Id: PErrlOx3LYE
Channel Id: undefined
Length: 11min 17sec (677 seconds)
Published: Tue Jun 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.