红联Linux门户
Linux帮助

用信号进行进程通信好像出现了死锁

发布时间:2014-04-14 15:56:19来源:红联作者:weizexi
#include
#include
#include
#include
#include
void startc(int a);
void startp(int a);
int sig1=0,sig2=0;
void startc(int a)
{
sig1=1;
}
void startp(int a)
{
sig2=2;
}
int main()
{
pid_t ppid,cpid;
int n;
printf("parent process and child process will get their ID and they will communicate to each other\n");
n=fork();
if(n==-1)
perror("fork failed");
else if(n==0)
{
signal(SIGINT,startc);
cpid=getpid();
ppid=getppid();
printf("this is child process,child process ID is %d and parent process ID is %d\n",cpid,ppid);
kill(ppid,SIGINT);
while(sig1==0)
sleep(2);
if(sig1==1)
printf("child process communicate to the parent process\n");
}
else
{
signal(SIGINT,startp);
ppid=getpid();
printf("this is parent process,ID is %d\n",ppid);
kill(n,SIGINT);
while(sig2==0)
sleep(2);
if(sig2==2)
printf("parent process communicate to the child process\n");
}
return 0;
}这是我用signal函数写的进程通信程序,但只有父进程会执行一部分,由键盘输入ctrl+c也只有父进程能收到从而输出
文章评论

共有 3 条评论

  1. hecong129 于 2014-04-22 14:51:05发表:

    man kill 下

  2. 于 2014-04-22 14:49:28发表:

    man kill 下

  3. cosxeb 于 2014-04-15 08:40:54发表:

    帮你顶下