我正在尝试编写一个代码,其中必须将信号广播到多个进程(不使用简单的循环将信号分别发送到每个进程)。
我的计划是将所有进程的进程组ID设置为与发送方进程的进程ID相同,然后使用kill(0,SIGUSR1)
将信号发送到组中的所有进程。但是,每当我尝试执行系统调用setpgid(0,pgid)
时,都会出现错误:Could not change gid: Operation not permitted
。
我对这两个过程的代码是:
#include "cn.h"
// sender process
void sig_handler(int alrm)
{
printf("Signal Received\n");
}
int main(int argc, char const *argv[])
{
signal(SIGUSR1,sig_handler);
printf("%d\n",(int)getpgid(0)); //print the gid and wait before proceeding
getchar();
if(setpgid(0,100)<0)
{
perror("Could not change gid");
}
kill(0,SIGUSR1);
return 0;
}
对于接收过程:
#include "cn.h"
void sig_handler(int alrm)
{
printf("Signal Received\n");
}
int main(int argc, char const *argv[])
{
signal(SIGUSR1,sig_handler);
int d;
scanf("%d",&d); //take input of the gid and then change it
if(setpgid(0,d)<0)
{
perror("Could not change gid");
}
while(1);
return 0;
}
我想念什么?我需要进行哪些更改才能允许进程更改其自己的进程组ID?