Thread: How to make children to wait for infinite signals from parent?

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    12

    How to make children to wait for infinite signals from parent?

    Hi, I wrote a program where the parent creates many children with fork(). I then made all of them to pause(), cause I needed to send them the signal 15 to kill them. Once I sent them that signal, in the parent I use waitpid() to collect the zombie children (lol) and kill them instead of showing them as <defunct>.
    My problem now is that I need to send the children the signals 16 and 17 (SIGUSR1 and SIGUSR2) indefinitely. In particular, I need to make a handler for SIGUSR1 that allows me to increase a counter in every children, as many times as I want. For example:
    - Send to the child 4 the signal SIGUSR1: <counter of child 4: 1>
    - Send to the child 4 the signal SIGUSR1: <counter of child 4: 2>
    - Send to the child 4 the signal SIGUSR1: <counter of child 4: 3>
    - Send to the child 2 the signal SIGUSR1: <counter of child 2: 1>
    And so on... but as I send the first signal, the child terminates execution, and I don't know how to maintain every child running with the parent, while the parent is asking forever to the user which signal to send. Is there any function that I don't know of to do that?

    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're looking for sigprocmask.
    Code:
    #define _POSIX_C_SOURCE 199309L
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    #define NUMCHILDREN 4
    
    void child_func(int n) {
        sigset_t sigset;
        sigemptyset(&sigset);
        sigaddset(&sigset, SIGUSR1);
        sigprocmask(SIG_SETMASK, &sigset, NULL);
    
        int cnt = 0;
        for (;;)
            while (sigwaitinfo(&sigset, NULL) > 0)
                printf("child %d cnt: %d\n", n, ++cnt);
    }
    
    int main(void) {
        pid_t child[NUMCHILDREN];
    
        for (int i = 0; i < NUMCHILDREN; i++)
            if ((child[i] = fork()) == 0) {
                child_func(i);
                exit(0);
            }
    
        printf("Enter 0 to %d to send USR1 signal to that child;"
               " -1 to send KILL to all\n", NUMCHILDREN - 1);
        for (int n; scanf("%d", &n) == 1 && n >= 0; )
            kill(child[n], SIGUSR1);
    
        for (int i = 0; i < NUMCHILDREN; i++) {
            kill(child[i], SIGKILL);
            wait(NULL);
        }
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    12
    Thank you so much! This helped a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make C program to listen to Operating System Signals?
    By halfrobinhood in forum C Programming
    Replies: 2
    Last Post: 09-21-2011, 06:45 AM
  2. Replies: 1
    Last Post: 03-06-2011, 08:01 PM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. Signals, fork(), wait() and exec()
    By DJTurboToJo in forum C Programming
    Replies: 7
    Last Post: 03-18-2008, 09:14 AM
  5. "Which parent has children with no children"?!?
    By SMurf in forum C Programming
    Replies: 2
    Last Post: 04-28-2003, 01:59 PM

Tags for this Thread