Advertisement
STANAANDREY

linux send sigusr1 signal from parent to kid

Nov 2nd, 2024
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8.  
  9. int done;
  10.  
  11. void handleSigusr1() {
  12.     puts("Received SIGUSR1");
  13.     done = 1;
  14. }
  15.  
  16. void kidProc() {
  17.     struct sigaction sa;
  18.     memset(&sa, 0, sizeof(struct sigaction));
  19.     sa.sa_handler = &handleSigusr1;
  20.     if (sigaction(SIGUSR1, &sa, NULL)) {
  21.         perror("sigaction");
  22.         exit(1);
  23.     }
  24.     while (!done) sleep(1);
  25. }
  26.  
  27. int main(int argc, char *argv[]) {
  28.     pid_t pid = fork();
  29.     if (pid == -1) {
  30.         perror("");
  31.         exit(-1);
  32.     }
  33.     if (pid == 0) {//kid
  34.         kidProc();
  35.         exit(0);
  36.     }
  37.     sleep(2);
  38.     kill(pid, SIGUSR1);
  39.     int stat;
  40.     wait(&stat);
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement