Short introduction to signals in C

Поделиться
HTML-код
  • Опубликовано: 25 май 2020
  • Check out our Discord server: / discord

Комментарии • 71

  • @amitkazado2739
    @amitkazado2739 2 года назад +28

    Thanks for creating this content, it really helped me in my OS class.

  • @ash__borne
    @ash__borne 2 года назад

    Thank you for making such incredible videos. You're helping a lot of people. You're too underrated !

  • @rahulmaddula5776
    @rahulmaddula5776 2 года назад +2

    No other channel can match the quality of your videos. Hands down the best.

  • @VultureGamerPL
    @VultureGamerPL 2 года назад +1

    Clearn and concise. Thanks

  • @hectordoyle4718
    @hectordoyle4718 4 года назад +7

    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 года назад +8

      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 3 года назад +2

      Even a simple C shell

    • @aniruddharora4004
      @aniruddharora4004 Год назад

      @@CodeVault do a podcast, i'd be interested.

  • @Justin-nf6tw
    @Justin-nf6tw Год назад

    I love you dude you really helped me a lot

  • @abbadkamel8839
    @abbadkamel8839 3 года назад

    thankk you soo much you helped a lot you explain very simple and easy way

  • @starthustling3290
    @starthustling3290 2 года назад

    OS students are grateful.
    Love from Poland

  • @sxx2491
    @sxx2491 Год назад

    nice and useful contents, helped me a lot, hope to see more😀

  • @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.

  • @michelemassa3029
    @michelemassa3029 2 года назад

    saved my day dude, thanks

  • @haomintian6815
    @haomintian6815 2 года назад +9

    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  2 года назад +11

      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 года назад +1

      @@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 года назад +5

      ​@@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 Год назад

      @@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

  • @Kuriom_Mandara
    @Kuriom_Mandara 7 месяцев назад

    Thank you ❤

  • @antoineloiseau7811
    @antoineloiseau7811 Год назад +8

    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

  • @TuMadre8000
    @TuMadre8000 2 года назад

    amazing. thanks

  • @lit7961
    @lit7961 Год назад

    Helpfull content, thanks

  • @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

  • @ib1664
    @ib1664 4 года назад +1

    Nice, Thanks. How about some threads, mutex, or semaphores

    • @CodeVault
      @CodeVault  4 года назад +3

      They are planned. First, we'll finish with the Linux specifics related to processes and then I'll release some videos on multi-threading.

  • @rajatsharma2429
    @rajatsharma2429 2 года назад +1

    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  2 года назад +1

      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 Год назад

      Same here on My M1 Mac

  • @altoma3562
    @altoma3562 3 года назад +1

    What program do you use for codding? Thanks

    • @CodeVault
      @CodeVault  3 года назад

      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).
      ruclips.net/video/N5GhV8K8DIc/видео.html

  • @anantpawar6319
    @anantpawar6319 Год назад

    thanks sir

  • @yesnt1918
    @yesnt1918 Год назад

    you are best and more thing you are the bestestest

  • @georgioziozas9031
    @georgioziozas9031 3 года назад

    if we have multiple forks() so multiple processes , how are we gonna pass the correct pid in the kill function ?

    • @CodeVault
      @CodeVault  3 года назад

      If you properly retain the return values of fork() you can refer back to the child processes using it

    • @georgioziozas9031
      @georgioziozas9031 3 года назад

      @@CodeVault assigning an int variable at each fork call and play with if statements you mean?

    • @CodeVault
      @CodeVault  3 года назад

      Yes

  • @anuragupadhyay319
    @anuragupadhyay319 2 года назад

    Good content

  • @alanmenescal3487
    @alanmenescal3487 Год назад

    Veery good content

  • @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 года назад +1

      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

  • @animeshkumar1201
    @animeshkumar1201 3 года назад

    Can this be done with threads?(expecting pthreads)

    • @CodeVault
      @CodeVault  3 года назад +3

      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

  • @reborn4612
    @reborn4612 2 года назад

    thanks teacher :D

  • @matheusmaaia
    @matheusmaaia 2 года назад

    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  2 года назад +4

      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 2 года назад

      @@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!

  • @mikelgarciarodriguez6961
    @mikelgarciarodriguez6961 2 года назад

    Does the created process with fork run in parallel? i mean, the child and the father process?

    • @CodeVault
      @CodeVault  2 года назад

      Yes

    • @mikelgarciarodriguez6961
      @mikelgarciarodriguez6961 2 года назад

      @@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

  • @ORagnar
    @ORagnar 3 года назад

    Is there anything equivalent to signals in Windows?

    • @CodeVault
      @CodeVault  3 года назад +1

      Not very similar. Windows uses asynchronous procedure calls

  • @AmitSingh-sh8cc
    @AmitSingh-sh8cc 2 года назад +3

    This very very basic concept gave me migraine.

  • @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

  • @joseluisdominguezplaza6839
    @joseluisdominguezplaza6839 Год назад

    What program are you using?

    • @CodeVault
      @CodeVault  Год назад

      If you mean the IDE, I'm using Visual Studio Code

  • @andreamartinez-xy8dm
    @andreamartinez-xy8dm 2 года назад

    when it gets stuck does that mean it will create deadlock?

    • @CodeVault
      @CodeVault  2 года назад +1

      No, a deadlock is created when two processes or threads are waiting for each other. Usually that happens with mutexes

  • @m.preacher2829
    @m.preacher2829 2 года назад

    can i think that signal is just like an interrupt?

    • @CodeVault
      @CodeVault  2 года назад +3

      To some degree they are similar. This post explains the difference pretty well: stackoverflow.com/questions/13341870/signals-and-interrupts-a-comparison

  • @jaibalajt833
    @jaibalajt833 2 года назад

    can child kill the parent process by using getppid()?

    • @CodeVault
      @CodeVault  2 года назад +1

      You can, yes. But that would make the current process a zombie process.