Operating system concept learning Note CPU scheduling

Source: Internet
Author: User
Tags terminates

Operating System Concept learning Note 10CPU scheduling


The basis of a multi-channel program operating system. By switching CPUs between processes, the operating system can increase the throughput rate of the computer.

For single-processor systems, only one process is allowed to run at a time: any other process must wait until the CPU is idle to be dispatched.

The goal of a multi-channel program is to have certain processes running at all times to maximize CPU usage. The idea of a multi-channel program is simpler, and when a process has to wait, the operating system takes the CPU from the process and hands the CPU to other processes.

CPU-I/O interval Period

The successful dispatch of the CPU relies on the following properties of the process:

Process execution consists of the CPU execution cycle and the I/O wait period . The process switches between the two states (CPU burst-i/o bust).

Process execution starts at the CPU interval (CPU burst), after which is the I/O interval (I/O burst). Then another CPU interval, then another I/O interval, goes on, and finally, the last CPU interval is aborted by the system request.

Tested for the length of a large number of CPU intervals. Discovery has a large number of short CPU intervals and a small number of long CPU intervals. I/O constrained programs typically have many short CPU intervals. CPU bound programs may have a small number of long CPU intervals. This distribution helps to select the appropriate CPU scheduling algorithm.

CPU Program Scheduling

Each time the CPU is idle, the operating system must select a process from the ready queue to execute. Process selection is performed by the short-term scheduler (short-term Scheduler) or the CPU scheduler . The scheduler selects a process that can execute from memory and allocates CPUs for it.

The ready queue does not have to be a first-in, in-out (FIFO) queue, or it can be a priority queue, tree, or simple unordered list. However, all the processes in the queue are queued to wait for the CPU to run. The records in the queue are typically process control blocks (PCBS).

Preemption Schedule:

CPU scheduling decisions can occur in the following 4 scenarios:

(1) When a process switches from running to a wait state (for example: I/O request, or call wait waits for a child process to terminate)

(2) When a process switches from a running state to a ready state (for example, an interrupt occurs)

(3) When a process switches from a waiting state to a ready state (for example: I/O completion)

(4) When a process terminates

For the 1th and 42 cases, there is no choice but to dispatch. A new process (if a process already exists in the ready queue) must be selected for execution. In the case of 2nd and 32, a choice can be made.

When the dispatch can only occur in the 1th and 42 cases, the dispatch is called non-preemption (nonpreemptive) or Cooperative (cooperative); otherwise, the scheduling scheme is preempted (preemptive) . With non-preemptive scheduling, once the CPU is assigned to a process, the process will always use the CPU until the process terminates or switches to a wait state.

Preemption scheduling the other side asks the shared data to be at a cost (such as locking), and requires a new mechanism to coordinate access to the shared data.

Preemption also has an impact on the design of the operating system kernel. When processing system calls, the kernel may be busy with process activity. These activities may involve changing important kernel data, such as I/O queues.

Because interrupts can occur at any time by definition and cannot always be overlooked by the kernel, the segments affected by interrupts must be protected to avoid simultaneous access. The operating system needs to be able to receive interrupts at all times, otherwise the input will be lost or the output will be overwritten. In order for these snippets to not be accessed concurrently by multiple processes, it is necessary to disable interrupts when entering, and to allow interrupts at exit.

Dispatch program

The Dispatcher (Dispatch) is a module that is used to give control of the CPU to processes selected by the short-term scheduler.

Its features include:

    • Toggle Context
    • Switch to User mode
    • Jump to the appropriate location in the user program to restart the program.

The dispatcher stops a process and starts another time it takes to become an allocation delay (dispatch latency).

Scheduling guidelines

To compare the guidelines presented by the CPU scheduling algorithm:

    • CPU usage: Need to make the CPU as busy as possible

    • Throughput: Refers to the number of processes completed within a time unit

    • Turnaround time: The time period from process submission to process completion is called turnaround time, and the turnaround time is the sum of all time periods, including waiting to enter memory, waiting in the ready queue, executing on the CPU, and I/O execution

    • Wait time: The sum of the time spent waiting in the ready queue

    • Response time: From submitting a request to the time when the first response was generated

You need to maximize CPU usage and throughput, while minimizing turnaround time, wait time, and response time. In most cases, you need to optimize the average, sometimes by optimizing the maximum or minimum, rather than the average value.

Scheduling algorithm first-to-first service scheduling

The simplest CPU scheduling algorithm is the first-to-first service algorithm (first-come,first-served scheduling): The process of requesting the CPU first allocates to the CPU. FCFS policies can be easily implemented with FIFO queues. When a process enters the ready queue, its PCB is linked to the tail of the queue. When the CPU is idle, the CPU is assigned to the process that is located in the queue header, and then the running process is removed from the queue.

The code writing of the FCFS strategy is simple and easy to understand, but the average wait time for the FCFS strategy is usually longer. When the CPU interval of a process varies greatly, the average wait time varies greatly.

such as the following example

Process interval Time
P1 24
P2 3
P3 3

If you arrive in P1 P2 P3 order, the Gantt Chart is as follows:

Average wait Time: (0+24+27)/3 = 17

If you arrive by P2 P3 P1 Order,

Average wait Time: (0+3+6)/3 = 3

In addition to considering performance in dynamic situations, assuming there is a CPU constrained process and many I/O constrained processes, the CPU constrained process moves back to the ready queue and is allocated to the CPU. Again, all I/O processes wait for the CPU process to complete in the ready queue. Since all other processes are waiting for a large process to release the CPU, this is called the Escort effect (convoy effect). This results in a low CPU and device usage when compared to the first execution of a shorter process.

The FCFS scheduling algorithm is non-preemptive. It is particularly troublesome for time-sharing systems (where each user needs to wait for a certain amount of CPU). Allowing a process to keep CPU time too long is a serious error.

Shortest job Priority scheduling (Shortest-job-first SCHEDULING,SJF)

Associates each process with the next CPU interval segment. When the CPU is idle, it is assigned to processes with the shortest CPU interval. If two processes have the same length, you can use FCFS scheduling to handle them. Note that an algorithm that is more appropriately represented as the shortest next CPU interval is due to the length of the next CPU interval of the scheduling check process, not its total length.

such as the following example

Process interval Time
P1 6
P2 8
P3 7
P4 3

SJF: (0+3 + 9 + 16)/4 = 7

FCFS: (0+6+14+21)/4 = 10.25

The average wait time for the SJF algorithm is minimal. The real difficulty with the SJF algorithm is how to know the length of the next CPU interval. For long-term (job) scheduling of batch systems, the process time limit set by the user to submit the job time can be used as the length. SJF scheduling is often used for long-term scheduling.

It cannot be implemented on a short-term CPU scheduling level. We can predict the next CPU interval. The length of the next CPU interval is considered similar to the previous. Therefore, by calculating the approximate value of the next CPU interval length, a process with the shortest predicted CPU interval can be selected to run. The next CPU interval is usually predicted as the exponential average (exponential average)of the measured length of the previous CPU cut-off.

The SJF algorithm may be preemptive or non-preemptive. The preemption SJF algorithm can preempt the currently running process, rather than the preempted SJF algorithm, which allows the current process to complete its CPU interval first. preemption SJF Scheduling is sometimes referred to as the shortest remaining time priority dispatch (Shortest-remaining-time-first scheduling).

such as the following example

Process Arrival Time interval Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

According to Gantt diagram:

Average Wait Time:

(0+0+ (5-3) + (10-1) + (17-2))/4 = 26/4 = 6.5

Non-preemption SJF:

(0+ (8-1) + (12-3) + (17-2))/4 = 7.75

Priority scheduling (scheduling algorithm)

The SJF algorithm can be used as a special case of the general priority scheduling algorithm. Each process has a priority associated with it, and the process with the highest priority is assigned to the CPU. Processes with the same priority are dispatched in FCFS order. SJF, whose priority (p) is the reciprocal of the next CPU interval. The larger the CPU interval, the smaller the priority, and vice versa.

The priority is usually a fixed-interval number, such as 0~7, but the number size and priority level are inconclusive.

For the following example, assume that the smaller the number, the higher the priority

Process interval Time Priority Level
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

The average wait time is:

(0+1+6+16+18)/5 = 8.2

Priorities can be defined either internally or externally. The internal definition priority uses some measurement data to calculate the process priority. External precedence is defined by criteria other than the operating system, such as process importance.

Priority scheduling can be preemptive or non-preemptive.

An important problem with priority scheduling algorithms is infinite blocking (indefinite blocking) or starvation (starvation). A process that can run but lacks CPU can be considered blocked, and it is waiting for the CPU. The priority scheduling algorithm causes one to have a low-priority infinite waiting CPU.

One of the low-priority processes to wait for the problem to be resolved is aging (aging). Aging is a technique that gradually increases the priority of a process that waits for a long time in the system.

Rotation scheduling (ROUND-ROBIN,RR)

Specially designed for time-sharing systems. It is similar to FCFS scheduling, but increases preemption to switch processes. Defines a smaller time unit, called a time slice, which is a temporal quantum,or slice. The ready queue is used as the loop queue. The CPU Scheduler loops the ready queue to allocate CPUs of no more than one time fragment per process.

The new process is added to the tail of the ready queue. The CPU scheduler selects the first process from the ready queue, sets the timer to break after a time slice, and then dispatches the process. There are two possible scenarios to follow. The process may only need to be less than the CPU interval of the time slice. In this case, the process itself automatically frees the CPU. The scheduler then processes the next process of the ready queue. Otherwise, if the current running process has a longer CPU interval than the time slice, the timer interrupts the operating system interrupt, then makes context switches, joins the process to the end of the ready queue, and the CPU scheduler chooses the next process in the ready queue.

The average wait time for RR policies is usually longer

For example, using 4ms time slices

Process interval Time
P1 24
P2 3
P3 3

Draw a Gantt diagram:

Average Wait Time:

(0+4+7+ (10-4))/3 = 5.66

If ready, then each process gets 1/n CPU time, which is not longer than the Q-time unit. Each process must wait for the CPU time not to exceed the (n-1) Q time unit until its next time slice.

The performance of the RR algorithm is largely dependent on the size of the time slice. In extreme cases, if the time slice is very large, then the RR algorithm is the same as the FCFS algorithm. If the time slice is small, then the RR algorithm is called processor sharing , and n processes have its own processor for the user, with a speed of 1/n for the true processor speed.

Small time slices increase the number of context switches, so hopefully the time slice is longer than the context switch, in fact, most modern operating systems, the context switch time only a fraction of the time slice.

Turnaround time also depends on the size of the time slice.

Multi-level queue scheduling (multilevel queue scheduling)

Foreground (interactive) and background (batch) processes. These two different types of processes have different response time requirements, and there are different scheduling needs. The foreground process has a higher (or externally defined) priority than the background process.

The multistage queue scheduling algorithm divides the ready queue into multiple Independiente columns. Depending on the properties of the process, such as memory size, a process is permanently assigned to a queue (low scheduling overhead but not flexible enough), and each queue has its own scheduling algorithm. The foreground queue may use the RR algorithm to dispatch, the latter schedule may use the FCFS algorithm dispatch.

In addition, there must be scheduling between queues, usually with a fixed priority preemption schedule, such as a foreground queue that can have absolute precedence over a background queue. Another possibility to divide the time slices between queues for example, the foreground queue can have 80% of the time for RR scheduling between processes, and the second queue can have 20% CPU time using the FCFS algorithm scheduling process.

Multilevel feedback queue scheduling (multilevel feedback-queue scheduling)

In contrast to multi-level queue scheduling, Multi-level feedback queue scheduling allows processes to move between queues. The main idea is to differentiate processes based on the characteristics of different CPU intervals. If the process uses too much CPU time, it may be transferred to a lower priority queue. This scenario leaves the I/O constraints and the interaction process in a higher priority queue. In addition, a process that waits too long in a lower priority queue is transferred to a higher-priority queue. This form of aging tissue starvation occurs.

In general, the multilevel feedback Queue scheduler can be defined by the following parameters:

    • The number of queues.
    • The scheduling algorithm for each queue.
    • The method used to determine when to upgrade to a higher priority queue.
    • The method used to determine when to downgrade to a lower priority queue.
    • The method used to determine which queue the process should enter when it needs services.
Multiprocessor scheduling (Multiple-processor scheduling)

If more than one CPU, the load distribution (load sharing) becomes possible. It focuses on the same (or homogeneous) system of processors that can be used by any processor to run any process within the queue.

Multi-Processor Scheduling method

In a multiprocessor, one method of CPU scheduling is to have one processor (master server) handle all scheduling decisions, I/O processing, and other system activities, and the other processors execute only user code. This asymmetric processing (asymmetric multiprocessing) approach is simpler because only one processor accesses the system data structure, reducing the need for data sharing.

Another approach is to use the symmetric multi-processing (symmetric MULTIPROCESSING,SMP) method, which is self-scheduling for each processor. All processes may be in a common ready queue, or each processor will have its own private-ready queue. In any case, the schedule checks the common-readiness queue by each processor and chooses a process to execute. If multiple processors attempt to access and update a common data structure, then each processor must be programmed to: you must ensure that two processors cannot select the same process and that the process is not lost from the queue.

Processor Affinity

When the process moves to a different processor, the contents of the cache of the first processor being migrated must be invalid, and the cache on the second processor to be migrated will need to be rebuilt. Because of the high cost of invalidating or refactoring the cache, SMP is trying to make a process run on the same processor, which is called processor affinity, which means that a process needs to have affinity for the other processor on which it is running.

Several forms of processor affinity:

    • Soft affinity (soft affinity, the operating system has policies that try to keep a process running on the same processor, but cannot guarantee any)

    • Rigid affinity (hard affinity, allows the process to specify that it is not allowed to move to another processor), such as Linux

Load balancing (Load Balancing)

Load balancing tries to distribute the workload evenly across all processors in the SMP system. It is usually only necessary for processors that have their own private executable processes, and in systems with common queues, load balancing is usually not required because once the processor is idle, an executable process is taken from the ready queue immediately.

Two methods: push migration and pull migration, for push migration, a specific task periodically checks the load on each processor, and if it finds an imbalance, by moving the process from the overloaded processor to (or pushed to) an idle or less busy processor , the load is distributed evenly, and pull migration occurs when the idle processor pushes a wait task from a busy processor. Push migration and pull migration cannot be mutually exclusive.

Load balancing will counteract processor affinity.

Symmetric multithreading

Provides multiple logical (and not physical) processors to run several threads, called symmetric Multithreading (SMT), or Hyper-Threading (hyperthreading) technology.

The idea of SMT is to generate multiple logical processors on the same physical processor, even if the system has only a single processor, each of which has its own architecture state, including general purpose and machine State registers. Each logical processor is responsible for its own interrupt handling, which means that interrupts are sent to and handled by the logical processor, and each logical processor shares its physical processor's resources, such as cache or bus.

SMT is provided by hardware, not software. The hardware should provide a representation of the schema state of each logical processor and an interrupt handling method. The scheduler first manages to dispatch different threads to each physical processor, instead of dispatching to different logical processors on the same physical processor.

Thread scheduling

The system is scheduling kernel threads, not processes. User threads are managed by line libraries, and the kernel does not understand them. The user thread must eventually be mapped to the appropriate kernel-level thread, although this mapping may be indirect and may use a lightweight process (LWP).

Scope of competition

One of the differences between a user thread and a kernel thread is how they are dispatched. On a multiple-to-one model and many-to-many model systems, the line libraries schedules user-level threads to run on a valid LWP, which is called the process contention range (Process-contention scope,pcs) method, Because CPU contention occurs between threads that belong to the same process. In order to decide which kernel thread is to be dispatched to the CPU, the kernel uses the system competitive range (System-contention Scope,scs) method, which competes for the CPU in all threads of the system, using a one-to-one model of the system, scheduling only using the SCS method.

PCs are done by priority.

Operating system concept learning Note CPU scheduling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.