Multi-process programming in Linux (according to the summary in the Linux C programmer Guide)

Source: Internet
Author: User

The address space of a process includes a set of memory units that can be accessed or referenced. Usually controlled by PC pointer

This command is called a control point. A newer UNIX system supports multiple control points.

Multiple control points are called threads. Multiple control points are called multithreading. The thread exists in the process. The process is macro and the thread is micro.

The address space of a process is often virtual, and only part of the address space is mapped to the physical memory unit. Kernel Processes

The content (Code, etc.) in the address space is stored on a variety of storage objects, including physical memory, disks, and swap partitions.

The memory management subsystem of the kernel transfers the process's storage page between these objects.

Each process has a set of CPU Hardware registers. Read the register of the currently running process

Store the registers of other processes in the data structure of each process.

Each process has a unique PID.

A process table is a data structure used by the system to describe all processes currently loaded. You can use PS to view them.

Generally, processes are started by other processes (known as parent processes) and started processes become self-processes.

The earliest process is called init, pid = 1.

The time slice rotation method is used to allocate CPU usage among various processes.

When a process needs to call an external command or program, it needs to create a sub-process.
The simplest method is to use the system function, run shell commands, external programs (write the program name of external programs

To a bash script file, and then run the script in system.

Rows ).

After a child process is created with fork, the child process is detached from the parent process and executed according to its own process. Parent Process

The running program is the code in the IF (PID) in the code. When common code is used by the parent and child processes to use the CPU

. If the parent process of this public code is finished, the execution continues when the parent process uses the CPU time.

Line.
Sometimes the parent process has ended and the child process has not ended. You can use wait () to mount it in the IF (PID)

Wait until the sub-process ends to run wait () and the following code.
For details, see the P204 example.
A basic structure of fork:
PID = fork ();
Switch (PID)
{
Case-1;
Exit (1 );
Case 0: // sub-process
.... (Mainly set variables in sub-processes)
Default: // parent process
.... (Mainly set variables in the parent process)
}

... // Public code segment. Here, the Parent and Child processes use the CPU time alternately.

If (PID)
{
Wait (); // wait until the child process ends
.. // The parent process exits.
}

Botnets occupy the proc structure, reducing the total number of active processes. After the sub-process exits

Before the parent process clears it, it remains in the zombie state. In this state, it retains the unique resource

The source is the proc structure. The parent process calls wait to release the proc structure.
If the parent process exits before the child process, the INIT process will adopt the child process and clear the proc structure.
However, if the child process exits before the parent process, but the parent process does not call the wait () function, the proc of the child Process

The structure will not be released. The sub-process maintains a zombie state and occupies the proc structure until the system restarts.

 

 

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.