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

作者: weizexi 2014-04-14 15:56:19
#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也只有父进程能收到从而输出

相关资讯