Multi-process of concurrent programming

Source: Internet
Author: User

Process Theory

First, the process

Process: A process that is in progress or a task. The responsibility for executing the task is CPU.

Ii. The difference between process and procedure

A program is just a bunch of code, and a process refers to the process of running a program.

need to emphasize is : the same program executes two times, that is also two processes, such as Open Storm video, although all are the same software, but one can play Cang Jing empty, one can play Rice Island love.

third, concurrency and parallelism

Whether it is parallel or concurrent, in the view of the user is ' simultaneously ' running, whether it is a process or a thread, is just a task, really work is cpu,cpu to do these tasks, and a CPU can only perform one task at a time.

    • Concurrent

is pseudo-parallel, which appears to be running concurrently. Single cpu+ multi-channel technology enables concurrent

    • Parallel

Run concurrently, only with multiple CPUs to achieve parallelism

Iv. Creation of processes

But all hardware, need to have the operating system to manage, as long as there is an operating system, there is the concept of process, you need to have a way to create processes, some operating systems for a single application program design, such as the microwave oven controller, once the microwave oven, all processes are already there.

For general-purpose systems (running many applications), there is a need to have the ability to create or revoke processes in the process of running the system, mainly divided into 4 forms to create new processes

      1. System initialization (view process Linux with the PS command, Windows with Task Manager, the foreground process is responsible for interacting with the user, the process running in the background is not user-independent, the process that runs in the background and wakes only when needed, called daemons, such as e-mail, Web pages, news, printing)

      2. A process in the process of running a child process (such as Nginx open multi-process, os.fork,subprocess. Popen, etc.) Our main research object

      3. User's interactive request, while creating a new process (such as user double-click Storm Movie)

      4. Initialization of a batch job (applied only in the mainframe batch system)

Either way, the creation of a new process is created by an already existing process that executes a system call to create the process:

      1. In Unix, the system call is: Fork,fork creates a copy that is identical to the parent process, with the same storage image, the same environment string, and the same open file (in the shell interpreter process, the execution of a command creates a child process)

      2. In Windows, the system call is: Createprocess,createprocess processes both the creation of the process and the process of loading the correct program into a new one.

About creating child processes, UNIX and Windows

1. The same is true: After a process is created, the parent and child processes have their own different address spaces (the multi-channel technology requires the physical plane to implement the isolation of the memory between processes ), and the modification of any one process in its address space does not affect another process.

2. The difference is that, in Unix, the initial address space of a child process is a copy of the parent process, which indicates that the child process and the parent process can have read-only shared memory areas. However, for Windows systems, the address space of the parent process and the child process is different from the beginning.

The UNIX parent process copies the data to the child process, the initial state of the child process is identical to the parent process, and the Windows parent process copies the data to the child process, but differs from the parent process.

V. Termination of the process (understanding)

    1. Normal exit (voluntary, such as the user clicks on the interactive page of the cross, or the program has completed the call to initiate system calls to exit normally, in Linux with exit, in Windows with ExitProcess)

    2. Error exiting (voluntary, a.py does not exist in Python a.py)

    3. Critical Error (involuntary, execution of illegal instructions, such as referencing non-existent memory, 1/0, etc., can catch the exception, try...except ... )

    4. Killed by other processes (involuntary, e.g. kill-9)

VI. hierarchical structure of processes

Regardless of Unix or Windows, the process has only one parent process, and the difference is:

    1. All of the processes in Unix are rooted in the init process and form a tree structure. Parent-child processes collectively form a process group so that when a signal is emitted from the keyboard, the signal is sent to all members of the current keyboard-related process group.

    2. In Windows, without the concept of process hierarchy, all processes are of the same status, and the only hint similar to the process hierarchy is that when the process is created, the parent process gets a special token ( called a handle) that can be used to control the child process. But the parent process has the right to pass the handle to the other child processes, so there is no hierarchy.

vii. Status of the process

Tail-f access.log |grep ' 404 '

Execute the program tail, open a subprocess, execute the program grep, open another subprocess, two processes based on pipe ' | ' Communication, tail the result as input to grep.

The state of process grep while waiting for input (i.e. I/O) is called blocking, when the grep command cannot be run

In fact, in both cases, a process cannot be logically run,

    1. Process hangs is its own cause, encountering I/O blocking, it will let the CPU to allow other processes to execute, so that the CPU has been working

    2. Process-Independent, OS-level, may call other processes to use the CPU because a process takes up too much time, or a priority, and so on.

Thus a process consists of three states

VIII. Implementation of process concurrency (understanding)

The implementation of the concurrency of the process is that the hardware interrupts a running process, saving all the state at which the process is running, and for this reason, the operating system maintains a table, the process table, which consumes a process table entry (these are also referred to as Process Control blocks)

This table holds important information about the status of the process: program counters, stack pointers, memory allocations, status of all open files, account and dispatch information, and other information that must be saved when a process is turned into a ready or blocked state, ensuring that the process starts again as if it had never been interrupted.

Open process two ways

Multi-process of concurrent programming

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.