简体   繁体   中英

sigqueue() to send a signal to process group

I create process tree with fork() (about 3 child). I can easly send a signal to all of them by kill(-getpgrp(), signal_number), but how to do the same with sigqueue?

The sigqueue() function sends a signal to a process or a group of processes, they said, but unlike kill(), i can't use sigqueue() to send a signal to an entire process group by specifying a negative value in pid.

So how to do that ?

EDIT:

Code:

s1.c is a program where i "catch" the signals to take control over them. So the couple of s1 ( with the same group id) waits for signal from sigqueue().

int main (int argc,char *argv[])
{   

    int i=0;
    char *do=argv[1];
    int b=atoi(argv[2]); 
    int y=fork();
    if(argc !=3)
    {
        printf("wrong arg number\n");
        exit(0);
    }

do{

switch(fork())
    {
    case -1:
            perror("error \n");
            exit(1);
            break;
    case 0:
            execl("s1","s1.c",argv[1],argv[2], NULL);
            break;
    default:
            sleep(2);
            break;
}
i++;
}
while(i<3);
    sleep(2);
    printf("ssignal %d send to gpid : %d\n",b,getpgrp());

    union sigval value;
    value.sival_int = 0;
    value.sival_ptr = 0;

    if(sigqueue(getpgrp(), b, value) == 0) {
            printf("signal sent successfully!!\n");
    } else {
            perror("SIGSENT-ERROR:");
    }
    return 0;


while(wait(0) != -1){}

return 0;
}       

If your implementation does in fact support process group signaling in sigqueue , then you probably need to do exactly what you would do for the pid argument to kill :

sigqueue(-getprgp(), signo, value);  // Nonstandard. Remember to _negate_ the PGID
sigqueue(0, signo, value);           // Nonstandard. Shorthand to signal my own PGID

By the strict standard, however, you are out of luck. sigqueue supports neither the null signal (0) nor the ability to signal groups of processes :

The standard developers required that sigqueue() have the same semantics with respect to the null signal as kill() , and that the same permission checking be used. But because of the difficulty of implementing the "broadcast" semantic of kill() (for example, to process groups) and the interaction with resource allocation, this semantic was not adopted. The sigqueue() function queues a signal to a single process specified by the pid argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM