Multi-process of concurrent programming

Source: Internet
Author: User


Process theory

First, what is 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.
The same program executes two times, which is also two processes

Third, concurrency and parallelism

Parallel processing (Parallel processing) is a computational method that can perform two or more processes simultaneously in a computer system. Parallel processing can work in different aspects of the same program at the same time, and the main purpose of parallel processing is to save time to solve large and complex problems.

Concurrent processing (concurrency processing) refers to a time period in which several programs are running to completion, and these programs are running on the same processor (CPU), but only one program is running on the processor (CPU) at any moment.

The key to concurrency is that you have the ability to handle multiple tasks, not necessarily at the same time, the key to parallelism is that you have the ability to handle multiple tasks at the same time, so that parallelism is a subset of concurrency

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 in the PS command, Windows with Task Manager, the foreground process is responsible for interacting with the user, the background running process is not user-independent, running in the background and only when needed to wake up the process, 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.)

3. User's interactive request, and create a new process (such as user double-click Storm Video)

4. Initialization of a batch job (applied only in a 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:

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)

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.


V. Termination of the process

1. Normal exit (voluntary, such as the user clicks on the interactive page of the cross, or the completion of the program 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. Serious 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 processes in Unix are rooted in the init process and are formed into 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 that is 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. Regardless of the process, it is the operating system level that may invoke other processes to use the CPU because one process takes up too much time, or a priority, and so on.

Thus a process consists of three states


Viii. concurrent implementation of the process
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.

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.