Linux system programming issues related to the exit of a child process that is associated with a redis persistence mechanism

Source: Internet
Author: User

19:22:01 2014-08-27

Introduction:

Previously the functions of wait waitpid and exit were only generally understood, but looking at the aof and Rdb 2 persistence of Redis to handle the completion of the child process run out and what the parent process needs to do, so specific read the UNIX environment programming and Linux system programming 2 books Re-comb the whole point.

Content:

Generally: If the program resembles the following:

if ((Pid=fork ()) ==0) {  dochildtthing (); exit (0);      } else if (pid>0) {dofathertthing ();}    

The child process will call our process exit system call exit () divided into 2 States 0: Success not 0: failed

Exit needs to do the following things:

1: Close all open IO streams

2 Delete all temporary files

1 2 is the legendary user state needs to complete the thing next call _exit () "This function in the user state is the exit () encapsulated function" trigger the kernel to complete the process of the exit of the work

The next step is to empty all the resources needed for the subprocess. Oh, the last thing you do is send a process exit status "signal" to the parent process parent process what processing is done by the programmer.

The signal sent by the child process is sigchld generally, after a process like Redis fork () has completed the corresponding persistence action, the parent process needs to know some state of the child process to do the corresponding processing, so in the Linux system, the child process exits with a tag bit We call it the zombie process. The parent process must fetch the corresponding token information for the corresponding zombie of the child process or it will not be eliminated.

So for the parent process, the corresponding function must be called "first registered with SIGCHLD" to process these token bits Oh, wait/waitpid, that's what this is about. Let's take a look at what this series of calls is going to do:

1#include <unistd.h>2#include <stdio.h>3#include <sys/types.h>4#include <sys/wait.h>5#include <stdlib.h>6 7 intMain ()8 {9     intstatus;Ten pid_t pid; One     if(fork () = =0) A     { - abort (); -     } thePid=wait (&status); -printf"pid=%d\n", PID); -     if(wifexited (status)) -printf"normal exit \ n"); +     if(wifsignaled (status)) -printf"killed by signal\n"); +     return 0; A}

Wait waits for the child process to hang the result can be read out the child process is signaled by the signal is terminated, so it is very practical to read the status of the sub-process!

Next look at the difference between the Wait/waitpid functions:

If there are more than one child process what do I do? Either select multiple wait but arrive at the same time may handle a child process and other process state is not processed causing many zombie processes this is not possible. This has the Waitpid function:

Waitpid (pid_t pid,int *status,int options)

Pid=-1 waiting for any one child process

>0 the child process to be passed in

Parameters:

Wnohang: does not block immediately return. Other parameters are not being studied for the time being.

So if there is this while (Waitpid ( -1,&stat,wnohang) >0);

This will enable the sigchld of all sub-processes to be accepted. Wait is only executed once and the Linux signal does not queue and arrive at the same time will be punished only once oh this must be understood through the waitpid loop to solve the problem, but if you do not specify Wnohang words and more trouble the main process will block this is not what we want oh so must be unblocked.

Linux system programming issues related to the exit of a child process that is associated with a redis persistence mechanism

Related Article

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.