当前位置:网站首页>Status of processes and communication between processes

Status of processes and communication between processes

2022-06-26 04:59:00 The story of Ula

State of process :

In the process of running the program , Because the scheduling algorithm of the operating system controls , The program goes into several States : be ready , Running and blocking . Process status shows the change of process execution , These states change with process execution and external conditions . The life cycle of a process can be divided into a set of States , These depict the whole process . Process state is the life state of a process .
 Insert picture description here

1. be ready (Ready):

The process has obtained the required resources other than the processor , Just waiting to allocate resources , As long as the processor is allocated, the process can execute .
The readiness process can be queued by multiple priorities , for example : When a process is ready due to running out of time , Queued to priority . When the process consists of I/O operation When finished and in the ready state , High priority queue .

2. function (Running):

The process occupies processor resources , The number of processes in this state is less than or equal to the number of processors , When no other process can execute ( For example, all processes are in a blocking state ), The system usually automatically executes idle processes of the system .

3. Blocking (Blocked):

The system waits for a condition due to a process ( Such as I/O Operation or process synchronization ), Cannot continue until conditions are met . Even if the processor resources are allocated to the process before this event occurs , The process cannot continue .

State transition :

be ready ------> perform :

A process in a ready state , When the process scheduler has allocated processors , The process changes from ready state to execution state . When the running process is blocked , The scheduler selects a process with the highest priority to occupy the processor .

perform ------> be ready :

A process in an execution state is in its execution , He had to give up the processor because one of the event slices assigned to him had run out , So the process changes from the execution state to the ready state .

perform ------> jam :

When an executing process cannot continue because it is waiting for an event to occur , It changes from the execution state to the blocking state .

jam ------> be ready :

A blocked process , If the event it is waiting for has happened , So the process changes from a blocked state to a ready state .
 .

Communication between processes ——Queue:

We know the process multiprocessing Modular Queue Implement data transfer between multiple processes ,Queue It's a message queuing program .
put() Method :

from multiprocessing import Queue(3)
# Create a queue , Specify the maximum capacity , If not specified, it is infinite .
q = put("A")
q = puy("B")
q = put("C")
# The queue is full , At this point the program will be blocked , Until it leaves the space 
q = put("D")

get() Method :

from multiprocessing import Queue(3)
# Create a queue , Specify the maximum capacity , If not specified, it is infinite .
q = put("A")
q = puy("B")
q = put("C")
# The queue is full , At this point the program will be blocked , Until it leaves the space 
q = put("D")
q = get() # "A"
q = get() # "B"
q = get() # "C"
# The queue is empty , Program blocking , Until new data is added 
q =get()

Use Queue:

Write two child processes in the parent process , A write operation , A read .

from multiprocessing import Process, Queue
import time
import random


#  Write data process 
def write(q):
    for value in ["A", "B", "C"]:
        print(" write in %s Into the Queue.." % value)
        q.put(value)
        time.sleep(random.random())

#  Data reading process 
def read(q):
    while True:
        if not q.empty():
            value = q.get()
            print(" obtain %s stay Queue" % value)
            time.sleep(random.random())
        else:
            break

if __name__ == '__main__':
    #  Parent process creation Queue, To each subprocess 
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    #  Start subprocess 
    pw.start()
    #  Wait for the subprocess that writes the data to end 
    pw.join()

    pr.start()
    pr.join()

    print(" All data is written and read ")

q.qsize(), Get queue length ( The amount of data in the queue )
get_nowait() Throw exceptions
empty() Determine whether the queue is empty True Means empty
full() Determine whether the queue is full

 Insert picture description here
You can't be too low , Because my father once held you above his head .

原网站

版权声明
本文为[The story of Ula]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180509132640.html