Tsinghua Operating System Course Notes--Chapter 7: Process Management

Six points:

  • Process description (static representation)
  • Process state (state) (dynamic representation)
  • thread
  • inter-process communication
  • Process Mutex and Synchronization
  • deadlock

1. Process description (static part)

  • 1.1 Definition of process
    • Definition: A dynamic execution process of a program with certain independent functions on a data set (this entire dynamic process is called a process)
  • 1.2 The composition of the process
    • A process should include
      • program code
      • data processed by the program
      • The value in the program counter (register), which indicates the location of the next instruction that will be run
      • current value of a common set of registers, heap, stack
      • A set of system resources (such as open files, network resources)
        • In summary, a process contains all state information about a running program
    • The relationship between the process and the program (the process is a dynamically executed process, and the program is a static code)
      • Programs are the basis for generating processes
      • Each run of the program constitutes a different process
      • Process is the embodiment of program function
      • Through multiple executions, a program can correspond to multiple processes; through calling relationships, a process can contain multiple programs
    • The difference between a process and a program
      • The process is dynamic and the program is static: the program is a collection of ordered codes, the process is the execution of the program, and the process is divided into core state and user state
      • The process is temporary, and the program is permanent: the process is a dynamic process, and the program can be saved for a long time
      • The composition of the process and the program is different: the composition of the process includes the program, data and process control block (that is, the process status information)
    • Examples of Processes and Programs

  • 1.3 Characteristics of the process
    • Dynamic: Dynamically create and end processes
    • Concurrency: Processes can be scheduled independently and occupy processors to run
    • Independence: The work of different processes does not affect each other (different processes have different page tables, different memory space addresses)
    • Constraints: Constraints due to access to shared data/resources, or synchronization between processes
  • 1.4 Process Control Structure
    • process control block
      • program = algorithm + data structure
        • Describe the data structure of the process: Program Control Block (Process Control Block, PCB)
        • The operating system maintains a PCB for each process, which is used to save various state information related to the process
      • Process control block: the collection of information used by the operating system to manage and control the operation of the process
      • The operating system uses PCB to describe the basic situation of the process and the process of running changes. PCB is the only sign of the existence of the process.
    • use process control block
      • Process creation: create a PCB for the process
      • Termination of a process: recycling its PCB
      • Organizational management of the process: the organization and management of the process is realized through the organizational management of the PCB
    • The PCB contains the following three categories of information:
      • (1) Process identification information.
        • Such as the identification of the process (ID number), the parent process identification of the process (the process that created the process); user identification (which user the process belongs to)
      • (2) Processor (processor, that is, CPU) state information storage area. Save the running site information of the process
        • User-visible registers. Registers such as data and addresses that can be used by the user program
        • control and status registers. Such as status counter (PC), program status word (PSW)
        • stack pointer. It is needed for program call/system call/interrupt handling and return
      • (3) Process control information
        • Scheduling and Status Information
        • interprocess communication information
        • storage management information
        • resources used by the process
        • Information about data structure connections
    • How the PCB is organized
      • Linked list: The PCB of a process in the same state forms a linked list, and multiple states correspond to multiple different linked lists (ready linked list, blocked linked list)
      • Index table (similar to an array): PCBs of processes in the same state form an Iindex table (from Index to PCB), and multiple states correspond to multiple different index tables (ready index table, blocking index table)
      • The most commonly used is the linked list, because the process is dynamic, and the linked list is easy to insert and delete.

2. Process status (dynamic part)

  • three parts
    • Process Lifecycle Management
    • Process State Transition Model
    • Process hang model
  • 1.5 Process Lifecycle Management
    • Process Lifecycle Management
      • process creation
      • process running
      • The process waits (blocks). The process can only block itself, because only the process itself can know when to wait for some event to occur
      • The process wakes up. Processes can only be woken up by other processes or by the operating system
      • end of process
  • 1.6 Process state transition model
    • The 3 basic states of the process (the process is in and only in one of the 3 basic states before the end of its life, and the number of process states set by different systems is different)
      • Running: when the process is running on the processor
      • Ready state (Ready): A process has obtained all required resources except the processor, and can run once it gets the processor
      • Waiting state (also known as blocking state Blocked): When a process is waiting for an event and suspends its operation, such as waiting for a resource, waiting for input/output to complete
    • Other basic state of the process
      • Creation status (New)
      • End state (Exit)

  

  • process hang
    • Suspend: Transfer a process from memory to external storage (i.e. disk/hard disk). When the process is in the suspended state, it means that the process does not occupy memory space. Process image in pending state on disk
    • why? Reasonable and full use of system resources
    • two pending states
      • Blocked-suspend state (Blocked-suspend): The process is in external storage and is waiting for an event to occur
      • Ready-suspend state (Ready-suspend): The process is in external memory, but can run as long as it enters memory
    • state transitions related to suspend
      • block to block hang
      • ready to ready suspend
      • run to ready suspend
    • State transitions in external storage
      • blocking suspend to ready suspend
    • Unsuspend/activate (activat): Transfer a process from external storage to internal memory. There may be the following situations
      • ready suspend to ready
      • block hang to block
    • status queue
      • The operating system maintains a set of queues, which respectively represent the current status of all processes in the system
      • Different states are represented by different queues (such as ready queues, various types of blocking queues)
      • The PCB of each process is added to the corresponding queue according to its state. When the state of a process changes, its PCB is separated from one state queue and added to another state queue.

3. Thread management

  • Four content
    • Why use threads
    • what is thread
    • Implementation of threads
    • Example of multi-threaded programming interface
  • 1 Why use threads
    • Problems with multiple processes
      • Data cannot be shared between processes, and inter-process communication needs to go through the CPU
      • The system overhead of maintaining a process is large: when creating a process, allocate resources and build a PCB; when canceling a process, reclaim resources and cancel PCB; when switching between processes, it is necessary to save the status information of the current process.
  • 2 What is a thread
    • Thread: A flow of execution in a process
    • With the concept of threads, the process can be re-understood from two aspects
      • From the perspective of resource combination: a process combines a group of related resources to form a resource platform (environment), including address space (code segment, data segment), open files, accessed networks and other resources (the process is used to manage resources)
      • From the perspective of operation: an execution process (thread) of the code on this resource platform
      • That is, a process consists of two parts: resource management + threads
    • thread = process - shared resource
    • Advantages of threads
      • Multiple threads can exist in a process at the same time
      • Each thread can execute concurrently
      • Resources such as address space and files can be shared between threads (shared code, data and files)
    • Disadvantages of threads
      • A thread crashes, causing all threads in the process it belongs to to crash
    • Comparing threads and processes
      • A process is a resource allocation unit, and a thread is a CPU scheduling unit
      • Processes have a complete resource platform, while threads only share essential resources (registers and stacks)
      • Threads also have three basic states of ready, blocked and executing, and also have transition relationships between states
      • Threads can reduce the time and space overhead of concurrent execution:
        • Threads have shorter creation and termination times than processes
        • The thread switching time in the same process is shorter than that of the process (because there is no need to switch the page tables required for memory management)
        • Since the threads of the same process share memory and file resources, they can directly communicate without going through the kernel (the data transfer between threads does not need to go through the kernel, and can be accessed directly through the memory address )

  • 3. Implementation of threads
    • There are three main implementations of threads:
      • User thread (user thread): implemented in user space, invisible to the operating system, and managed by a user-mode application library (thread library function) (the operating system can see the process, but not the threads in the process)
      • Kernel thread: A thread implemented in the kernel and managed by the operating system (Windows, linux, solaris)
      • Lightweight thread (Lightweight): implemented in the kernel, supports user thread (solaris)
    • Correspondence between user threads and kernel threads
      • many to one
      • one to one
      • many to many
    • user thread
      •  

  • kernel thread

4 process control

  • context switch 
    • Definition: CPU switching between different processes is called context switching (context switch)

  •  

 

 

 

Guess you like

Origin blog.csdn.net/yzy__zju/article/details/109710719