我正在按照 [Mat][1] 的建议使用 IPC 来解决它,但我仍然无法做到
,顺便说一下,这是示例练习,而不是家庭作业!但是我要考试了,所以
请在这里帮助我... :)
有人在这里指出我做错了什么。Kid2 没有发送或父母没有收到来自孩子 2 的任何信号。
谢谢。
#include<stdio.h>
#include<sys/wait.h>
#include<wait.h>
#include<signal.h>
void my_handler_for_kid2(int sig_num){
printf("KID2:Recieved SIGUSR1 from KID1\n");
}
void my_handler_for_parent(int sig_num){
printf("PARENT:Recieved SIGUSR2 from KID2\n");
}
int main(){
int status;
pid_t kid1,kid2;
void my_handler_for_kid2(int sig_num);
void my_handler_for_parent(int sig_num);
signal(SIGUSR1,my_handler_for_kid2);
signal(SIGUSR2,my_handler_for_parent);
kid1=fork();
if(kid1 < 0){
printf("Fork not successfull\n");
}
else if(kid1 == 0){
printf("I am KID1[%ld] and I am going to sleep\n",(long) getpid());
sleep(5);
printf("Just woke up!\n");
kill(kid2,SIGUSR1);
}
else{
kid2=fork();
if(kid2 < 0){
printf("Fork not successfull\n");
}
else if(kid2 == 0){
printf("I am KID2[%ld] and I am going to wait for KID1 to wakeup\n",(long) getpid());
pause();
//signal(SIGUSR1,my_handler_for_kid2);
kill(getppid(),SIGUSR2);
}
else{
printf("Father[%ld] here and going to wait for both kids to respond to me\n", (long) getpid());
pause();
//signal(SIGUSR2,my_handler_for_parent);
waitpid(kid1,&status,NULL);
waitpid(kid2,&status,NULL);
}
}
}
rizwan@riz-pc:~/Documents$ ./a.out
I am KID1[2278] and I am going to sleep
Father[2277] here and going to wait for both kids to respond to me
I am KID2[2279] and I am going to wait for KID1 to wakeup
Just woke up!
KID2:Recieved SIGUSR1 from KID1
^C
rizwan@riz-pc:~/Documents$
[1]: http://stackoverflow.com/users/635608/mat