Concurrent programming history --- operating system development process

History of operating system development

The computer consists of:

input device

Output Device

CPU

1. punchcards

A computer room can only use one card

Drawback: CPU utilization is very low

2. Batch Online

It supports multiple users to use a computer room

3. off-line batch processing

High-speed disk: improve reading speed file

Benefits: increase CPU utilization

4. Multi-channel technology

Single Channel: must be allowed after the end of a program, a program before proceeding to the next

Multi-channel technology: allow multiple programs into the memory, and allowing them alternately run in the CPU

Multiplexing (focus) on 1. Space: a CPU may be provided to a plurality of users to use

Multiplexing (focus) on the 2 time: switching state of preservation +

Note: encountered IO switch, take a long time to switch CPU core that will save the state before cutting down the process, so as to ensure that the next time you switch back, can continue to operate based on the last go-cut position

IO operations: inpot (), print (), time.sleep (3).

(1) If the CPU has encountered an IO operation, the program will execute the current CPU use right off immediately,

Advantages: High CPU utilization

(2) If a program uses the CPU time is too long, the right to use the CPU program currently executed immediately disconnect

Disadvantages: execution rate reduction program

process

1. procedures and processes:

Program: a bunch of code

A bunch of code running process: Process

Difference: the implementation of the same program twice, there will be two processes in the operating system, so we can run a software at the same time, it will not mess are doing different things.

2. The process features

Dynamics: the essence of the process is the execution of a program in a multiprogramming system, the process is dynamically generated, dynamic extinction.

Concurrency: any process can be executed concurrently with other processes together

Independent unit process is a basic unit can operate independently, but also the system resource allocation and scheduling: independence

Asynchronous nature: the mutual restraint between processes, so that the process has intermittent execution of that process by independent, unpredictable pace to move forward.

Structure: three partial blocks of the process control program, data, and processes the composition

3. The process of scheduling

Now OS: round-robin queue feedback fractionation method +

1. FCFS scheduling.

a, b program, if a first-come, first occupied CPU.

Disadvantages: a program must first use the program b must wait for the end of the program in order to use a CPU use

2. short operating priority scheduling

a, b the program, whose use is shorter, the first priority scheduling using CPU.

Disadvantages: If a program using the longest time, there are n program end time, you must wait before you can use the short end of all time applications.

3. The round-robin method

CPU execution time of 1 second, the program loads the n, to n, one second is divided into time slices.

4. grading feedback queue

The implementation of priority points multi-layer level.

Level 1: the highest priority

Level 2: a second priority, and so on

Level 3: ... ...

... ... ... ...

Down the queue, the lower the priority.

4. Parallel and Concurrent

Concurrent: refers looks like a run, a plurality of switching programs + kept save state, it aims to improve the efficiency.

Parallel: run on a real sense, in the case of multi-core (multiple CPU), the simultaneous execution of multiple programs

Note: It is now generally multicore host, it will use it each core multi-channel technology, there are four CPU, a program run encounters with CPU1 blocking IO, IO will wait until the end of the re-scheduling, it is scheduled to four any one of the CPU, as determined by the operating system scheduling algorithm.

5. A three state scheduler

1. 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. Run mode: When the process has been the processor, the processor of the program is on executing each process is loaded too called the execution state

3. blocking state: the process of being executed, due to the occurrence of waiting for some time and can not perform, they give up the processor and is blocked, causing the process to block a variety of events, such as waiting for I / O completion, the application buffer region is not met, wait letter (signal) and the like.

Program during the scheduled time of the process: Before the program starts running, not immediately start executing code, but will enter the ready state, waiting for the operating system scheduler is running, such as when the operating system is running, the program went into operation, and other procedures encountered when waiting for user input operation (I / O), the program goes to blocking state, when the user input is complete, the program is not run immediately, but enter the ready state, waiting for the operating system scheduler continues to run,

6. synchronous and asynchronous

Way when referring to submit the task

Synchronize:

If two tasks need to mention to submit, at the time of submission of the first task, you must wait after the execution of the task, you can perform the second task. Simply means that one by one, after the completion of the previous one, the latter will be carried out. Similar to a bank queue.

asynchronous:

Means that if two tasks, when submitting the first task without waiting for the end of the task, you can immediately presented to the second task. People like to buy.

7. blocking and non-blocking

Blocking: blocking state, IO will encounter obstruction. While waiting, waiting if in addition to other things can not do, is to block, performance improvement program is in the function blocks the calling out of the function can not continue down

Non-blocking: ready state and run state. But wait can continue to do other things, it is a non-blocking state;

Note: synchronous non-blocking line when in fact inefficient, imagine that you side of the phone, while the team also need to look up there to you, if the two phone calls and queuing worse position as program operation, this sequence need to switch back and forth between the two different behaviors, imagine efficiency is low, but non-blocking asynchronous line is no such problem, because calling (waiters) you thing, and you notice is the counter (news trigger mechanism) things, the program does not switch back and forth in two different operations.

Between about 8. The composition synchronous / asynchronous with the blocking / nonblocking

1. synchronous blocking form

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

2. Asynchronous blocking form

If the bank waiting for a man of business uses asynchronous way to wait for the message is triggered (notification), which is picking up a piece of paper, if he can not leave the bank at this time to do something else, then it is obviously, 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 programs of operations, then this program needs in these two different acts switching back and forth between, imagine efficiency is low.

4. A non-blocking asynchronous form

higher efficiency,

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, so he told the lobby manager, said this number when I discharged the trouble to inform me out, then he would not be blocked waiting for this operation above, this is the natural way of non-blocking asynchronous +.

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

9. face questions:

Synchronous and asynchronous, blocking and non-blocking is the same concept it?

Answer: It is not the same concept, can not be confused

10. maximize improve CPU utilization

Minimize unnecessary IO operations

Use process

For general-purpose systems, the need to have the ability to create or revocation process of the system is running. Divided into four kinds of line to create a new process

1. System Initialization

2. A process opens a child process during operation (such as nginx open multi-process)

3. The user interactive requests, and create a new process (such as double-click the QQ)

4. initialize a batch job (only applications in a mainframe batch system)

Create a new process is executing the system call for creating a process from an existing process created,

1. Create a process

Running program is a process, all processes are by his parent process to create, run up the python program is a process, we can re-create the process in a program, you can achieve multiple processes concurrently effect, that is, He said that when there are multiple processes of our program, and then certain time, it will allow faster execution speed of the program.

2. The process module used --- multiprocess module

1. Create a module process --- Process module

Process is the process of creating a module, with this module, you can complete the process of creating, it indicates that the task in a child process by the object class to instantiate obtained (not yet started)

. Ⅰ Process parameters in the introduction:

① group parameter is not used, the value is always None

② target represents the calling object, task child process that is to be executed

③ args specified as parameters to the targe function call object indicates a position parameter tuple is a tuple form, there must be a comma,

④ kwargs indicates that the call object dictionary

⑤ name is the name of the child process

Ⅱ. Use

p.start (): to start the process, and calls the process p.run ().

p.run (): method of operation, when the process starts, is it to call the target function specified in our custom class must implement this method.

p.terminate (): p forcibly terminate the process, will not carry out any cleaning operation, if p create a child process, the child process to become a zombie process, this method requires special care this case, if p also holds a lock, then it will not be released, leading to a deadlock.

p.is_alive (): If p is still running and returns True.

p.join ([timeout]): p wait for the main thread to terminate, (stressed: the state is the main thread in the other, and p is running out of state), timeout is optional timeout should be emphasized that p.join You can only join live start open process, and can not join the live run open process

Introduction property Ⅲ

p.name: name of the process

p.pid: get the child inherits process ID

p.daemon: The default value is False, if set to True, the representative of p is a daemon running in the background, when the termination of the parent process p, p also will be terminated, and after setting True, you can not create your own new p process must be p.start () before setting

Process Use

Create a process

# 创建一个进程
from multiprocessing import Process
import time

def f(name):
    print('hello',name)
    time.sleep(1)
    print('我是子进程')

if __name__ == '__main__':
    p = Process(target=f,args=('boy',))
    p.start()
    p.join()
    print('执行主进程内容了')
    
# 输出内容:
执行主进程内容了
hello boy
我是子进程

join to use

# 创建一个进程
from multiprocessing import Process
import time
import os

def f(name):
    print('hello',name)
    time.sleep(1)
    print('我是子进程')

if __name__ == '__main__':
    p = Process(target=f,args=('boy',))
    p.start()
    p.join()
    print('执行主进程内容了')
    
# 输出结果
hello boy
我是子进程
执行主进程内容了

Inter-process data are isolated from each other, the main process and sub-process will have their own namespace

No. recovery process in two ways

1.join, the child can be recycled to the main process

2. The main process ends normally, the child process will be recycled to the main process

from multiprocessing import Process
from multiprocessing import current_process
import os  # 与操作系统交互
import time


def task(name):
    print(f'{name} start...', current_process().pid)
    time.sleep(1)
    print(f'{name} over..', current_process().pid)


if __name__ == '__main__':
    p = Process(target=task, args=('jason', ))
    p.start()  # 告诉操作系统,开启子进程

    # 判断子进程是否存活
    print(p.is_alive())

    # 直接告诉操作系统,终止 子进程
    p.terminate()
    time.sleep(0.1)

    # 判断子进程是否存活
    print(p.is_alive())

    p.join()  # 告诉操作系统,等子进程结束后,父进程再结束。

    print('主进程', os.getpid())
    print('主主进程', os.getppid())
    time.sleep(100)
Daemon
from multiprocessing import Process
from multiprocessing import current_process
import time


def task(name):

    print(f'{name} start...', current_process().pid)

    time.sleep(5)

    print(f'{name} over..', current_process().pid)

    print(f'管家{name}')


if __name__ == '__main__':
    p1 = Process(target=task, args=('jason', ))

    # 添加守护进程参数
    p1.daemon = True  # True代表该进程是守护进程
    p1.start()

    print(f'egon 驾鹤西去...')

End of the process

1. Normal exit (click to close the program)

2. Error exit (voluntary, the file does not exist)

3. serious error (involuntary, illegal instruction execution)

4. killed by other programs (involuntary, such as kill-9)

Guess you like

Origin www.cnblogs.com/whkzm/p/11716143.html