父进程如何给子进程发送signal

bear234 2017-01-09 10:08:54
有这样一道题目:
一个父进程创建两个子进程。
一个子进程可以接受signal 3,然后显示ping,然后给另一个子进程发送4;
另一个子进程可以接受signal 4,然后显示pong,然后给另一个子进程发送3。

父进程给第一个子进程发送signal 3。

五秒后,父进程给两个子进程发送SIGKILL,程序结束。


这是我写的代码:
#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

void func(int n)
{
printf("ping\n");
// how to send signal 4 to the second process?
}

void func2(int n)
{
printf("pong\n");
// how to send signal 3 to the first process?
}

int main()
{
pid_t pid;
int i;
for(i = 0; i < 2; i++)
{
pid = fork();
if(pid == 0)
{
if(i == 0)
{
signal(3, func);
}
else
{
signal(4, func2);
}
while(1);
}
else
{
if(i == 1)
{
sleep(3);
// how to send signal 3 to the first child process?
sleep(3);
// how to kill the two children?
}
}
}
return 0;
}



我知道用kill可以发送signal,但是这里我不知道如何给具体的某个子进程发送signal。
可以告诉我我的注释部分应该如何写代码吗?
...全文
522 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

18,778

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧