Short introduction to signals in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
another communication method between processes that it used all throughout Linux is signals unlike pipes where you could send say an array signals we've signal as you just sort of send a notification just a simple integral that says to the other process what it should do and that's it it doesn't send any more information and that is used to sort of take control of that process so for example if I have a process and I want to pause it to pull its execution well I can actually do that using signals now see how we can send such a signal to start off we're going to actually fork our process and we say PID equals fork and if our PID is negative one then I'm just going to return an error code okay and then if we are in the child process for PID is zero I want to something not amazing it is just an infinite loop of printing sentences on the and I don't want it to print out at maximum speed I just let's say I want 20 times a second to print on the screen so I'm just going to sleep I'm gonna call the you sleep function and this guy takes in the number in number of microseconds it should just pause its execution I'm gonna give it here sees microseconds our one millionth of a second I'm gonna give it 50,000 millionth of a second that that's really in 50 milliseconds and that should be exactly 1 1/20 of a second this use the function you can find in uni STD dot H all right and let's say in the else program what we do is just simply wait off in all that's all now this is a bit problematic because the program just continues on printing on the screen forever and ever and ever and ever ever and ever and it never stops so the both the parent and the child are just gonna get stuck so if I try to launch this well you'll notice that you're just gonna keep on printing on the terminal here even if I try to terminate the process here I'm just going to terminate the parent and the child is gonna the child process gonna keep on printing on the screen so I have to go to H stop and actually kill the process now what I just did basically terminating that process from the H stop command can be done programmatically in C and you can send signals even here to do so we're gonna need to include first signal of H some input here signal dot H and this guy gives us a simple function that we can call on the parent to kill the child process that just kind of started executing in an infinite loop to send a signal to another process we can call the function named kill and a be the name is strange it doesn't necessarily kill the process it just sends a signal and then the process that receives that signal knows what to do with it where is to terminate itself or positive solution or whatever else it might need to do in this case what we're gonna do is actually terminate the process so we're gonna first have to pass in the process ID that we want to send a signal to so well we know the process ID is the speed here because that's what we're receiving in a parent process and next is the signal at we want to send and that is we want to send a sig kill signal that should actually terminate the child process and now to make sure that the while loop runs for a while I'm gonna wait for a second using the sleep function that takes in just seconds here and in say sleep of one so wait one second before actually killing the child process which should print about twenty lines in the terminal so if I try to launch this now you we'll notice that we're gonna get some lines of code of text printed on the screen and then both processes actually finish their execution with this we were able to actually stop the execution of a process that kept on executing something forever and ever this is useful when you have something like a an event handler that keeps on listening for things to receive forever and ever until someone says the seek kill signal or other types of signals now a really important thing to notice here is that receiving signals did not depend on the order of the execution of the code so right with pipes we had to actually hit a line of code that said read or write or something specific but in here even though we were in a wire loop the signal got handled and the process got killed right so these signals are sort of outside the the code they are executing so whenever your execution code if you receive a signal the process just pauses there for second and execute that the code that needs to be executed when a signal is received and in this case it actually terminated the process so nothing else was printed afterwards but suppose it you could actually send different signals telling it whatever you want and after it is handled it could continue its execution now there are all sorts of signals in Linux here's a list of them you don't really have to know all of them you don't really have to use all of them but here they are and some notes here you can see the sig child process which is actually the signal that is sent from the child to the parent when a child actually terminates and there's a few other ones that we can take a look here here's sig kill that we have used to actually stop the process and down here think it is there's cgus r1 and see us r2 so these are sort of signals that are not defined by the operating system they are user defined so they are therefore basically for somebody to use them in case you want something custom as you can see all the others actually have some sort of default functionality other interesting notable signals are the cig stop signal which just basically stops the execution pauses the execution of the process so that well the process is not terminated it can continue its execution but it's just paused and then when it receives the sig contour the continued signal it can continue its execution so with this you can actually basically control whether or not a process is executing or not that's really all I wanted to talk about in today's video just a very very basic explanation of signals right they are different from pipes and they are used to control most to control the processes at a sort of granular level just tell me the process or stop the process or something related to processes or i/o happened in later lessons we're going to take a look at how to actually handle a signal because right now we just send a signal command and it automatically kills itself but you can actually tell it how to handle that signal you're going to tell it to ignore certain signals and a lot more other customizations can be achieved with these signals it is quite interesting so I hope you stick around ok thank you guys so much for watching I hope you got something out of it if you do have any questions you can leave them down comments below or on our discord solver take care bye you
Info
Channel: CodeVault
Views: 14,251
Rating: 4.9614458 out of 5
Keywords: signal, kill, sigkill, linux, c (programming language), process, codevault
Id: 5We_HtLlAbs
Channel Id: undefined
Length: 8min 23sec (503 seconds)
Published: Tue May 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.