Linux Network Programming Study Note 3

Source: Internet
Author: User

The first step is the fork () function. For more clear explanations, see explain.

In addition, after fork returns, the reference count of each file or set of interface descriptors in the original process is added with 1 (equivalent to being opened once more), and each call is closed, the reference count is reduced by 1. The socket is actually closed only when the reference count is reduced to 0.

The only way to run an executable file by linux is to call exec, replace the current process image with a new program file, and run it from the main of the program.

A typical linux practice is to start exec after fork, usually using COW technology.


About zombie Processes

After a process ends with exit (), its information such as process number, exit status, and running time is retained. Although it has abandoned the memory space, it cannot be scheduled. Due to linux's restrictions on the number of processes, too many zombie processes will occupy the available process number, resulting in the failure to generate new processes, so they must be cleared in time. At that time, I was wondering... What is the significance of the existence of a zombie process? A: Keep the exit status of the sub-process and wait for the parent process to determine the cause of death. (Call various Macros in the parent process: WEXITSTATUS (status) and so on)

Processing Method:

1. The parent process calls wait () or waitpid () to wait until the child process ends. Of course, this will cause the parent process to stop.

2. If the parent process does not care about when the child process ends, you can call SIGCHLD (SIG_IGN) to notify the kernel that the kernel will automatically recycle the child process after it ends. Note: This method is not portable. Stevent mentioned in UNP5.9 that this processing is not a POSIX standard, but is only available on some systems.

3. asynchronous processing is also used. The signal processing function is signal, and handler is installed to process SIGCHLD signals. In this way, the parent process can call wait () in handler to recycle it. However, because the same signal is not queued, you must handle the simultaneously submitted signal, that is, processing the child processes that are terminated at the same time. UNP5.10 discusses the problem in detail and provides a security method.

4. Set the SA_NOCLDWAIT flag for the signal processing function. Make the sub-process of the called process stop without creating a zombie process (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 the parent process exits, the child process will be passed to the init process. If the child process is already a zombie process, init will directly recycle it, because the parent process of the Administrator does not exist, init does not care about the cause of death. Therefore, you can take the fork () method twice in a row. The child process exits directly after the fork and is processed by the sun process. After the sun process ends, the resource is recycled by init. But note that the cleanup of sub-processes must be completed by the parent process.


Signal Processing:

In this summary, the signal and operating system of each kernel have the default processing method. For example, the previous SIGCHLD is ignored by default, so no botnets will be processed. We can define functions to process signals by ourselves. Note that SIGKILL and SIGSTOP cannot be captured or ignored.

The signal processing function is related to the process and is public to all threads of the whole process.

Do not use non-reentrant functions such as printf (UNP11.18) in signal processing functions)

A signal is responded by a signal processing function. during processing, the signal is blocked. The standard signal implementation has no queuing function, so the signal may be lost, and multiple consecutive signals cannot be processed. If a signal set is set through sa_mask, the signal in the set will also be blocked.

The POSIX method used to create a signal processing function is to call sigaction () and directly call signal (). Although it is not a POSIX function, most platforms use signal () to implement sigaction (), to achieve backward compatibility.


The following is a simple multi-process server 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));pid_t pid;if ((pid = fork()) > 0){close(acfd);continue;}else if(pid == 0){close(sockfd);process_client(acfd, &client_addr);close(acfd);exit(0); }else{perror("Fork error");exit(0);}}close(sockfd);return 0;}

Some code calls the function implementation, move to my github: https://github.com/simon-xia/lnp


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.