How does a child process crash? How does the parent process know to restart the child process?

Source: Internet
Author: User
How does the parent process know how to repeat the sub-process-general Linux technology-Linux technology and application information when the sub-process crashes? The following is a detailed description. Hello everyone, I have a program. In the main process, fork has two processes and sub-processes execute tasks cyclically. I want to make a control. When the sub-process crashes, let the parent process obtain a signal from the child process and restart the child process to ensure that there are always two child processes working.
I used signal (SIGCLD, sig_process );
The sig_process function is as follows:
Static void sig_process (int signum)
{
Int pid_t, pstatus, acsid;
Switch (signum)
{
Case SIGCHLD:
Pid_t = wait (& pstatus );
If (pid_t> 0)
{
For (acsid = 1; acsid <= SHMINFO-> EPOLLNUM; acsid ++)
{
Printf ("SHMINFO-> SU_EPOLLS [% D-1]. pid = % d, pid_t = % d \ n", acsid, SHMINFO-> SU_EPOLLS [acsid-1]. pid, pid_t );
If (SHMINFO-> SU_EPOLLS [acsid-1]. pid = pid_t)
{
Printf ("set the acs % d state \ n", acsid );
P_Action (SEM_ I _ID, 0 );
SHMINFO-> SU_EPOLLS [acsid-1]. state = 4;
V_Action (SEM_ I _ID, 0 );
Printf ("set the acs % d state end \ n", acsid );
Siglongjmp (env_startacs, 1); // jump to the previous setjmp
}
}
}
Break;
}

}

There is a loop in the parent process to restart the child process:
While (1)
{
If (sigsetjmp (env_startacs, 1) = 0) // set the mark
{
Printf ("setjmp OK... \ n ");
}
Else
{
Printf ("longjmp OK... \ n ");
}
Printf ("start wait... \ n ");
Signal (SIGCLD, sig_process); // handle a subprocess Error
For (acsid = 1; acsid <= epnum; acsid ++)
{
// Printf ("circle acsid = % d \ n", acsid );
If (SHMINFO-> SU_EPOLLS [acsid-1]. state = 4)
{
Printf ("start_acsserver_worker acsid = % d \ n", acsid );
Start_acsserver_worker (argc, argv, envp, acsid, listenfd );
}
}
}
However, signal (SIGCLD, sig_process); // when the sub-process fails to handle the error, it will not run down in this line.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.