History of computer development and process

1. history of operating system development

1). Punchcards

A room can only use one card
CPU utilization is very low

2). Online batch processing system

It supports multiple users to use a computer room.

3). Offline batch processing system

High-speed tape improves reading speed, improve the utilization of the CPU.

4) Multi-channel technology

Single-channel:

(Single-core cases) when multiple users are using the CPU serial, one by one execution, only one program to the next in order to complete the implementation of a program.

Multi-channel

(Case-based single core): A program, loads the program in memory B, the program when finished using A CPU B will begin using CPU (also only one CPU).

(1) the spatial multiplexing

Simple may be provided to a plurality of users to use.

(2) Multiplexing (******) on time

+ Handover saved state (the current state is saved program switching)

IO operations: input (), print (), time.sleep ().

1) If the CPU has encountered an IO operation, immediately cut off the right to use the CPU program currently executing
advantages: high CPU utilization

2) If a program using the CPU time is too long (there are cup set of the most commonly used time), CPU use right immediately disconnect the current program, to the other programs.
Cons: reducing the rate of implementation of the program.

Concurrent and Parallel:

Concurrency: It looks like while running. (Programs corresponding to the plurality of switching back and forth)

Parallel: real running while simultaneously executing a plurality of programs in a multi-core case.

2. Process

Procedures and processes

Program: a bunch of code that
processes a bunch of code that runs: Process

Process Scheduling:

1. FCFS scheduling:
a, b program, if a program first-come, first occupied CPU

Cons: First use a program, the program must have been waiting b
2. short job priority scheduling:
a, b program, who is short of time, who use priority scheduling

Disadvantages: must wait for all the time with the short end of the program to use with long execution

3. The round-robin process
time executed by the CPU 1 second, loading n procedures, to 1 second, etc. time slots into n (n parts)
4. classification queue feedback
execution priority level multilayer into
--1 level: the highest priority
--2 level: the second, and so on
--3 level

Three state program is running;

Ready state: the state will enter this program creates all ready to dispatch.

Running state: after the process scheduler, enter the run state.

Blocking state: Those who encountered IO operation process, will enter the blocking state. If the IO end, they must re-enter the ready state.

Synchronous and asynchronous:

It refers to the submission of the task.
Sync: If two tasks need to be submitted, when the job at the time of filing the first task, the task must wait for the end of execution to the next task.
Asynchronous: If two tasks submitted, the second task does not need to wait two tasks can be performed simultaneously.

#同步演示程序:程序一直在运行,并未将CPU的运行权限交出去
import time


def test():
    # IO操作
    # time.sleep(3)

    # 计算操作
    num = 1
    for line in range(1000000000000000000000000):
        num += 1


if __name__ == '__main__':
    test()
    print('hello tank')

Blocking and non-blocking:

Blocking: blocking state, IO will certainly encounter blocking
non-blocking: ready state, running state

Interview questions: synchronous asynchronous, non-blocking is blocking the same concept it?
Waiting is not necessarily blocked, there may be a CPU-intensive task for too long, so they are not the same concept, can not be confused!

Maximize improve CPU utilization: minimize unnecessary IO operations

Create a process in two ways

Method 1: Define a task

In Windows error-prone system will call the parent process child process when creating the program, the parent process execution time and call the child process, so keep on going, resulting in a recursive (windows will import file as a copy of the code). This problem will only appear in the Windows system (Windows system tray when the process of creating child processes, will reload the current parent process code executed once), a re-copy the Mac and Linux systems which will be the current sub-process code parts, performed separately.

import time
from multiprocessing import Process


def task(name):
    print(f'{name}的任务开始执行')
    time.sleep(1)
    print(f'{name}的任务已经结束')
#target=执行函数的地址,agrs里面填元组,且元素是task的函数的参数
p = Process(target=task,args=('jason',))
#向操作系统提交创建进程的任务
p.start()
print('主进程')

The child process to write the main function which is running the program will not run before the parent process, but the main function is running (main function which is a sub-process) from, so it will not issue a recursive appeared.

import time
from multiprocessing import Process


def task(name):
    print(f'{name}的任务开始执行')
    time.sleep(1)
    print(f'{name}的任务已经结束')



if __name__ == '__main__':
    #target=执行函数的地址,agrs里面填元组,且元素是task的函数的参数
    p = Process(target=task,args=('jason',))
    #向操作系统提交创建进程的任务
    p.start()
    print('主进程')

Second way: a custom class, and inherit Process

Here the Lord and child processes are running in parallel, independently of each other between them, each running their own.


from multiprocessing import Process
import time


class MyProcess(Process):

    def run(self):
        print('任务开始执行')
        time.sleep(1)
        print('任务已经结束')

if __name__ == '__main__':
    p = MyProcess()
    p.start()
    print('主进程')

主进程
任务开始执行
任务已经结束

join method

Role: tells the operating system, so that the child process is finished, the parent process at the end.

from multiprocessing import Process
import time


class MyProcess(Process):

    def run(self):
        print('任务开始执行')
        time.sleep(1)
        print('任务已经结束')

if __name__ == '__main__':
    p = MyProcess()
    p.start()#告诉操作系统,开启子进程
    p.join()#告诉操作系统,等子进程结束后,父进程再结束
    print('主进程')

任务开始执行
任务已经结束
主进程

When opening a number of child processes to use more join

import time
from multiprocessing import Process


def task(name,n):
    print(f'{name}的任务开始执行')
    time.sleep(n)
    print(f'{name}的任务已经结束')



if __name__ == '__main__':
    #target=执行函数的地址,agrs里面填元组,且元素是task的函数的参数
    p1 = Process(target=task,args=('jason',1))
    p2 = Process(target=task, args=('xiaoming',2))
    p3 = Process(target=task, args=('xiaohua',3))
    #向操作系统提交创建进程的任务
    p1.start()
    p2.start()
    p3.start()

    p1.join()
    p2.join()
    p3.join()
    print('主进程')
jason的任务开始执行
xiaoming的任务开始执行
xiaohua的任务开始执行
jason的任务已经结束
xiaoming的任务已经结束
xiaohua的任务已经结束
主进程

Data between processes are isolated from each other, the child process and the main process will have their own name space, independently of each other at the time of execution.

x = 100
def func():
    print('执行func函数。。。')
    global x
    x = 200
    print(f'子程序x:{x}')

if __name__ == '__main__':
    p = Process(target=func)
    p.start()
    print(f'主程序x{x}')
    
主程序x100
执行func函数。。。
子程序x:200

About process ID

Process ID is in the implementation process, the operating system randomly assigned to a digital channeling This string of numbers is limited, if the operating system has run out of process ID, the system will no longer have a new process running.

courent_process () pid:. acquiring sub-process ID

os.getpid (): Gets the main process pid No.

os.getppid (): Gets the main process of the parent process (p for parent)

cmd view the process ID: tasklist | findstr process ID

Recovery process ID of two conditions:

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.

Check whether the child survival method

Child process .is_alive () returns the bool value of True or False

Method of terminating a child process

Child process .terminate () allow the operating system to force termination of a child process

from multiprocessing import Process
import time
from multiprocessing import current_process
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)#此处让子进程暂停0.1秒是因为子进程与主进程是并行执行的,而打印操作是在主进程中进行的,如果不暂停的话有可能,主进程执行到最后一步时,子进程还没执行到强制终止的指令,这样就无法得到正确的打印接过了

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

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

After adding the child process to terminate the program

from multiprocessing import Process
import time
from multiprocessing import current_process
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)#此处让子进程暂停0.1秒是因为子进程与主进程是并行执行的,而打印操作是在主进程中进行的,如果不暂停的话有可能,主进程执行到最后一步时,子进程还没执行到强制终止的指令,这样就无法得到正确的打印接过了

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

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

True
False

Zombie process and orphaned (understand):

Zombie process: child process has ended, but there are still pid number is not destroyed,

Disadvantages: occupation pid number footprint.

Orphan process: refers to the child process is still running but the parent process ends unexpectedly.

The operating system provides a orphanage, to help recover the child has no parent process, which is daemon.

Daemon

After refers to the main process, the main process of all child processes created and also followed the end of the recovery.

from multiprocessing import Process
import time
from multiprocessing import current_process
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.daemon = True# True代表该进程是守护进程
    p.start()  # 告诉操作系统,开启子进程

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

    # 直接告诉操作系统,终止 子进程
    p.terminate()
    time.sleep(0.1)#此处让子进程暂停0.1秒是因为子进程与主进程是并行执行的,而打印操作是在主进程中进行的,如果不暂停的话有可能,主进程执行到最后一步时,子进程还没执行到强制终止的指令,这样就无法得到正确的打印接过了

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

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

Guess you like

Origin www.cnblogs.com/ghylpb/p/11715195.html