0

Write a program that creates a child process using fork (). The child prints its parent’s name, Parent ID and Own ID while the parent waits for the signal from the child process. Parent sets an alarm 10 seconds after the Child termination.

The below code does not receive the alarm signal from child and also it does not go in the alarm handler i.e. ding function.

what should i 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 is Finished"<<endl; 

        signal(SIGALRM, ding);        
}
}

1 Answers1

0

You need to setup parent signal handler "signal(SIGALRM, ding)" before the child sends SIGALARM. In your case you can move "signal(SIGALRM, ding)" one line above fork().

Max Fomichev
  • 244
  • 1
  • 13