Python of course: Basic Concepts

First, the basics

As the name implies, a process that is a process being executed. Process is an abstraction of a running program.

The concept originated in the process of the operating system, is the core concept of the operating system, one of the oldest and most important abstraction is the operating system. All other operating systems are all around the concept of the process of unfolding.
  
So you want to really understand the process, you must know in advance the operating system

PS: Even if you can only use a cpu (early computer is true), but also to ensure the ability to support the (pseudo) concurrently. Will become a separate multiple virtual cpu cpu (multi-channel technologies: time division multiplexing and spatial multiplexing isolation hardware + support), there is no process of abstraction, modern computers will cease to exist.

The necessary theoretical basis:

A 操作系统role:
1: hide ugly complex hardware interface, providing good abstract interface
2: management, scheduling process, and multiple processes competing hardware becomes ordered =

Two 多道技术:
1. Background: For single-core, concurrency
ps:
now hosts are generally multi-core, each core will use multi-channel technology
with four cpu, io encountered obstruction to run a program cpu1, we will wait until io end of the re scheduling, will be scheduled to 4
of any one cpu specific algorithm determined by the operating system scheduler.
2. spatial multiplexing: the memory as multiple concurrent channel programs
multiplexed on 3. Time: time-multiplexed slice of a cpu
stressed: io encountered cut, also occupied cpu cut too long, the core is cut before save the state of the process down, so
as to ensure the next time you switch back, can continue to operate based on the last go-cut position


Second, what is the process

进程(Process)The computer program is run on a certain activity on the collection of data is the basic unit of resource allocation and scheduling system is the underlying operating system architecture. In the computer architecture for the early design process, the basic process is the execution of the program entity; in contemporary computer architecture design for the thread, the thread is the process vessel. Program is instruction, organization of data and its description of the process is a solid program. We wrote in the python file in some code, this is called a program, when run the python file, which is called process.

Narrow definition: the process is an instance of a program running (an instance of a computer program that is being executed).

Broadly defined: a process is run activities must have a separate function on a data collection program. It is the basic unit operating system dynamically performed in traditional operating systems, the process is both a basic allocation unit, is the basic unit of execution.

Example: For example py1 file has a variable a = 1, py2 file contains variables a = 2, the two of them will conflict? No, is not it, because the two documents is up and running two processes, the operating system in memory so that they isolate, right.

The difference between the process and procedures:

Program is an ordered set of instructions and data, which does not have any operational meaning, is a static concept; and the process is a program on the processor implementation process, it is a dynamic concept.

A software program can be used as long-term data exist, but there is a certain process life cycle.

Program is permanent, the process is temporary.

Example: Like qq, qq is our client program installed on their computers, in fact, a bunch of code files, we do not run qq, then he is a bunch of code program, when we run qq, the code up and running, it becomes a process of.

Note: Perform the same program twice, there will be two processes in the operating system, so we can run a software at the same time, each doing different things will not be confused.


Three process scheduling

To run multiple processes alternately, the operating system must be scheduled for these processes, the scheduling is not carried out immediately, but need to follow certain rules, thus there is a scheduling algorithm process.

FCFS scheduling algorithm:

First-come, first-served (FCFS) scheduling algorithm is a simple scheduling algorithm for job scheduling can also be used for process scheduling. FCFS algorithm is more conducive to a long job (process), to the detriment of short job (process). It can be seen that the algorithm is suitable for CPU busy type work, to the detriment of heavy I / O type of job (process).

Short operating priority scheduling algorithm:

Short job (the process) scheduling algorithm and (SJ / PF) refers to short or short process priority job scheduling algorithm for job scheduling can also be used to schedule processes. But its work for long; not guarantee the urgency of the job (process) to be in time; the length of the job is only to be estimated.

Round-robin method:

Round-robin (Round Robin, RR) method basic idea is to have each process in the ready queue waiting time and enjoy the service proportional to the time. In the round-robin method, the processing time of the CPU time slice needs to be divided into a fixed size, e.g., several tens of milliseconds to hundreds of milliseconds. If a process run out of time slice prescribed system after being selected schedule, but not required to complete the task, it is the release of their self-occupied CPU and discharged at the end of the ready queue, waiting for the next scheduling. At the same time, the process scheduler went Scheduling a process currently in the queue ready.

Clearly, round-robin scheduling can only be used to allocate some resources can be preempted. These resources can be preempted at any time be deprived, and they can be reassigned to another process. A CPU is to seize resources. However, printers and other resources can not be preempted. Because the job scheduling assignment to all the system hardware resources, in addition to the CPU, which comprises a non-preemptive resources, it does not use rotation job scheduling method.
In the rotation method, the length of the time slice selected is important. First, select the length of the time slice will directly affect the response time and system overhead. If the chip length is too short, the scheduler increases the number of processors preemption. This will make the process context switches also greatly increased, thereby increasing the system overhead. Conversely, if the time slice selected length is too long, for example, to ensure that a time slice execution time required for the longest queue in the ready process can be executed, into the round-robin method a first come first served. Chip length is selected to determine the system response time requirements of the ready queue and the maximum number allowed in accordance with processes.

In the round robin, adding to the process ready queue are three cases:
one is to divide its time slice is used up, but the process has not been completed, return to the end of the ready queue waiting for the next scheduled execution to continue.
Another case is the time slice given to the process does not run out, but since the request I / O or due to the synchronization relationship with the exclusive process is blocked. After unblocking return to the ready queue.
The third case is that the new process is created to enter the ready queue.

If the difference between these treatment processes, giving different priorities and time slice from a visual point of view, the system can further improve service quality and efficiency. For example, we may be the ready queue in accordance with the process of arriving at the reasons for blocking the ready queue and the type of process is blocked into different ready queues, each arranged according to the principle of FCFS, process among different priority queues enjoy, but at the same the same priority queue. In this way, when a process after the completion of the implementation of its time slice, or wake up from sleep and being created, enter different ready queue.

Multilevel feedback queue:

As a variety of process scheduling algorithm described earlier have certain limitations. Such as short process priority scheduling algorithm, only to take care of the short process while ignoring the long process, but did not specify if the length of the process, the short process priority-based preemptive scheduling algorithm and the length of the process will not be used.

The multi-level feedback queue scheduling algorithm is not necessary to know in advance the time required to perform a variety of processes, but also to meet the needs of various types of processes, so it is now being recognized as a better process scheduling algorithm. In systems that employ multiple feedback queue scheduling algorithm, the scheduling algorithm implementation process is as follows.

(1) a plurality of ready queues to be provided, and given different priority for each queue. The first has the highest priority, followed by a second queue of queues, each queue priority remaining decreased by one. The algorithm gives each queue process execution time slice size can vary, the higher the priority queue, the execution time specified for each sheet the less process. For example, the second queue time slices than a first time slice twice as long as the queue, ......, i + 1-th time slot queue than the queue i-th time slice twice as long.

(2) When a new process into the memory, it is first placed into the first end of the queue, waiting to be scheduled according to the principle of FCFS queuing. When it came time to perform the process, as it can be completed within the time slice, you can prepare evacuation system; if it is not completed at the end of a time slice, the process scheduler then transferred to a second end of the queue, and then Likewise waiting to be scheduled according to the principle of FCFS performed; if it is running in a second time slot queue is not yet completed, then turn it into a third queue, ......, and so on, when a long job (process) from after a queue of n sequentially down queue, the queue will be taken at the n-th time run round-robin.

(3) only when the first queue is empty, the process scheduler dispatches a second run queue; only if the first 1 ~ (i-1) when the queue was empty, the scheduler will process the i-th run queue. If the processor is the i-th process in the queue for a service, but also a new process into the high priority queue (first to (any queue i-1)), the new process at this time will preempt running the processor processes, i.e., the process by the scheduler is running back into the end of the queue i, allocated to the processor to process the new arriving high priority.

For multi-level feedback queue, windows is not clear, but you can set the priority of a process in linux inside, improve the limited level will be more likely to perform several time slots.


Fourth, concurrent and parallel

By scheduling between processes, that is, switching between processes, we are perceived by the user as if two video files simultaneously in play, or at the same time making music and games, let us look at what is called concurrency and parallelism

Either parallel or concurrent, users seem 'while' running, whether it is a process or thread, are just a task it really work is cpu, cpu to do these tasks, while the same time, only a cpu perform a task

Concurrency : a pseudo-parallel, that appear to be run simultaneously. Cpu + single multi-channel technology can be achieved concurrently, (also a concurrent parallel)

Parallel : Parallel: run, only be achieved in parallel with multiple cpu

Multiple cpu, multiple processes in parallel, for example:

Multiple cpu to have more lanes on the highway, the process is like, the parallel is to say, everyone traveling in their own lane, it will not impact than running on each lane of the vehicle while driving. This is the parallel

Nucleation single, multi-channel technique may be utilized, a plurality of cores, each core may also use multi-channel technology ( multi-channel technology for mononuclear terms )

There are four cores, six tasks, so at the same time there are four tasks to be performed, assuming that each is assigned to cpu1, cpu2, cpu3, ​​cpu4,

Once the task 1 encounter I / O was forced to interrupt execution, then the task will get 5 cpu1 time slice to execute, which is the multi-channel technology of single Nucleation

Once the task 1 I / O is over, the operating system will recall it (Notes scheduling process, which is assigned to run the cpu, the final say by the operating system), it may be assigned to any of the four cpu in one go carried out


Modern computers often have to do a lot of things at the same time, a user's PC (whether single or multi-cpu cpu), can run multiple tasks (a task can be understood as a process).

  • Start a process to antivirus (360 software)
  • Start a process to watch movies (STORM)
  • Start a process to chat (Tencent QQ)

All of these processes need to be managed, so multiprogramming system that supports multi-process is critical

Review of the technical concept of multi-channel: simultaneous memory stores multi-channel program (s), cpu quickly switch from one process to another, so that each process runs each tens or hundreds of milliseconds, so that, although in an instant, a cpu can only perform one task, but in one second, but cpu can run multiple processes, which gives the illusion produced a parallel, that pseudo-parallel, in order to distinguish the true hardware parallelism of multiprocessor operating system ( a plurality of cpu share the same physical memory)


V. Synchronous \ asynchronous \ blocking \ non-blocking (focus)

1. Introduction Process Status

Before understanding other concepts, we first need to understand a few state of the process. The program is running, since the scheduling algorithm is the operating system of control, the program will enter several states: ready, running and blocking.

  • (1) Ready (Ready) state: When the process has been assigned to all the necessary resources in addition to the CPU, as long as the processor can get immediate execution, when the process state called the ready state.

  • (2) execution / operation (Running) state: When the process has obtained the processor, which processor is executing the program, the state of the process at this time is referred to as an execution state.

  • (3) blocked (Blocked) state: the process of being executed when an event occurs because the wait can not be executed, and they give up the processor is blocked. Cause the process to block a variety of events, such as waiting for I / O completion, the application does not meet the buffer, wait for a letter (signal) and so on.

Event inputrequest: sleep, 文件输入输出, recv, , accept, etc.

Events:sleep , inputand so completed

After the pieces have time to return to the ready state, three in a state of constant conversion.

2. Synchronous Asynchronous

The so-called synchronization is to complete a task relies on other tasks, just waiting to be dependent on the completion of the task, the task can be considered dependent on the completion of this task is a reliable sequence. Either succeed are successful, failed and failed, two state of the task can be consistent. In fact end before the implementation of a program to another program, serial, two programs do not necessarily have dependencies.

The so-called asynchronous is no need to wait to be dependent on completion of the task, but notification is dependent on what work tasks to complete, depending on the tasks executed immediately, as long as they completed the task is complete. As a final task was dependent on whether truly complete, depending on its mission can not be determined, so it is unreliable task sequence.

Synchronous asynchronous example :

For example, we went downstairs to eat meatloaf home, a good meal points, taking meals when there have been some things asynchronous synchronization:

Sync: We all stood in line waiting to get food in front of someone ordered a meatloaf , Houchu do for a long time, but due to synchronization mechanisms, we still have to wait for the team to stand in front of that person's meatloaf do take away, we go one step further.

Asynchronous: We point after completing meal, order-taker gives us a number to take meal, tell you, you do not have it here waiting in line, to find a place to sit and play Go mobile phones, such as rice well, my name is you. This mechanism (waiting for others to notice) is waiting for asynchronous message notification. In asynchronous message processing, the message notifier wait (waiting to take meals in this case you) tend to register a callback mechanism when waiting for the event to be triggered by a trigger mechanism (taker) by some mechanism (call number '250 your buns good') to find people waiting for the event.

3. blocking and non-blocking

Blocking and non-blocking state when these two concepts and procedures (thread) to wait for message notification (does not matter synchronous or asynchronous) related. That is the main blocking and non-blocking state when the angle of the program (thread) to wait for the message notification

Continue the example above, use either queuing or waiting notification number, if in the waiting process, in addition to those waiting outside to wait for a message notification can not do other things, then the mechanism is blocked, the performance in the program, also the program is unable to continue down the implementation has been blocked at the call of the function.

On the contrary, some people like playing video games while waiting to take meal while waiting, such a state is non-blocking, because he (waiter) is not blocked on the message notification, but while doing their own thing while waiting.

Blocking methods: input, time.sleep, socket in recv, accept, and so on.

4. The synchronous / asynchronous and non-blocking and blocking

1. synchronous blocking form

Minimum efficiency. Take the example above, is to concentrate on your line, do not do anything else.

2. Asynchronous blocking form

If people queued meal is used in an asynchronous way to wait for the message is triggered (notification), which is picking up a piece of paper, if at this time he can not do something else, I sit in that the waiting, not play games, then it is clear that this person is blocked in this operating above waiting;

Asynchronous operation can be blocked to live, but it is not blocked while processing a message, but the message is blocked while waiting for notification.

3. Synchronize the form of non-blocking

It is actually inefficient.

Imagine that while the name of the phone while you also need to look up in the end the team routed you do not, if the call queuing and observation positions as two operating procedures, then this program needs two different behaviors switching back and forth between, imagine efficiency is low.

4. A non-blocking asynchronous form

More efficient, because you are calling (waiters) thing, and you notice is the counter (news trigger mechanism) things, the program does not switch back and forth in two different operations.

For example, this person suddenly find themselves guilty of addiction, need to go out a cigarette, and he told taker said, my number is routed to the outside when the trouble to inform me, then he is not blocked in this waiting operating above, this is the natural way of non-blocking asynchronous +.

Many people will be confused and blocking synchronization, because a lot of time synchronization will show to block out the form , the same, many people will confuse the asynchronous and non-blocking, because asynchronous operations are generally not in the real IO operations at blocked .


Sixth, the creation process, and the end of concurrency

1. Create a process

But all the hardware, the operating system needs to have to manage, as long as the concept of an operating system, there is a process, you need to have a way to create a process, some operating systems designed for only one application, such as a microwave oven controller, once start a microwave, all processes already exist.

As for the general system (running many applications), you need to have the ability to create or revocation process during system operation, mainly divided into four kinds of forms to create a new process:

  1. System initialization (see the process with ps command linux, windows with Task Manager, foreground process is responsible for interaction with the user, the process running in the background relevant to the user, runs in the background and only when needed wake-up process, called daemons such as email, web pages, news, print)
  2. A process opens a child process during operation (such as nginx open multi-process, os.fork, subprocess.Popen, etc.)
  3. Interactive user requests, and create a new process (such as the user double-STORM)
  4. A batch initialization operations (application only on mainframe batch system)

Either way, create a new process is the implementation of a system for creating process is called by an existing process created:

  1. The system call in UNIX is: fork, fork creates an exact copy of the parent process, both have the same memory map, the same environment and the same string to open the file (in the shell interpreter process, execute a command creates a child process)
  2. The system call in windows are: CreateProcess, both the process of creating the process CreateProcess, is also responsible for the correct program into the new process.

About child process created, UNIX and windows of the similarities and differences:

  1. Is the same: the process is created, the parent and child processes have different address spaces (technical requirements for multi-channel physical level to achieve isolation between process memory), a process to modify any of its address space will not affect Another process.
  2. The difference is: in UNIX, the initial address space of the child process is a copy of the parent process, suggesting that: the child and the parent process can have read-only shared memory area. But for the windows system, from the beginning of the address space of a parent and child process is different.

2. The process of ending

  1. Normal exit (voluntary, if the user clicks crosses interactive page, or call the procedure is finished to initiate normal exit system call, with the exit linux, in windows using ExitProcess)
  2. Error exit (voluntary, python a.py in a.py does not exist)
  3. Serious error (involuntary, to execute an illegal instruction, such as references to nonexistent memory, 1/0, etc., you can catch the exception, try ... except ...)
  4. Killed by another process (involuntary, such as kill -9)

3. The process of concurrent implementations

Implement concurrent processes that hardware interrupt a running process, the preservation of the status of all processes running at this time, to this end, the operating system maintains a table that process table ( process table), each process occupies a process table entry (these are also referred to as the entry process control block)

The process status table to store the important information: 程序计数器, , 堆栈指针, 内存分配状况, 所有打开文件的状态time,帐号和调度信息 and other process into the ready state or blocked state run by the state, must save the information to ensure that the process start again, just never been He interrupted the same.


Reference: Python's process 1 - Basic concepts

Published 58 original articles · won praise 4 · Views 1944

Guess you like

Origin blog.csdn.net/weixin_43999327/article/details/104044491