Short introduction to signals in C

  Рет қаралды 86,267

CodeVault

CodeVault

Күн бұрын

Пікірлер: 74
@amitkazado2739
@amitkazado2739 3 жыл бұрын
Thanks for creating this content, it really helped me in my OS class.
@rahulmaddula5776
@rahulmaddula5776 3 жыл бұрын
No other channel can match the quality of your videos. Hands down the best.
@ash__borne
@ash__borne 2 жыл бұрын
Thank you for making such incredible videos. You're helping a lot of people. You're too underrated !
@hectordoyle4718
@hectordoyle4718 4 жыл бұрын
you're keep on makin' exactly what I am interested in - thank you! it may be quite a hardcore request, but how about a series about a small system programming project that will clip stuff from some of your other great videos together? (maybe a shell-like program, vim-like editor or a terminal function remake)
@CodeVault
@CodeVault 4 жыл бұрын
Nice suggestion! I have something similar in mind. Currently the website for CodeVault is in the works and I was thinking about making a devlog series about how I take certain implementation decisions for it. Another project would be related to computer graphics and implementing your own game engine (but, again, just a devlog series not full blown detailed tutorials). What do you think?
@hectordoyle4718
@hectordoyle4718 4 жыл бұрын
@@CodeVault I considered game engine a dream hahah that would be best thing possible!
@millicentmalinga962
@millicentmalinga962 4 жыл бұрын
Even a simple C shell
@aniruddharora4004
@aniruddharora4004 2 жыл бұрын
@@CodeVault do a podcast, i'd be interested.
@haomintian6815
@haomintian6815 3 жыл бұрын
Great video content! But I am confused by why kill(pid, SIGKILL); under parent process. Since the pid is the parent process ID, does it actually terminate the parent process instead of children process? Would you mind explain? Thanks
@CodeVault
@CodeVault 3 жыл бұрын
Nonono, pid is the child's process ID. It's what fork() returns in the parent process. So we're sending sigkill to the child process
@pawcherry8807
@pawcherry8807 2 жыл бұрын
@@CodeVault I have the same confusion. Since we are in the parent process doesnt pid have the ID of the current procces we are in(the parent process)? If we were in the child process shouldnt it only then contain the childs ID? Thanks for the content it helped me a lot so far!
@CodeVault
@CodeVault 2 жыл бұрын
​@@pawcherry8807 No, the pid variable stores what fork() returns. And fork doesn't return the current process id. If you want to get the current process id you can simply use getpid() for that. fork() returns 0 in the child process so that you can distinguish between the child and the parent process. If it were to work the way you're explaining then this is what would happen: int pid = fork(); // parent process id is 1001, which means pid is 1001 in the parent process // child process id is 1002, which means pid is 1002 in the child process How do we check here which is which? We could try: if (pid == 1001) { // parent process } else if (pid == 1002) { // child process } That works, BUT, the problem is that the process IDs are dynamic and relies on what other current processes are running on the system. So... maybe the next time you run the program you would get the parent process id to be 1201 and the child process id to be 903. Now... what do we do? The code above no longer works and we cannot (easily) distinguish between the child and parent process
@pawcherry8807
@pawcherry8807 2 жыл бұрын
@@CodeVault Ah i see. I had the return value confused... Thank you!
@dennisjoseph4528
@dennisjoseph4528 2 жыл бұрын
Thanks for asking this question. I was thinking about this as well
@starthustling3290
@starthustling3290 3 жыл бұрын
OS students are grateful. Love from Poland
@antoineloiseau7811
@antoineloiseau7811 2 жыл бұрын
Hi, I have a small suggestion to make here. We hear a lot of desk noises on the lowest frequencies, you should try to put a high pass filter on your microphone (you should be able to do that from your audio interface, if not I'd be glad to help you find out how to do it) Great contents by the way, very usefull, you helped me to understand many concepts during my cursus, thank you !
@CodeVault
@CodeVault 2 жыл бұрын
I'm sorry about that. I noticed that issue in the past and the later videos (namely the thread course) should have this issue anymore. If it's not yet fixed I will continue looking into it
@Justin-nf6tw
@Justin-nf6tw 2 жыл бұрын
I love you dude you really helped me a lot
@sxx2491
@sxx2491 2 жыл бұрын
nice and useful contents, helped me a lot, hope to see more😀
@jorgealves6944
@jorgealves6944 2 жыл бұрын
Good evennig! At minute 3:49. When you called the kill(pid, SIGKILL) function, was the process that died the child because the parent was on hold? Is this how KILL identified the pid of the child process?
@CodeVault
@CodeVault 2 жыл бұрын
pid was returned by fork(), the kill() function is executed in the parent process and pid is the process id of the child process. That's how it knows which process it should send the signal to
@Anduuu077
@Anduuu077 2 ай бұрын
very good video adn explanation
@rajatsharma2429
@rajatsharma2429 3 жыл бұрын
If i run the same code on my PC and send CTRL + C on STDOUT, both parent and child are getting terminated, which is in contrast to your observation at 2:20 in this video, where sending CTRL + C only terminates the parent. Is it something machine specific? I am using MACOS terminal. In general if multiple processes are point to same STDOUT, if signal specific commands such as CTRL + C are sent to STDOUT, will OS send same INT to all the processes pointing to same STDOUT, it seems like this in my setup. Last but not the least, super content, much appreciated!
@CodeVault
@CodeVault 3 жыл бұрын
Interesting.The behavior could be platform specific. I'm not sure if it has to do with STDOUT or the terminal itself is doing some extra logic behind the scenes
@martinhuang3469
@martinhuang3469 2 жыл бұрын
Same here on My M1 Mac
@VultureGamerPL
@VultureGamerPL 3 жыл бұрын
Clearn and concise. Thanks
@michelemassa3029
@michelemassa3029 2 жыл бұрын
saved my day dude, thanks
@joseluisdominguezplaza6839
@joseluisdominguezplaza6839 Жыл бұрын
What program are you using?
@CodeVault
@CodeVault Жыл бұрын
If you mean the IDE, I'm using Visual Studio Code
@yesnt1918
@yesnt1918 2 жыл бұрын
you are best and more thing you are the bestestest
@abbadkamel8839
@abbadkamel8839 4 жыл бұрын
thankk you soo much you helped a lot you explain very simple and easy way
@suneelabbigari
@suneelabbigari 2 жыл бұрын
Hi @Codevault Requested earlier as well, Please do a video on Timer module (timer init, timer set up) and different kinds of processes like daemon, init and sample programs in that. Please do a video on that.
@altoma3562
@altoma3562 4 жыл бұрын
What program do you use for codding? Thanks
@CodeVault
@CodeVault 4 жыл бұрын
Visual Studio Code. Here's a video on how you can set it up (although you need to be on Unix/Linux if you want to use the functions in this video). kzbin.info/www/bejne/hGaqmYlugJ13f8U
@ib1664
@ib1664 4 жыл бұрын
Nice, Thanks. How about some threads, mutex, or semaphores
@CodeVault
@CodeVault 4 жыл бұрын
They are planned. First, we'll finish with the Linux specifics related to processes and then I'll release some videos on multi-threading.
@m.preacher2829
@m.preacher2829 2 жыл бұрын
can i think that signal is just like an interrupt?
@CodeVault
@CodeVault 2 жыл бұрын
To some degree they are similar. This post explains the difference pretty well: stackoverflow.com/questions/13341870/signals-and-interrupts-a-comparison
@sudchmo744
@sudchmo744 7 ай бұрын
Is the function 'wait()' nessecerry after 'kill()' function, If so why do we need 'wait() function' ?
@CodeVault
@CodeVault 7 ай бұрын
wait() also clears the resources left by the child process (if any)
@sakkamouid3343
@sakkamouid3343 Жыл бұрын
is it possible to call the function kill() from the child process so that the parent process is the one to be killed . I tried it and the whole program seems to be stopped.
@CodeVault
@CodeVault Жыл бұрын
The child process shouldn't stop its execution even if you stop the parent process. After calling kill() try adding a sleep and some printf call in the child process. You should still see that message even after stopping the parent process
@alanmenescal3487
@alanmenescal3487 2 жыл бұрын
Veery good content
@lit7961
@lit7961 Жыл бұрын
Helpfull content, thanks
@mikelgarciarodriguez6961
@mikelgarciarodriguez6961 3 жыл бұрын
Does the created process with fork run in parallel? i mean, the child and the father process?
@CodeVault
@CodeVault 3 жыл бұрын
Yes
@mikelgarciarodriguez6961
@mikelgarciarodriguez6961 3 жыл бұрын
@@CodeVault how can i check this out by myself? im trying to make a programa and i want to see how this works. But i dont know how to see how processes works
@ruoyuanluo6896
@ruoyuanluo6896 3 жыл бұрын
What happens if a signal handler gets invoked when it’s already handling a signal for that same signal/handler? For example, what happens when a process receives a signal for SIGUSR1 when it’s already handling SIGUSR1?
@CodeVault
@CodeVault 3 жыл бұрын
They are executed in parallel multiple times
@ruoyuanluo6896
@ruoyuanluo6896 3 жыл бұрын
@@CodeVault Thanks! I am also wondering what happens if a signal handler gets invoked if the process is already handling a different signal? For example, what happens when a process receives a signal for SIGUSR2 when it’s busy handling SIGUSR1?
@CodeVault
@CodeVault 3 жыл бұрын
From my experience, they are handled in parallel as well. This is the reason I/O functions shouldn't really be used here, because it could cause issues when called in parallel.
@ruoyuanluo6896
@ruoyuanluo6896 3 жыл бұрын
@@CodeVault Thanks a lot! much appreciated
@georgioziozas9031
@georgioziozas9031 4 жыл бұрын
if we have multiple forks() so multiple processes , how are we gonna pass the correct pid in the kill function ?
@CodeVault
@CodeVault 4 жыл бұрын
If you properly retain the return values of fork() you can refer back to the child processes using it
@georgioziozas9031
@georgioziozas9031 4 жыл бұрын
@@CodeVault assigning an int variable at each fork call and play with if statements you mean?
@CodeVault
@CodeVault 4 жыл бұрын
Yes
@anuragupadhyay319
@anuragupadhyay319 3 жыл бұрын
Good content
@andreamartinez-xy8dm
@andreamartinez-xy8dm 3 жыл бұрын
when it gets stuck does that mean it will create deadlock?
@CodeVault
@CodeVault 3 жыл бұрын
No, a deadlock is created when two processes or threads are waiting for each other. Usually that happens with mutexes
@TuMadre8000
@TuMadre8000 3 жыл бұрын
amazing. thanks
@jaibalajt833
@jaibalajt833 3 жыл бұрын
can child kill the parent process by using getppid()?
@CodeVault
@CodeVault 3 жыл бұрын
You can, yes. But that would make the current process a zombie process.
@ORagnar
@ORagnar 3 жыл бұрын
Is there anything equivalent to signals in Windows?
@CodeVault
@CodeVault 3 жыл бұрын
Not very similar. Windows uses asynchronous procedure calls
@rigelmandarakuriom
@rigelmandarakuriom Жыл бұрын
Thank you ❤
@animeshkumar1201
@animeshkumar1201 4 жыл бұрын
Can this be done with threads?(expecting pthreads)
@CodeVault
@CodeVault 4 жыл бұрын
Threads are a different concept. You can have multiple threads running in the same process thus you can't communicate between them with signals. You usually use shared memory, locks, semaphores etc. to communicate between them
@matheusmaaia
@matheusmaaia 3 жыл бұрын
Thank you sir, it was very helpful!! I dont know if you'll see this but im kinda desperate so im going to ask anyway. I have some forks doing some executions but those executions are depending on random numbers generated with the rand function. Since the random takes a seed based on the time , the child processes keep finding always the sabe result as their fathers. Any tip to avoid that??Keep in mind that the program logic is to test random solutions for a problem and put the best one in a shared memory.
@CodeVault
@CodeVault 3 жыл бұрын
Hmm... You can change the parameter that you pass to srand. So instead of srand(time(NULL)); you could do srand(time(NULL) + getpid()); That way every process will pass a different number even if the time is the same
@matheusmaaia
@matheusmaaia 3 жыл бұрын
@@CodeVault Brilliantly simple! Thank you sir! i couldn't be abe to see this to the first step of my project but i'll try for sure on the next one!
@reborn4612
@reborn4612 3 жыл бұрын
thanks teacher :D
@anantpawar6319
@anantpawar6319 2 жыл бұрын
thanks sir
@AmitSingh-sh8cc
@AmitSingh-sh8cc 3 жыл бұрын
This very very basic concept gave me migraine.
Handling signals
11:44
CodeVault
Рет қаралды 82 М.
The Lost World: Living Room Edition
0:46
Daniel LaBelle
Рет қаралды 27 МЛН
Маусымашар-2023 / Гала-концерт / АТУ қоштасу
1:27:35
Jaidarman OFFICIAL / JCI
Рет қаралды 390 М.
Communicating between processes (using pipes) in C
14:00
CodeVault
Рет қаралды 294 М.
Process IDs in C
10:14
CodeVault
Рет қаралды 98 М.
Making Minimalist Chat Server in C on Linux
14:28
Nir Lichtman
Рет қаралды 45 М.
Introduction to FIFOs (aka named pipes) in C
18:49
CodeVault
Рет қаралды 90 М.
Communicating between processes using signals
11:17
CodeVault
Рет қаралды 64 М.
Sending and Handling Signals in C (kill, signal, sigaction)
5:52
Jacob Sorber
Рет қаралды 196 М.
Linux Signals. I Spent 2 Years to Understand This Part.
21:24
kimylamp
Рет қаралды 262 М.