A simple understanding of * nix daemon and fork Functions

Source: Internet
Author: User

Recently, I took a closer look at the memcached instruction documents and looked at the source code. Sometimes I can skip some obscure C functions. However, it is not a success. Finally, LZ dared to read it again.C LanguageFinally, LZ saw the C language again, and the recent sleep quality was much better. Now, let's record your learning experience.

1. Unix daemon process

Memcached daemon uses the classic UnixDaemon mode(Daemon. c). Its source code is as follows:

 Memcached daemon. c # If defined _ sunpro_c | defined _ DECC | defined _ hp_cc # pragma ident" @ (#) $ Header:/cvsroot/Wikipedia/Willow/src/bin/Willow/daemon. C, V 1.1 2005/05/02 19:15:21 kateturner exp $ "# Pragma ident" $ NetBSD: Daemon. C, V 1.9 2003/08/07 16:42:46 AGC exp $ "# Endif # include <fcntl. h> # include <stdio. h> # include <stdlib. h> # include <unistd. h> # include"Memcached. h "Int daemonize (INT nochdir, int noclose) {int FD; Switch (Fork () {/* fork a sub-process */case-1: Return (-1 ); case 0: break; default: _ exit (exit_success);/* terminate parent process */} If (setsid () =-1) /* Create a new session (setsid) to make the current process the header process of the session */Return (-1); If (nochdir = 0) {If (chdir (" / ")! = 0) {/* change the working directory to the "/" root directory */perror (" Chdir "); Return (-1) ;}} if (noclose = 0 & (FD = open (" /Dev/null ", O_rdwr, 0 ))! =-1) {/* redirect standard input and output to/dev/null */If (dup2 (FD, stdin_fileno) <0) {/* copy the descriptors of stdin_fileno, stdout_fileno, and stderr_fileno to fd */perror (" Dup2 stdin "); Return (-1);} If (dup2 (FD, stdout_fileno) <0) {perror (" Dup2 stdout "); Return (-1);} If (dup2 (FD, stderr_fileno) <0) {perror (" Dup2 stderr "); Return (-1);} If (FD> stderr_fileno) {If (close (FD) <0) {perror (" Close "); Return (-1) ;}} return (0 );

The specific workflow is as follows:

Looking at the source code and processing flowcharts, it seems that they are also cool. When talking about the operating system, the teachers in the school must mention the well-known single-user, single-task operating system PC DoS (Disk Operating System ), A single-user single-task operating system means that one computer can only be used by one user at a time. The user can only submit one job at a time, and one user can enjoy all the hardware and software resources of the system. Unlike dos, Unix invented the fork function in 1970s to implement a multi-task operating system. In this way, Unix can process multiple tasks in parallel. Therefore, from the historical perspective of operating system development, every seemingly simple logic is a great leap. I still think it is very mysterious. It is necessary to copy a daemon concept to explain it to myself.

1. What is daemon process)
Daemon is a special process running in the background. It is independent of the control terminal and periodically executes a task or waits to process some events. It can run without user input and provide certain services, either for the entire system or for a userProgramProvide services. * Most nix servers are implemented through daemon. Common daemon processes include the system log process Syslogd, the Web Server httpd, the mail server Sendmail, and the database server mysqld. (do you see the illusion that "This TMD is not a Windows Service ?). The daemon generally starts running when the system is started. Unless it is forcibly terminated, it remains running until the system is shut down. Daemon often run with root privileges because they use special ports (1-1024) or access certain special resources.The parent process of a daemon is the INIT process. Because the real parent process exits before the fork child process exits, it is an orphan process inherited by init.. The daemon process is a non-interactive program with no control terminal. Therefore, any output from stdout to the standard output device or stderr to the standard error device requires special processing.

2. Working Principle
* The working mode of the nix daemon is server/client. The server listens on a specific port (Listen) and waits for the client to connect, after the connection is successful, the server and the client communicate through the port. The daemon is used to open a port and listen (Listen) to wait for the client to connect. If the client generates a connection request, the daemon creates a (fork) subserver to respond to the connection, and the master server continues to listen to other service requests.

After reading the above two points and descriptions, friends who are familiar with windows will surely think of Windows Services. Although the concept of sub-process in Windows is rarely mentioned, it does exist. As for whether the sub-process is implemented through fork in windows, I can only be left alone.

 

Ii. Fork Functions

From the memcached processing process, we can see that the first step to implementing the daemon mechanism is to fork a sub-process. I have read an article written by T2 in the garden.ArticleI was very impressed with a programming question about fork. The following is an example of a widely used fork function:

Aboutfork# Include <unistd. h >;# include <sys/types. h>; Main () {pid_t PID; // the return value of the fork function pid = fork (); // assign a value to the process ID through the fork FunctionIf(PID <0)Printf("Error in fork!");Else If(Pid = 0)Printf("I am the child process, my process ID is % d \ n", Getpid ());ElsePrintf("I am the parent process, my process ID is % d \ n", Getpid ());

 

The best part of this function is that, on the surface, only one of its conditions can be true (that is, only printing (printf) once ), the actual output is confusing for common developers who are not familiar with * nix, such as the zone. The actual output of running in Linux is:

I am the child process, my process ID is 3279
I am the parent process, my process ID is 3278

The output sequence may be different in different Linux kernel implementations, but two rows are always output. At first, I couldn't think of the reason why the two lines are printed out, because from the perspective of the general program running, a process is executed in order, no matter how many PIDs are, only one line should be printed, however, fork (fork) functions can break our mindset. Excerpt from the following sectionHow fork functions and sub-processes work:

A new process created by fork is called a sub-process (this process is almost a full copy of the current process ),The fork function is called once, but returns two. The only difference between the two responses is that the return value of the child process is 0, and the return value of the parent process is the ID of the new Child process.The reason for returning the child process ID to the parent process is: a process can have multiple child processes, in addition, this process is almost a full copy function of the current process. It is the process ID that a process obtains its sub-processes. The reason fork returns 0 in the child process is that a process can only have one parent process, and the ID of its parent process can be obtained through the getppid function. The sub-process and the parent process continue to execute the command after the fork call. The sub-process is a copy of the parent process. For example, a child process can obtain the data space, heap, and stack copies of the parent process.

The above paragraph seems more profound. In fact, if we have learned the operating system and C language, we should be able to understand its general meaning, of course, the concept of parent and child processes, that is, processes, must be correctly understood here.

So what is a process?

We may have seen many interview questions and reference books discussing what processes and threads are and the relationship between them. Here, we cannot avoid it. By referring to the process concept, we can better understand that a process has three pieces of data in the memory, namely"CodeSegment "," Stack segment ", and" data segment "are necessary components to form a complete executable unit. "Code snippet" stores program code data. If several processes on the machine run the same program, they can use the same code snippet. The "Stack segment" stores the return address of the subroutine, the parameters of the subroutine, and the local variables of the program. The data segment stores the global variables, constants, and dynamic data space allocated by the Program (for example, space obtained using functions such as malloc ).
With the development of the development language, many applications now use a relatively complete memory management mechanism (for example. net managed environment (CLR), Java virtual machine, etc.), so ordinary developers do not have to worry about memory allocation and management at ordinary times, therefore, some basic underlying things are not very clear. However, in fact, advanced languages must eventually be translated to native code by using a CPU like il (MS intermediate language) or bytecode, therefore, we need to maintain the "code segment", "Stack segment", and "data segment" in the memory during execution of programs written in advanced languages.

Now that the process requires memory maintenance, isn't it necessary to allocate more memory and maintain more data for creating sub-processes? Isn't the fork system overhead very large? Haha, this is my question. I am very unfamiliar with * nix. Although I have heard some explanations, I feel that I am not very thorough at all, here, I will keep my views for the time being. I hope you can read some materials for further study.

 

Next let's take a look at it and its simple data. It is said that the C function of the operating system can soon be killed:

 
VoidMain (){For(;) Fork ();}

How is the system killed? It is said that the principle is that this program does nothing, that is, fork in an endless loop, so that the program continuously produces processes, and these processes continuously generate new processes... As a result, the system generates many processes until the resources are insufficient and crash.

In fact, this endless loop is not the best in my opinion. Analyze the code above, for loop generates a sub-process at a time, loop two times to generate two sub-processes... In fact, the number of processes still increases by the number of algebra. Imagine if a function is executed once, then the process can generate sub-processes, and sub-processes will generate sub-processes ...... In this Recursive Execution, the process expands exponentially. Is this the legendary fork bomb? (how should I write this function? I can see one on the Internet. It's very short. Thank you for your answers )?

In general, the running mechanism of fork is still very interesting. In fact, I am most interested in how multi-process data can be shared and synchronized like multi-thread programming. Whether it is multi-process or multi-thread, data sharing and synchronization (Communication) have always been a top challenge. If you have time, make a good summary.

 

Refer:

Http://www.cnblogs.com/leoo2sk/archive/2009/12/11/talk-about-fork-in-linux.html

Http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html

Http://www.berlinix.com/unix_daemon.html

Http://www.phpx.com/happy/thread-131124-1-1.html

Http://kb.cnblogs.com/page/48194/

Http://code.google.com/p/memcached/wiki/NewCommands

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.