c++ - How to receive the signal from child -
write program creates child process using fork (). child prints parent’s name, parent id , own id while parent waits signal child process. parent sets alarm 10 seconds after child termination.
the below code not receive alarm signal child , not go in alarm handler i.e. ding function.
what should do?
using namespace std; static int alarm_fired = 0; void ding(int sig) { sleep(10); cout<<"alarm\n"; } int main() { int cpid,i,ppid; int status = 0; cpid=fork(); switch(cpid) { case -1: cout<<"error"; break; case 0: cout<<"child block"<<endl; cout<<"parent\n"<<"parentid: "<<getppid()<<"\nchildid: "<<getpid(); kill(getppid(), sigalrm); default: cout<<"parent waiting"<<endl; wait(&status); cout<<"parent waiting finished"<<endl; signal(sigalrm, ding); } }
you need setup parent signal handler "signal(sigalrm, ding)" before child sends sigalarm. in case can move "signal(sigalrm, ding)" 1 line above fork().
Comments
Post a Comment