Linux Network Programming Learning notes three-----Multi-process concurrent service side

Source: Internet
Author: User
Tags posix

The first is the fork () function.  Apue 8.3. A clearer explanation can be http://blog.csdn.net/lingdxuyan/article/details/4993883 and http://www.oschina.net/question/195301_62902.

One thing to add: After the fork returns, each file or socket in the original process describes the reference count of the Descriptor plus 1 (equivalent to being opened more than once), each call to close, the reference count minus 1, only if the reference count is reduced to 0 o'clock to actually close the socket.

The only way to run a file running on Linux is to call exec and replace the current process image with a new program file. Run from the program's main start.

The typical Linux approach is to start exec after fork. Often use cow technology.


About the zombie process

The process ends after exit (). The process table retains information such as the process number, exit status, execution time, and so on. Although it has given up memory space, it cannot be dispatched. Because Linux has a limited number of processes, too many zombie processes will consume the available process numbers. Causes the new process to fail to build, so it must be purged in a timely manner. I was wondering ... What is the meaning of the zombie process? A: Preserve the status of the child process exit and determine the cause of death while waiting for the parent process to corpse. (Calling various macros in the parent process: wexitstatus (status), etc.)

Processing method:

1. The parent process invokes wait () or waitpid () to wait for the child process to end. Of course this will cause the parent process to hang

2, assume that the parent process does not care when the child process ends, can call signal (Sigchld,sig_ign) to notify the kernel. The kernel recycles itself at the end of the child process.

Note: This method is not portable. Stevent mentioned in the UNP5.9. This processing is not a POSIX standard. is only available on some systems

3, the same way of using asynchronous processing. Signal processing function signal, install handler to process SIGCHLD signal. Such The parent process can call wait () in handler to recycle. However, because the same signal does not queue the reason, we should pay attention to the same time to deal with the signal submitted. That is, the child process terminated at the same time to do processing. UNP5.10 discussed the problem in detail. And a safe method is given.

4, by the signal processing function to set the SA_NOCLDWAIT flag. Do not create a zombie process when the child process of the calling process terminates (APUE10.14)

Signala.sa_handler = Sig_ign;signala.sa_flags = Sa_nocldwait;sigemptyset (&signala.sa_mask); Sigaction (SIGCHLD, &signala, NULL);

5, due to the characteristics of the process. After its parent process exits, the child process is passed on to the INIT process. Assume that the child process is already a zombie process. Init directly reclaims it, and Init does not care about the cause of death because its manager parent process no longer exists.

Therefore, it is possible to take two consecutive fork () ways, and the sub-process exits directly after the fork. Processed by the grandchild process, the resource is retrieved by Init after the grandchild process is finished. Note, however, that the cleanup of the child process is done by the parent process.


Signal Processing:

Under this summary. Each core signal, the operating system has the default processing mode, such as the previous SIGCHLD, the default is ignored, so do not handle will leave zombies. We are able to customize the function to handle the signal. Note that Sigkill and sigstop cannot be captured or ignored.

Signal processing functions are process-dependent, common to all threads of a process.

The signal processing function. Try not to use the non-reentrant function (UNP11.18) of printf

A signal is responded to by a signal processing function, which is masked during processing. The standard signal implementation does not have a queued function, so the signal may be lost, and multiple successive signals are too late to process. Assuming that the signal set is set by Sa_mask, the signal in the collection is also blocked.


The POSIX method for establishing a signal processing function is to call sigaction () and Simply call signal () directly. Although it is not a POSIX function, most platforms use signal () to implement Sigaction () for backwards compatibility.


The following is a simple multi-process server-side program:

#include "simon_socket.h" #define Serv_port 12345extern pid_t waitpid (pid_t, int *, int), void handler (int signo) {pid_t pid ; int status;while (PID = Waitpid ( -1, &status, Wnohang) > 0) {printf ("Child process%d terminated\nthe wexitstatus Return code is%d \nthe wifexited return code is%d\n ", PID, Wexitstatus (status), wifexited (status));}} int main () {int sockfd, acfd;struct sockaddr_in client_addr;size_t sin_len = sizeof (CLIENT_ADDR); sockfd = Init_tcp_psock ( Serv_port); signal (SIGCHLD, handler);/* Another method: *struct sigaction signala;signala.sa_handler = Sig_ign;signala. Sa_flags = Sa_nocldwait;sigemptyset (&signala.sa_mask); Sigaction (SIGCHLD, &signala, NULL); */while (1) {if (  ACFD = Accept (SOCKFD, (struct sockaddr *) &client_addr, &sin_len)) = =-1) {perror ("Accept request failed:"); return 1;} elseprintf ("Get a connection from%s:%d!\n", Inet_ntoa (CLIENT_ADDR.SIN_ADDR), Ntohs (client_addr.sin_port));p id_t pid; if (PID = fork ()) > 0) {close (ACFD); continue;} else if (PID = = 0) {Close (SOCKFD);p rocess_client (ACFD, &CLIENT_ADDR); close (ACFD); exit (0);} Else{perror ("Fork error"); exit (0);}} Close (SOCKFD); return 0;}

The implementation of some calling functions in the code, my GITHUB:HTTPS://GITHUB.COM/SIMON-XIA/LNP


Linux Network Programming Learning notes three-----Multi-process concurrent service side

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.