Process--fork,exec in Linux, daemon analysis

Source: Internet
Author: User

The classic three-state operating system is as follows:

1. Ready state

2. Waiting (blocking)

3. Operating state

Its conversion status is as follows:

multiple queues are maintained in the operating system kernel, and processes of different states are added to different queues, where revocation is resumed by the kernel after the process has finished running.

The above three states are given in the operating system principle, but the platform of each operating system to achieve these states are different, such as the Linux operating system in the status of the following several types of processes:

1. Operating Status (task_running)

2. Interruptible sleep status (task_interruptible)

3. Non-disruptive sleep status (task_uninterruptible)

4. Suspend status (task_stopped)

5, Zombie State (Task_zombie)

The transitions between these states are as follows:

The process state in Linux contains the classic three-state, but the classification is more granular, can interrupt sleep and non-disruptive sleep corresponds to the blocking state, the ready state is also considered as a running state, also used task_running identification, and the operating state is divided into user space operation and kernel space operation State, In addition, there are more paused and zombie states.

Small knowledge:

When the Linux kernel is loaded, it will create itself a process of No. No. 0, which is created in a different way from the normal process and then creates a process number 1th, one of which is/sbin/init. The maximum number of processes in Linux is limited and can be viewed by command Cat/proc/sys/kernel/pid_max, with the general default being 32768.

A process is an abstraction of an operating system's resources, a process that includes code snippets, data segments, stack segments, and process control blocks, which are an important data structure for the operating system management process. A process can only correspond to one program (a program here refers to a loadable executable program.) If a program is a file, then a process can correspond to multiple programs (files), for example: loadable executables and multiple dynamic library programs (files) can be run in a process), a program can correspond to multiple processes.

When you create a system call to a process in Linux, the Fork,fork function is called once, two times back, and the child processes and parent processes are returned in their own user space. When you fork a new process, what are the specific things that are generated or copied? In the case of cow (copy-on-write), when you fork a process, the kernel creates a new PCB (Process Control block), and copies the data segments, stack segments, etc. of the parent process. The newly created process enters the ready queue in the kernel.

You can use the man command to see how the fork is used. The man commands in Linux share the following chapters:

When fork fails, it returns only once, the return value is-1, and the global errno is set. When Fork returns successfully, the parent process returns the PID of the child process, which allows the parent process to know the PID of the child process, facilitates the control of the sub-process, the parent process may fork many child processes, and the child process ID is returned to the parent process so that the parent process can organize the PID save. can easily control the sub-process, if not, the parent process to find a child process may need to traverse a lot of complex data structures, adding complexity. The child process returns 0 when it returns from fork. Parent and child processes are a one-to-many relationship, and it is convenient for a child process to get the PID of the parent process.

Process--fork,exec in Linux, daemon analysis

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.