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

Popular posts from this blog

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -