Communicating between processes using signals

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

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

  • @hugo-garcia
    @hugo-garcia 3 года назад +16

    Some heroes don't wear cape

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

    Damn I watched all the series about Processes, pipes and signals and I found out that I didn't subscribe yet, shame on me! This is by far the best channel for learning advanced stuffs in C. Thank you a lot for sharing it with us.

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

    I really appreciate your videos! They’re really helpful and I learn a lot from them :)

  • @nuse420
    @nuse420 3 года назад +21

    Thanks for making these. I have shared your channel with my classmates, this series has been a valuable resource for me. If your into taking requests, is it possible you can make a video that really goes deeper with more complex examples using signal handlers.

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

      Thank you! I'll look into it... Although there's not too much more to talk about signals and signal handlers. Do you have something more specific in mind?

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

      ​@@CodeVault Yes actually, I'd like to know more about masking or blocking signals and the functions like sigaddset. sigfillset, I try reading the man pages on all this but they are just hard to parse sometimes; normally because there is some missing bit I don't know yet that makes it all come together.

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

    Hey there, just checked the website, it's really nice to have all your c courses in one platform together with the code. I have one suggestion, it will be really nice if there was a button to click to copy the code instead of having to select and copy. Thanks for the courses, it has helped in my degree. Still waiting for the threads lessons :)

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

      Thanks! Will add that in the next update

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

    Great videos !!

  • @JJ-uu5jg
    @JJ-uu5jg Год назад +1

    Always a pleasure to watch your videos! It would be wonderful if you can talk about signal safe actions too!

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

    Thanks for the series of videos provided in the playlist. Much useful. Could you provide videos on timer module pls

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

      I can look into it, thanks for the recommendation!

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

    For the struct sigaction code block, this should be where the signal will be handled? I have a few child processes that need to be in communication,

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

      The functions up top are what is executed once receiving a signal

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

    i have got doubt :(
    do change in global variable by child process gets reflected in parent process?
    if so , that means global variable is actually a shared variable.

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

      in these video , x is used like that; please help me out;

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

      No, they are not shared. x is only used in the parent process. The child process just sends the signal SIGUSR1 to the parent process which executes that handle_sigusr1 function

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

      Sir, in 9:17 .. then how it is able to not print "hint statement" after printing "right"..means how declaring x as global solves that ;

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

      Well the hint is being printed by the parent process. We have it global because we use it in both the main function and the signal handler function

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

      Got it thanks.

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

    great content!!!
    would you mind making a series of videos on header files and the functions they contain?

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

      What do you mean exactly? Making an overview of each of the standard header files like stdio.h, stdlib.h etc.?

    • @asifsaad5827
      @asifsaad5827 4 года назад

      @@CodeVault yes!!

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

      Alright, I'll look into it

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

    Hey at 10:49 you've mentioned atomic operations, I've looked in the Internet but I still do not fully understand what are they, could you please explain it to me if you have time to do so ofc ?

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

      Basically atomic operations are operations on data which occur instantly (in 1 CPU cycle). The idea is that, with these atomic operations, there's no risk of race conditions (video about race conditions here: code-vault.net/lesson/18ec1942c2da46840693efe9b51ea1a2) since there's no time for other operations to do anything in the time it takes to execute this atomic operation. I will look into making a video about these atomic operations

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

      @@CodeVault Thanks a lot I really appreciate your help, I understand now :) .

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

    very nice thanks bro ez project

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

    Is it possible to use this method to handling signals in two processes that are non related (no fork no exec)?
    But these two processes have a shared memory

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

      Yes, you can use the kill() function to send signals between unrelated processes

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

      @@CodeVault It helped. Thank you!

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

    thank you

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

    thanks bro,

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

    Could you explain a little about how a process know that there is a signal just arrived (so the process know it should execute the handler function immediately)?

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

      Signals are received from the underlying operating system. Since the operating system is the one managing each process it basically has full control over it.
      By default, when receiving a signal, the process immediately does some specific action while pausing the exection (for example SIGSTOP stops the process). For some of the signals we can assign custom signal handlers (as we did in the video) with which we can execute some custom code on top of the default behavior.

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

    great video, thanks!
    have you ever thought about a video about monitors? that would be super nice :D

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

      Monitors are designed for threads, not processes (or, rather, for shared memory not message passing).
      It's going to be a while but we'll look at threads too in a later course. That's where I'll surely be covering monitors!

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

      @@CodeVault amazing as always hahah

  • @omarghazi9553
    @omarghazi9553 9 месяцев назад

    is it possible to declare the signal struct into a header file then just call signaction in the main function ?

    • @CodeVault
      @CodeVault  9 месяцев назад

      Yes, of course

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

    I coded this program so if the user answer the question before the hint is displayed, from the parent process I send a SIGKILL signal to the child process, so there's no need to wait the sleep() call. Is there a problem with this approach?

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

    Hi, So after the scanf input I can just kill(pid, SIGKILL) to kill the child process so the hint doesn't show up, is this correct?

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

      Yes, that is correct

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

    What if after making x global, user enters 0 as input.?Then x would be 0 and the hint would be printed, despite the fact that an input is already given. Could we solve our problem in another way?

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

      Just get another variable that tells you whether or not the user gave an answer or not

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

      Well I guess, if the user thinks that the result of 3 * 5 is 0 the hint won`t be redundant...

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

    Hi, I am trying to find out how can a parent create a timer for 10 secs and if a specific child hasn't finished when the alarm signal triggers this child should be killed. Its a few days I am coding but can't solve it (no global vars allowed).. I would appreciate some help... Thank you anyway for your educative videos

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

      Hmm... First you need the waiting process to call sleep(10) and then sends a signal to kill a specific child process. The problem is how to get that PID to that process. To do that, you could simply create the processing process beforehand. Something like this:
      int main(int argc, char* argv[]) {
      int pidp = fork();
      if (pidp == 0) {
      sleep(30); // simulates doing some work for 30 seconds
      printf("Finished executing
      ");
      return 0;
      }
      int pidw = fork();
      if (pidw == 0) {
      sleep(10);
      kill(pidp, SIGKILL);
      printf("Stopped
      ");
      return 0;
      }
      wait(NULL);
      wait(NULL);
      return 0;
      }
      You should see just the message "Stopped" on the console

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

      @@CodeVault Thank you for your answer. Since the time limit is a parameter I use sigwait(timelimit) and then waitpid for the specific child and if it was alive I sent the kill(pidp, SIGKILL) as you mentionned.

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

    Hello i used same code , (copied from wepsite) but when i run it takes input 2 times why?

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

      Ahh, sorry, try
      scanf("%d", &x);
      I think I added an extra
      there, my bad

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

    My signals are not working quite right...I always have to add #define _XOPEN_SOURCE 700 for struct sigaction to be recognized. And the "hint" message is always being outputted, even when I enter the correct answer directly. Any idea what could be causing this?

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

      Odd... sigusr1 and sigusr2 shouldn't be sent by any other process. Double check you're not sending it too soon so the "hint" appears instantly

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

      @@CodeVault In the meantime everything works :)

  • @kiseijuu7420
    @kiseijuu7420 29 дней назад

    Something i dont understand is ur global variable. I learnt that if u change a global in a process, it wont change its value in an other process for exemple, and it sounds logical to me. But here it looks like after using scanf in the parent process, it also changed the value of x for the child process, how come?

    • @CodeVault
      @CodeVault  23 дня назад

      I was a bit worried for a second that I made a mistake but no. Both the child process and the parent process have their own "x" variable, although only the parent process makes use of it. Both the else branch and the handle_sigusr() function is executed by the parent process. The child process only sends the SIGUSR signal to its parent (getppid()), the parent process is the one that executes the handle_sigusr() function

  • @FrodosBeutel
    @FrodosBeutel 2 года назад +4

    7:25 only because of that wait(NULL) the programm didnt execute and waited for the Hint. Just remove it everything works perfect.

    • @Mousy
      @Mousy 5 месяцев назад

      Wouldn't that create a zombie process ? You could add a kill(pid, SIGKILL) after the printf("Right!"); to terminate the child process once the user answers correctly. Then wait(NULL) won't wait for the hint since the process will have been killed

  • @sliyk
    @sliyk Месяц назад

    I recreated the code, but doesn't print the HINT even after 5 seconds. Only if I input something (can be only pressing ENTER) then the hint is printed. Why? I think the scanf is waiting for input is blocking the signal, but don't know why.

    • @sliyk
      @sliyk Месяц назад

      I will answer myself. Needed to flush stdou manually...

    • @CodeVault
      @CodeVault  Месяц назад +1

      In signal handlers, using IO functions is discouraged and can create undefined behaviour. If it doesn't work as expected with printf, that's alright. I only used it for exemplification

    • @sliyk
      @sliyk Месяц назад

      Thank you for reply. I know it should be simple actions only, but I needed it to my school project (creating server - client with communication only via signals).
      It helped flushing stdout (already posted here, don't understand why it's disappeared 😮)

  • @joseluisdominguezplaza6839
    @joseluisdominguezplaza6839 11 месяцев назад

    what is the compiler that he is using?

    • @CodeVault
      @CodeVault  11 месяцев назад

      I'm using gcc for all the Linux-related videos