A rough understanding of the process

First, procedures and processes

  • Process: a process in progress or that a task is responsible for performing tasks cpu..

  • + Multi-channel single-core, implement multiple concurrent processes
  • Program: a bunch of code, a pile of documents
  • The same procedure is performed twice, that is two processes, such as opening storm video, though they are the same software, but can play a city in the sky, a play Dragon Ball

Second, the parallel and concurrent

  • Either parallel or concurrent, users seem 'while' running, whether it is a process or thread, are just a task it really work is cpu, cpu to do these tasks, while the same time, only a cpu perform a task

  • Serial: all processes of a solution to a cpu

  • Concurrency: single cpu, execute multiple processes simultaneously (multi-channel technology), it looks like a run

  • Parallel: multiple cpu, running multiple processes at the same time real

Third, synchronous \ asynchronous and blocking \ non-blocking

  • Sync: The so-called synchronous, that is emitted when a function call, until no results, the call will not return According to this definition, in fact, the vast majority of functions are called synchronously but in general we are talking synchronization. asynchronous time, especially those that require collaboration or other parts will take some time to complete tasks
  • Asynchronous: Asynchronous concept and synchronization relative Once an asynchronous function call issued, the caller can not get the results immediately after the asynchronous function is completed by the status notification or callback to notify the caller if the asynchronous function of a state to notify,... the caller will need some time to check every one, efficiency is very low. If you are using the notification of the way, the efficiency is very high, due to the asynchronous features almost no need for additional operations. as a callback function, and in fact did not notice too much the difference.
  • Blocking: blocking calls means before the call returns, the current thread is suspended (in case of I / O operations, recv, accept, read input, write, sleep, join, etc.), function only with the result only after the blocked thread is active. Maybe someone will call and synchronous call blocking equate, in fact, he is different. for synchronous calls, many times the current thread is still active, but does not return from the current function logically it
  • Note: When no blocked process is forced to suspend the computer when no call blocking
  • Non-blocking: blocking and non-blocking concept corresponds finger will return immediately before the result can not be obtained immediately, while the function does not block the current thread.
  • to sum up:
    • Synchronous and asynchronous for that function is called / task: synchronize is when a process initiated by a function (task) when called, wait until the function (task) is complete, and the process continues to be active while asynchronous case is when a. initiate a process function (task) call, it does not return other functions, but continue down when the function returned by the status notification, event notification, etc., the process of the task is completed.
    • Blocking and non-blocking for that process or thread: blocking time when the request is not met by the process will be suspended, while the non-blocking will not block the current process.

Create four process

  • For the general system (running many applications), you need to have the ability to create or revocation process during system operation, mainly divided into four forms create a new process

    • system initialization
    • A process opens a child process during operation
    • Interactive user requests, and create a new process (such as the user double-STORM)
    • A batch initialization operations (application only on mainframe batch system)
  • Create a new process is executing the system call for creating a process from an existing process created:

    • python, if you want to open a number of processes, it must be a primary process, opening a number of sub-processes

      # 开启进程的2种方式
      # 第一种方式
      from multiprocessing import Process
      import time
      def task(name):
          print(f'{name} is running')
          time.sleep(9)
          print(f'{name} is done')
      
      if __name__ == '__main__':  # windows必须在main下开启多进程
          p = Process(target=task, args=('子进程',))    # args一定是一个元组
          p.start()   # 通知操作系统在内存种开辟一个空间,将p这个进程放进去,让cpu执行
          print('===>主进程')
      ----------------------------------------------------
      # 第二种方式
      from multiprocessing import Process
      import time
      class MyProcess(Process):
          def __init__(self, name):
              super().__init__()  # 必须继承父类的init
              self.name = name
      
          def run(self):  # 必须是run
              print(f'{self.name} is running')
              time.sleep(9)
              print(f'{self.name} is done')
      
      if __name__ == '__main__':
          p = MyProcess('子进程')
          p.start()
          print('===>主进程')
  • About child process created, UNIX and windows

    • It is the same: the process is created, the parent and child processes have different address spaces (technical requirements for multi-channel physical level to achieve isolation between process memory), a process any modification in its address space will not affect another a process.
    • The difference is: in UNIX, the initial address space of the child process is a copy of the parent process, the child process and the parent process can have read-only shared memory area but for the windows system, right from the start with the parent process. the address space of the child process is different.

Terminate the five process

  1. Normal exit (voluntary, if the user clicks crosses interactive page, or program execution is completed to initiate normal exit system call, with the exit linux, in windows using ExitProcess)
  2. Error exit (voluntary)
  3. Serious error (involuntary, to execute an illegal instruction, such as references to nonexistent memory, 1/0, etc., you can catch the exception, try ... except ...)
  4. Killed by another process (involuntary)

Structure VI process

  • Whether linux or windows, only a parent process, except that:
  1. Linux in all the processes, are based on the init process is the root, the composition tree structure. A parent and child process together form a group, so that, when a signal is sent from the keyboard, the signal is sent to the current process group associated with the keyboard All members of.
  2. In the windows, there is no concept of process hierarchy, all the processes are the same status as the only similar process hierarchy implies, is when you create, the parent process to get a special token (called a handle), the handle can be It used to control the child, but the parent has the right to pass the handle to the other sub-process, so that no level of.

State VII process

  • A program execution, turn on a sub-process, the implementation of the program B, a further opening between the sub-pipes based processes, the two processes '|' communication, the result of A as input B of the process B is waiting for input (i.e., I / O. state when) called blocking, this time B command can not run

  • In fact, in both cases it will lead to a process that does not run on logic

    • The process is suspended for their own reasons, encounter I / O blocked, they want to let the CPU to perform other processes, so ensure that the CPU has been working
    • It has nothing to do with the process, the operating system level, a process that may take up too much because of time or priority reasons, and call other processes to use the CPU
  • A process has three states:

Eight, the complicated process of realization

Implement concurrent processes that hardware interrupt a running process, the preservation of the status of all processes running at this time, to this end, the operating system maintains a table, that table process (process table), each process occupies a process table important information items (also referred to these entries process control block), the table storing a process state: program counter, stack pointer, memory allocation status, the status of all open files, scheduling and account information, as well as in other processes run by state into ready state or blocking state when information must be preserved to ensure that the process start again, just as had never been interrupted.

Guess you like

Origin www.cnblogs.com/zyyhxbs/p/11228095.html