Search code examples
linuxsignalsipc

Is there any way to send a value through signal


I am programming a program with 2 process, parent process forks to generate a child process. And my problem is that i wanna send a value from parent to child in a most simple way. Can i do it with signal or any other IPC(most simple). Thank All !!!


Solution

  • Data can be sent/received with a signal - at least on systems supporting sigqueue() and sigaction() with SA_SIGINFO. Example for the receiving process:

    #include <signal.h>
    #include <string.h>
    
    void usr1(int sig, siginfo_t *sip, void *ptr)
    {
        printf("sival_int %d\n", sip->si_value.sival_int);
    }
    
    . . .
    
        struct sigaction sa;
    
        memset(&sa, 0, sizeof (sa));
        sa.sa_sigaction = usr1;
        sa.sa_flags = SA_SIGINFO;
        sigaction(SIGUSR1, &sa, NULL);
    

    Example for the sending process:

    #include <signal.h>
    
    . . .
    
        union sigval sv;
    
        sv.sival_int = 43210;
        sigqueue(target_pid, SIGUSR1, sv);
    

    See union sigval for another choice.
    Manual pages: sigaction, sigqueue