Programming in LINUX is related to the sub-process exit caused by the persistence mechanism of REDIS.

Source: Internet
Author: User

Programming in LINUX is related to the sub-process exit caused by the persistence mechanism of REDIS.

19:22:01 2014-08-27

Introduction:

I used to understand wait waitpid and exit functions in general, but when we look at REDIS's AOF and RDB for persistence, we need to handle what the child process needs to do after the child process is completed and exited, and what the parent process needs to do, therefore, I have read the UNIX environment programming and LINUX system programming books to repeat the overall points.

 

Content:

Generally, if the program is similar to the following:

 

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

 

The sub-process will call our process to exit the system call exit () is divided into two states: 0: success, not 0: Failure

Exit requires the following:

1: Close all open IO streams

2. Delete All temporary files

1 2 is what the legendary user State needs to accomplish. Next we will call_ Exit ()[This function is encapsulated by exit () in the user State] triggers the kernel to complete the process exit.

The next step is to clear the resources required by all sub-processes.Sends a process exit status [Signal]What to do to the parent process of the parent process is done by the programmer.

The signal sent by the sub-process is SIGCHLD. In general, it is like REDIS fork (). After a sub-process completes the corresponding persistence action, the parent process needs to know the status of the child process for corresponding processing. Therefore, in LINUX, after the child process exits, there is a tag bit called the zombie process. The parent process must obtain the corresponding zombie tag information of the child process. Otherwise, it will not be eliminated.

Therefore, for the parent process, you must call the corresponding function [Register SIGCHLD first] to handle these flag bits. wait/waitpid is just like this. Let's take a look at what this series of calls have 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 int main () 8 {9 int status; 10 pid_t pid; 11 if (fork () = 0) 12 {13 abort (); 14} 15 pid = wait (& status); 16 printf ("pid = % d \ n", pid); 17 if (WIFEXITED (status )) 18 printf ("normal exit \ n"); 19 if (WIFSIGNALED (status) 20 printf ("killed by signal \ n"); 21 return 0; 22}

 

The wait waits for the sub-process to be suspended and the sub-process can be read. Therefore, it is very practical to read the sub-process status ~!

Next let's take a look at the differences between the wait/waitpid functions:

If there are multiple sub-processes, what should I do? select multiple wait, but at the same time, it may process one sub-process. Other process statuses are not processed, resulting in many zombie processes. This is not feasible. with the waitpid function:

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

Pid =-1 waiting for any sub-process

> 0 indicates the sub-process to be passed in.

Parameters:

WNOHANG: return immediately without blocking. Other parameters are not studied for the moment.

In this case, if the while (waitpid (-1, & stat, WNOHANG)> 0 );

In this way, it can be connected to the SIGCHLD of all sub-processes. Wait is only executed once, but the Linux signal will only be penalized once if it arrives at the same time without queuing. This must be understood and solved through the waitpid loop, however, if WNOHANG is not specified, the main process will be congested. This is not what we want, so we must solve the blocking.

 


When redis chooses aof persistence, only half of maxmemory can be used?

A new fork process may be used for persistence operations. The child process completely copies the parent process. maxmemory may be the memory of all processes, so it has only half of the memory.


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.