[Operating System] 2.3 Process Management (Process Scheduling)

Basic concepts of scheduling

When there are a bunch of tasks to be processed, but due to limited resources, these things cannot be processed at the same time. This requires certain rules to determine the order of processing these tasks. This is the problem of "scheduling" research.

  • In a multi-program system, the number of processes is often more than the number of processors, so it is impossible to process each process in parallel at the same time.
  • Processor scheduling is fromReady queueIn accordance with certainalgorithmChoose a process and assign a processor to itRun to achieve the processConcurrentcarried out

Three levels of scheduling

  1. Advanced scheduling (Job scheduling

    1. Due to the limited memory space , sometimes it is impossible to put all the jobs submitted by the user into the memory , so it is necessary to determine a certain rule to determine the order in which the jobs are loaded into the memory
    2. Advanced scheduling: according to certain principlesBackup queuePick one (or more) of the assignmentsoperation,give themAllocate memoryAnd other necessary resources,And establish the corresponding process (build PCB), So that they can get the right to compete for the processor
    3. Advanced scheduling isAuxiliary storage (external storage) and internal memoryBetween scheduling. Each job is only called in once and called out once. The PCB will be created when the job is called in, and will be cancelled when the job is called out. Advanced scheduling mainly refers to the problem of calling in, because only the timing of calling in needs to be determined by the operating system, but the timing of calling out must be when the job is finished.
      Insert picture description here
  2. Intermediate scheduling (Memory scheduling

    1. After the introduction of virtual storage counting,Temporarily unable to run processTransfer to external storage and wait. When it has the operating conditions again and the memory is slightly free, then reload the memory
    2. The purpose of this is to improve memory utilization and system throughput
    3. The status of the process temporarily transferred to the external memory waiting is suspended. It is worth noting that⭐PCB will not be transferred to external memory together, but will be resident in memory. The PCB will record information such as the position where the process data is suspended and the process status in the external memory. The operating system maintains the monitoring and management of each process through the PCB in the memory. The suspended base PCB will be placed in the suspension queue
      Insert picture description here

    The suspended state of the process is in the seven-state model:

    1. Temporarily transfer to external storage and waitThe process status is suspended.
      Note: The difference between "suspended" and "blocked" : both states are temporarily unable to receive CPU services, butSuspended stateYesTransfer process image to external memory,andBlocking stateunderStill in the process image memory in
    2. It can be divided into:Ready to hangBlocking hang
    3. Seven-state model:
      Insert picture description here
  3. Low-level scheduling (Process scheduling

    1. The main task is to follow a certain algorithm and strategy fromReady queueChoose oneprocess, Assign the processor to it
    2. Process scheduling is the most basic type of scheduling in the operating system.Must be configuredProcess scheduling
    3. Process schedulingHigh frequency, Usually every tens of milliseconds
      Insert picture description here

⭐Three-tier scheduling connection and comparison

project What to do The scheduling happens on... occurrence frequency Impact on process status
Advanced scheduling (job scheduling) According to a certain rule, select the appropriate job from the backup queue to transfer it into the memory, and create a process for it External memory→memory (operation-oriented) lowest None→Create state→Ready state
Intermediate scheduling (memory scheduling) According to a certain rule, select the appropriate process from the suspended queue to transfer its data back to the memory External memory→memory (process-oriented) medium Suspend (ready suspend) state → ready state (blocking suspend → blocking state)
Low-level scheduling (process scheduling) According to a certain rule, select a process from the ready queue and assign a processor to it Memory → CPU highest Ready state → Running state

Timing of process scheduling

When do you need process scheduling?

  • needThe situation of process scheduling and switching:
    1. Currently running processGive upProcessor:
      1. Process terminated normally
      2. Abnormal termination occurred during operation
      3. The process actively requests to block (such as waiting for I/O)
    2. Currently running processPassively give upProcessor:
      1. The time slice allocated to the process is used up
      2. There are more urgent things to deal with (such as I/O interruption)
      3. Processes with higher priority enter the ready queue
  • Can'tThe situation of process scheduling and switching:
    1. inHandle interruptionin the process of. The interrupt handling process is complicated and closely related to the hardware. It is difficult to switch between processes during the interrupt handling process.

    2. Process inOperating system kernel program critical sectionin

      The process cannot be scheduled and switched in the critical section of the operating system kernel program
      ×The process cannot perform processor scheduling PS in the critical section
      :
      Critical resource: A resource that is only allowed to be used by one process in a time period. Each process needs to access critical resources mutually exclusive
      Critical section: The piece of code that accesses critical resources
      Kernel critical section: A move is used to access a certain kernel data structure, such as the ready queue of the process (composed of the PCB of the ready process)

      1. When a process accesses a critical resource, it will be locked. If it has not exited the critical area, the resource cannot be unlocked. If it has not been unlocked, the process will be scheduled .Programs related to process scheduling also need to access the ready queue, butThe current ready queue is lockedIf it fails, process scheduling cannot be carried out smoothly.
        If the critical resources accessed by the critical section of the kernel program are not released as soon as possible, it is very likelyOther management tasks affecting the operating system kernel, Therefore, the process is in the critical section of the operating system kernel programCan'tScheduling and switching
        Insert picture description here
      2. The process has been in the critical zone until the printer is completed, and the critical resource will not be unlocked, but the printer is a slow device. If process scheduling is not allowed at this time, the CPU will always be in an idle state while waiting for the printer to complete.
        Ordinary critical sectionAccess critical resourcesWill not directly affect the management of the operating system kernelTherefore, the process is in critical section whencanProcessor scheduling
        Insert picture description here
    3. inAtomic manipulationIn process ( primitive ). Atomic operations cannot be interrupted and must be done in one go (for example: modify the status flag of the process in the PCB and put the PCB in the corresponding queue)

Process scheduling method

In some operating systems, the process can take the initiative to abandon the processor, and when there are more urgent tasks to be processed, it will also force the processor to be deprived (passively abandon)

  • Non-deprivation scheduling mode (non-preemptive):
    1. Only the process is allowed to actively give up the processor. Even if a more urgent task arrives during the running process, the current process will continue to use the processor until the process terminates or actively asks to enter the blocking state
    2. Advantages: simple implementation, low system overhead
    3. Disadvantages: unable to handle urgent tasks in time
    4. Suitable for early batch processing systems
  • Deprivation scheduling method (preemptive)
    1. When a process is executed on the processor, if there is a more important or more urgent process that needs the processor, immediately suspend the currently executing process and assign the processor to the more important process
    2. Advantages: It can give priority to more urgent processes, and it can also realize the function of making each process execute in turn according to the time slice (via clock interruption)
    3. Suitable for time-sharing operating system and real-time operating system

Process switching

  • Generalized process scheduling includesChoose a process(Narrow process scheduling) andProcess switchingTwo steps
  • The process switching process is mainly completed:
    1. Save various data of the original running process
    2. Recovery of various data in the new process
  • noteProcess switching has a priceSo ifToo frequentThe scheduling and switching of processes will inevitably lead to a decrease in the efficiency of the entire system. When the system spends more time on process switching, the time actually spent on process execution is reduced.

Guess you like

Origin blog.csdn.net/Qmilumilu/article/details/112608444