Multi-process programming under Linux (summary according to the Linux C programmer guide)

xiaoxiao2021-03-06  41

The address space of the process includes a collection of memory cells that can be accessed or referenced. Generally controlled by PC pointer

Instructions in the process and tracking processes, this instruction is called a control point, and the new Unix supports multiple control points, this

A control point is the thread, and multiple control points are called multithreaded. There is a process in the process. The process is macro, thread is microscopic.

The address space of the process is often virtual, and only partially mapped to physical memory cells. Nuclear process

The content (code, etc.) in the address space is saved on various storage objects, including physical memory, disk, switched partition, and the like.

The kernel's memory management subsystem completes the transfer of the memory page between these objects.

Each process has a set of CPU hardware registers that correspond to the actual CPU hardware register. Read the register of the currently running process

To the hardware register, save the registers of other processes in the data structure of each process.

Each process has a unique process number PID.

Process Table is a data structure for describing all currently loaded processes, you can use PS to view them.

Generally, the process is started by other processes (called parent processes), started from the process.

The earliest process is called init, PID = 1.

The CPU is allocated between the various processes.

When a process needs to invoke an external command or a program, you need to create a child process. The easiest way to run the shell command with the System function, external programs (write the program name of the external program)

In a Bash script file, then run this script in System, you can use & make the program in the background

Row).

After creating a sub-process with the Fork, the child process is separated from the parent process, and performs it according to your own process. Parent process

The running program is the code in the IF (PID) in the code. When the public code is alternately using the CPU by the father and child process

between. If the public code parent process is executed, then when the parent process uses the CPU time, continue to

Row. Sometimes the parent process is over, the child process has not ended, and it can be used in the back IF (PID).

Start, wait for the sub-process to start executing Wait () and the following code. See example of P204 for details. One basic structure of Fork: PID = fork (); Switch (PID) {case -1; exit (1); case 0: // child process. . . . (Mainly a variable setting in the child process) Default: // Parent process. . . . (Mainly the variable settings in the parent process)}

. . . // Public code segment, where the parent child process is alternately using the CPU time.

IF (pid) {wait (); // Waiting for the sub-process end. . // Last parent process exit}

The zombie process takes up the Proc structure, thus reducing the total number of processes in the active state. After the child process quits

Before the parent process cleared it, it has been in a zombie state, in which the only piece it is reserved.

The source is the PROC structure. The parent process calls WAIT to release the Proc structure. If the parent process quits with a child process, the init process will adopt a child process to clear the PROC structure. But if the child process exits at the parent process, the parent process does not call the wait () function, the procor of the child process.

The structure will not be released, the child process maintains a zombin state and occupies the Proc structure until the system restarts.

转载请注明原文地址:https://www.9cbs.com/read-74434.html

New Post(0)