0
您好我只是在处理两个进程之间的信号。我有一个持续运行的主进程(如MAIN
)。这个MAIN
从Wrapper进程派生(例如WRAP
)。进程之间的信号查询Linux
这是我的代码,它将实际启动WRAP
进程,进而创建子进程为MAIN
。
当在MAIN中完成某些初始化时,我想发布一个信号SIGUSR1
,该信号将被WRAP
捕获并执行其他一些操作。
我的代码的问题是当信号从MAIN
提出时,它从未被WRAP
过程困住。 PLS。在此代码上分享您的建议,或者是否有其他方法可以实现此目标。 谢谢。
在主处理:
初始化完成我已经加入该代码后,
main()
{
// Do some work here
int pid = GetProcessID(); // Returns the process ID of WRAP process
kill(pid,SIGUSR1); // Tries to send signal to WRAP process
// Other code
}
int GetProcessID()
{
int pid = 0;
char results[128];
FILE *fp = popen("pgrep WRAP", "r");
if(fp == NULL)
{
printf("Error: Failed to get Process ID");
}
else
{
while(fgets(results, 128, fp) != NULL)
{
pid = atoi(results);
}
pclose(fp);
}
return pid;
}
在WRAP过程:
main()
{
int pid;
signal(SIGUSR1,InitComplete);
if ((pid = fork()) < 0)
{
perror("fork");
exit(1);
}
if (pid == 0)
{
/* child */
system("mainProc.out");
}
else
{
/* parent */
if(KeepListening() == 1)
printf("Init completed successfully\n");
}
return 0;
}
int KeepListening()
{
const int MAX_WAIT_TIME = 180;
int procStarted = 0;
int res = 0;
sigset_t origset;
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss, SIGWINCH);
sigaddset(&ss, SIGUSR1);
res = sigprocmask(SIG_BLOCK, &ss, &origset);
if(res)
{
printf("\nError: sigprocmask returned an error\n");
}
struct timespec theTimeout;
theTimeout.tv_nsec = 0;
theTimeout.tv_sec = MAX_WAIT_TIME;
int sig = 0;
siginfo_t theInfo;
memset(&theInfo, '\0', sizeof(theInfo));
int timedwaitcount = 0;
do
{
sig = sigtimedwait(&ss, &theInfo, &theTimeout);
if(sig < 0)
{
if(EAGAIN == errno)
{
timedwaitcount++;
}
else
{
PrintMessage("Error:Error occured with sigtimedwait\n");
}
}
else
{
timedwaitcount = 0;
}
if(SIGUSR1 == sig)
{
return 1;
}
}while(SIGWINCH == sig || 0 == sig);
return procStarted;
}
void InitComplete()
printf("InitComplete in MAIN. Signal Received.\n");
}
您已经添加TBIS鳕鱼哪儿?谁杀了谁?请发布**完整**代码。另外,叉+系统? –
早些时候,我使用execvp()来启动进程,但由于某种原因,MAIN进程在几秒后将不会运行。巨大的代码块是一个包装代码,MAIN过程更多的是LOC。我只在需要提升信号的位置添加一行。 – killer
如果代码很大,您有责任将它缩小为[mcve]。这些指导原则是有原因的。 'kill(pid,SIGUSR1)中的'pid';'?没人知道。 –