1.5 Process

1. Process Definition

1. The process is the smallest unit of resource allocation

2. When an executable program is executed (allocating memory and other resources) system becomes a process

Process Definition answer expand contents

1. The program can not be run separately, only the program is loaded into memory, the system allocates resources to run it, the program will be executed this process is called

2. The difference is that the procedures and processes: the program is a set of instructions, it is static text describing processes running; the process is a program of implementation activities, is the dynamic concept

3. In the multi-channel programming, we allow multiple programs loaded into memory, the operating system in the scheduling, can be implemented concurrently.

4. The process appears to make every user feel exclusive CPU, therefore, the process is to achieve the multi program on the CPU and raised.

It has its own separate memory processes between 5 and can not access each other between the various processes

6. Create a new thread is simply to create a new process requires the parent to copy

Multi-channel programming concepts

Multi-channel programming: store while a few independent programs in computer memory, they shared system resources, interspersed with each other run

Single channel programming: a memory of the computer program runs only

2. With the process of why a thread?

1. Process advantages:

Provides multi-channel programming, let us feel that each of us has its own CPU and other resources, can improve the utilization of the computer

2. The process of two important shortcomings

a first point: progress can only do one thing at a time, if you want to do two things or more things at the same time, the process can not do anything.

b The second point: the process in the course of execution, if blocked, even if some of the work process does not depend on the input of data, will not be performed (eg waiting for input, the whole process will be suspended).

c. For example, we use qq chat, qq as a separate process, if at the same time can only do one thing, that he realized how at the same time is able to monitor keyboard input, but also other people listening to your messages

d. You would say, not a time-sharing operating system it? Sharing means sharing between the different processes it

E. When that is the operating system will handle your qq a task, then switched to a word document tasks, each cpu time slice allocated to your program qq, qq you still can only do one thing at the same time it

3. The four methods of inter-process data access to each other

Note: different process memory is not shared, they can not access other data between each other

Method 1: Using Queues achieve the parent to the child (or between the child process) data transfer

Method 2: Use conduit pipe implement data transfer between two processes

Act 3: Managers share data among many processes

Method 4: data sharing means redis middleware

4. The process pool

from  multiprocessing import Process,Pool
import time, the
def foo(i):
    time.sleep(2)
    pid print ( "in the process", os.getpid ()) # print the child process
return i+100
 
def call(arg):
print('-->exec done:',arg,os.getpid())
 
if __name__ == '__main__':
    pool = Pool (3) # process pool allows up to five process into the process pool
    print ( "main process pid:", os.getpid ()) # print the parent process pid
    for i in range(10):
       # Usage 1 callback function is specified callback function when the function only after the end of Foo run execution callback call, call the parent process
        pool.apply_async(func=foo, args=(i,),callback=call)
        Usage Serial # 2 to start the process with Process but not directly pool.apply ()
        # pool.apply(func=foo, args=(i,))
    print('end')
    pool.close () # close the pool
    Pool process pool.join () # close before the process is finished, if the comment, then shut down the program directly.

 

The difference between the processes and procedures

1. The procedure is an ordinary file, a collection of machine code instructions and data, so that the program is a static entity

2. The process is a dynamic process running on a data set, the process is a dynamic entity, it should create and produce, waiting to be scheduled for execution due to resource or event is in a wait state, because the task is revoked

3. The process is an independent unit of system resource allocation and scheduling

4. A program corresponding to a plurality of processes, a process for multiple programs and services (many to many relationship between the two)

The program is executed on a different set of data becomes different processes, process control block can be used to uniquely identify each process

Guess you like

Origin www.cnblogs.com/lihouqi/p/12664232.html