Communicating between processes using signals

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 พ.ย. 2024

ความคิดเห็น • 71

  • @hugo-garcia
    @hugo-garcia 4 ปีที่แล้ว +22

    Some heroes don't wear cape

  • @charlessnachez4248
    @charlessnachez4248 2 ปีที่แล้ว +5

    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.

  • @nuse420
    @nuse420 4 ปีที่แล้ว +22

    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  4 ปีที่แล้ว +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 4 ปีที่แล้ว

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

  • @MothBallsYT
    @MothBallsYT 2 ปีที่แล้ว

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

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

  • @JJ-uu5jg
    @JJ-uu5jg 2 ปีที่แล้ว +1

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

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว +1

      I will look into it

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

  • @FrodosBeutel
    @FrodosBeutel 2 ปีที่แล้ว +5

    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 10 หลายเดือนก่อน

      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

    • @jkyleowens
      @jkyleowens หลายเดือนก่อน

      @@MousyMay be better to use waitpid to get pid and send a sigterm to handle in the child

  • @suneelabbigari
    @suneelabbigari 3 ปีที่แล้ว

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

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      I can look into it, thanks for the recommendation!

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

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

  • @martinhuang3469
    @martinhuang3469 2 ปีที่แล้ว +1

    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

  • @BradleyArdis
    @BradleyArdis 4 ปีที่แล้ว

    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  4 ปีที่แล้ว

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

  • @zikocult
    @zikocult 2 วันที่ผ่านมา

    if i don't want to declare x as global, how can i pass the value of x to the function? when i try this "sa.sa_handler = &handle_sigusr1(x);", give me an error.
    The prototype of my function was void "handle_sigusr1(int x);"

    • @CodeVault
      @CodeVault  วันที่ผ่านมา

      Since the handler is called on signal there isn't an easy way to do it without relying on something that is global

    • @zikocult
      @zikocult วันที่ผ่านมา

      @@CodeVault Thanks :)

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

  • @valerie8031
    @valerie8031 2 ปีที่แล้ว

    thank you

  • @kiseijuu7420
    @kiseijuu7420 6 หลายเดือนก่อน

    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  6 หลายเดือนก่อน

      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

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

    • @CodeVault
      @CodeVault  ปีที่แล้ว

      Yes, that's alright

  • @omarghazi9553
    @omarghazi9553 ปีที่แล้ว

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

    • @CodeVault
      @CodeVault  ปีที่แล้ว

      Yes, of course

  • @sliyk
    @sliyk 7 หลายเดือนก่อน

    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 7 หลายเดือนก่อน

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

    • @CodeVault
      @CodeVault  7 หลายเดือนก่อน +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 7 หลายเดือนก่อน

      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 😮)

  • @nisargpatel2958
    @nisargpatel2958 3 ปีที่แล้ว

    Great videos !!

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

  • @theodorekoussoulis7733
    @theodorekoussoulis7733 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

      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 2 ปีที่แล้ว

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

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

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

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

  • @nikb9585
    @nikb9585 3 ปีที่แล้ว

    thanks bro,

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

  • @joseluisdominguezplaza6839
    @joseluisdominguezplaza6839 ปีที่แล้ว

    what is the compiler that he is using?

    • @CodeVault
      @CodeVault  ปีที่แล้ว

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

  • @tomasoliveira7935
    @tomasoliveira7935 3 ปีที่แล้ว

    very nice thanks bro ez project