[Mat][1] が示唆するように、IPC を使用して解決していますが、それでもできません
。ちなみに、宿題ではなくサンプル演習です。しかし、私は試験が来るので、
ここで私を助けてください
.
ありがとう。
#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