Operating system history / multi-channel / process

Required knowledge

  • The computer is also called the computer, that is, the brain with electricity. The computer was invented to enable him to work like a human after being powered on, and it is more efficient than human because it can be uninterrupted for 24 hours.

  • The five major components of a computer

    Controller

    Operator

    Memory

    input device

    Output device

    The core of the computer really works is the CPU (controller + arithmetic unit = central processing unit)

  • For a program to be run by a computer, its code must be read from the hard disk to memory, and then the CPU fetches instructions before executing

Operating system history

Just refer to the blog: https://www.cnblogs.com/Dominic-Ji/articles/10929381.html

  • Punch card

Paper tape punching

  • Online batch processing system

Online batch processing system

  • Offline batch processing system

Offline batch processing system

Multi-channel technology

Single core achieves concurrent effect

Required knowledge

  • Concurrency

    It looks like running concurrently can be called concurrent

  • parallel

    Real simultaneous execution

ps:

  • Parallel must be considered concurrent
  • Single-core computers certainly cannot achieve parallelism, but they can achieve concurrency! ! !

Supplement: We directly assume that a single core is a core, and only one person works, don't consider the number of cores in the CPU

Multi-channel technical illustration

Save the total time spent running multiple programs

Multi-channel technology

Multi-channel technology key knowledge

Taking in space and taking in time

  • Spatial multiplexing

    Multiple programs share a set of computer hardware

  • Multiplexing in time

    Example: Washing clothes for 30s, cooking for 50s, boiling water for 30s

    Single channel needs 110s, multiple channels only need the long switch of task to save time

    Example: Playing a game while eating and saving state

 

Switch + save state

 

"" " 
Switching (CPU) is divided into two cases 
	1. When a program encounters IO operation, the operating system will deprive the program of the CPU execution authority 
		role: improve the CPU utilization and does not affect the program execution efficiency 
	
	2. When a program occupies the CPU for a long time, the operation attraction will also deprive the program of the CPU execution authority. 
		Disadvantages: reduced the execution efficiency of the program (original time + switching time) 
"" "

  

Process theory

The difference between program and process

A program is a bunch of code lying on the hard disk. A "dead" 
process indicates that the program is executing, and it is "live".

  

Process scheduling

  • First come first served scheduling algorithm

    "" "Is good for long work, not good for short work" ""
    

      

  • Short job priority scheduling algorithm
    "" "Is good for short work, but not for long work" ""
    

      

  • Time slice rotation method: when you want to open a new program, it will stop the currently executing first to open your new program to him

Three state diagram of process running

All programs must go through the ready state before they can be executed

img

Event request includes input, print, open

Blocking state to ready state: input gets the value, file reading is completed, and timesleep ends

img

Two pairs of important concepts

Synchronous and asynchronous

Describes how tasks are submitted

  • Synchronization: After the task is submitted, it waits for the return result of the task in situ. Nothing is done during the waiting process. The feeling at the program level is that it is stuck.

  • Asynchronous: after the task is submitted, it does not wait for the return result of the task in place, and directly does other things, waiting for the return result of the task

    The return result of the task will have an asynchronous callback mechanism

Blocking and non-blocking

Describes the running state of the program

  • Blocking: blocking state
  • Non-blocking: ready state, running state

The most efficient combination is asynchronous + non-blocking

Ideal state: we should keep the code we write in a ready state and a running state without blocking

Two ways to start the process

The way the code starts the process and the thread, the code writing is basically the same, learn how to start the process and learn how to start the thread

Two ways

  

from multiprocessing import Process 
import time 


def task (name): 
    print ('% s is running'% name) 
    time.sleep (3) 
    print ('% s is over'% name) 


if __name__ == '__main__': 
    # 1 Create an object 
    p = Process (target = task, args = ('jason',)) 
    # Container type Even if there is only 1 element inside, it is recommended to use commas to separate 
    # 2 Start the process 
    p.start () # Tell the operating system to help you Create a process asynchronous 
    print ('main') 
    
    
# The second way of class inheritance 
from multiprocessing import Process 
import time 


class MyProcess (Process): 
    def run (self): 
        print ('hello bf girl') 
        time.sleep (1) 
        print ('get out!') 


if __name__ == '__main__':
    p = MyProcess ()
    p.start () 
    print ('main')

  

Under the windows operating system, the creation process must be created in the main , because the creation process under windows is similar to the way of module import, it will be executed from top to bottom

 

The first way is used more

to sum up

To create a process is to apply for a piece of memory space in memory and throw the code that needs to be run. A process corresponds to an independent memory space in memory, and multiple processes correspond to multiple independent memory spaces.

The data between processes cannot be directly interacted with by default. If you want to interact, you can use other tools and modules.

 

 

join method

After the main process waits for the end of the child process, it will continue to execute

The result is to turn asynchronous into synchronous

 

from multiprocessing import Process 
import time 


def task (name, n): 
    print ('% s is running'% name) 
    time.sleep (n) 
    print ('% s is over'% name) 


if __name__ == '__main__': 
    # p1 = Process (target = task, args = ('jason', 1)) 
    # p2 = Process (target = task, args = ('egon', 2)) 
    # p3 = Process (target = task, args = ( 'tank', 3)) 
    # start_time = time.time () 
    # p1.start () 
    # p2.start () 
    # p3.start () # Just tell the operating system to create a process 
    # # time.sleep (50000000000000000000) 
    # # p.join () # The main process waits for the child process p to finish running before continuing. 
    # p1.join () 
    # p2.join () 
    # p3.join () 
    start_time = time.time() 
    p_list = []
    for i in range(1, 4):
        p = Process(target=task, args=('子进程%s'%i, i))
        p.start()
        p_list.append(p)
    for p in p_list:
        p.join()
    print('主', time.time() - start_time)

  

Data isolation between processes

from multiprocessing import Process


money = 100


def task():
    global money  # 局部修改全局
    money = 666
    print('子',money)


if __name__ == '__main__':
    p = Process(target=task)
    p.start()
    p.join()
    print(money)

  

 

Guess you like

Origin www.cnblogs.com/Tornadoes-Destroy-Parking-Lots/p/12755725.html