1

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;
}       
alk
  • 69,737
  • 10
  • 105
  • 255
Baal
  • 77
  • 8
  • 1
    "The `sigqueue()` function sends a signal to a process **or a group of processes**..." Where do you get that `sigqueue()` can be used to send a signal to an entire process group? The [POSIX specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigqueue.html) and [Linux man page](http://man7.org/linux/man-pages/man3/sigqueue.3.html) both only state a single process can be signaled. – Andrew Henle Sep 12 '16 at 15:53
  • [link](https://www.mkssoftware.com/docs/man3/sigqueue.3.asp), here. Ok so how i can do that? It is possible? Many posts tell me that kill() and sigqueue() are pretty the same... – Baal Sep 12 '16 at 16:05

1 Answers1

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.

pilcrow
  • 56,591
  • 13
  • 94
  • 135
  • My implementation of `sigqueue`?It's from so no chance... But there's no other way to do this? Somehow pull out pid from every child that i create? – Baal Sep 12 '16 at 21:31
  • @Baal, by "implementation" I mean the platform/environment you are running on. As for other ways to do what you want ... I'd suggest you ask a separate question on this site. – pilcrow Sep 12 '16 at 21:32