Chapter 5 Flashcards

1
Q

In a system with a single CPUcore

A

only one process can run at a time. Others must wait until theCPU’s core is free and can be rescheduled

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The objective of multiprogramming is

A

g is to have some process running at all times, to maximize CPUutilization. The idea is relatively simple. Aprocess is executed until it must wait, typically for the completion of someI/Orequest. In a simple computer system, theCPUthen just sits idle. All this waiting time is wasted; no useful work is accomplished. With multiprogramming, we try to use this time produc-tively. Several processes are kept in memory at one time. When one process has to wait, the operating system takes theCPUaway from that process and gives theCPUto another process. This pattern continues. Every time one process has to wait, another process can take over use of theCPU. On a multicore system, this concept of keeping theCPUbusy is extended to all processing cores on the system.
Scheduling of this kind is a fundamental operating-system function.
Almost all computer resources are scheduled before use. TheCPUis, of course, one of the primary computer resources. Thus, its scheduling is central to operating-system design.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

process execution consists of a

A

of a cycle ofCPUexecution and I/O wait. Processes alternate between these two states. Process execution begins with a CPU burst.
That is followed by an I/O burst, which is followed by anotherCPUburst, then anotherI/Oburst, and so on. Eventually, the f i nalCPUburst ends with a system request to terminate execution (Figure 5.1).
The durations ofCPUbursts have been measured extensively. Although they vary greatly from process to process and from computer to computer, they tend to have a frequency curve similar to that shown in Figure 5.2. The curve is generally characterized as exponential or hyperexponential, with a large number of shortCPUbursts and a small number of long CPUbursts.
AnI/O-bound program typically has many shortCPUbursts. ACPU-bound program might have a few long CPUbursts. This distribution can be important when implementing aCPU-scheduling algorithm.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

CPU Scheduler

A

Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed. The selection process is carried out by the CPU scheduler, which selects a process from the processes in memory that are ready to execute and allocates the CPU to that process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

CPU-scheduling decisions may take place under the following four circum-stances

A
  1. When a process switches from the running state to the waiting state (for example, as the result of an I/O request or an invocation of wait() for the termination of a child process)
  2. When a process switches from the running state to the ready state (for example, when an interrupt occurs)
  3. When a process switches from the waiting state to the ready state (for example, at completion of I/O) 4.
    When a process terminates

For situations 1 and 4, there is no choice in terms of scheduling. A new process (if one exists in the ready queue) must be selected for execution. There is a choice, however, for situations 2 and 3.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

scheduling scheme is nonpreemptive or cooperative when

A
  1. When a process switches from the running state to the waiting state (for example, as the result of anI/Orequest or an invocation ofwait()for the termination of a child process)
  2. When a process terminates

When scheduling takes place only under circumstances 1 and 4, we say that the scheduling scheme is nonpreemptive or cooperative. Otherwise, it is pre-emptive. Under nonpreemptive scheduling, once theCPUhas been allocated to a process, the process keeps theCPUuntil it releases it either by terminating or by switching to the waiting state. Virtually all modern operating systems including Windows, macOS, Linux, andUNIXuse preemptive scheduling algo-rithms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

pre-emptive scheduling

A

Otherwise, it is pre-emptive. Under nonpreemptive scheduling, once theCPUhas been allocated to a process, the process keeps theCPUuntil it releases it either by terminating or by switching to the waiting state. Virtually all modern operating systems including Windows, macOS, Linux, andUNIXuse preemptive scheduling algo-rithms.
Unfortunately, preemptive scheduling can result in race conditions when data are shared among several processes. Consider the case of two processes that share data. While one process is updating the data, it is preempted so that the second process can run. The second process then tries to read the data, which are in an inconsistent state. This issue will be explored in detail in Chapter 6.
Preemption also affects the design of the operating-system kernel. During the processing of a system call, the kernel may be busy with an activity on behalf of a process. Such activities may involve changing important kernel data (for instance,I/Oqueues). What happens if the process is preempted in the middle of these changes and the kernel (or the device driver) needs to read or modify the same structure? Chaos ensues. As will be discussed in Section 6.2, operating-system kernels can be designed as either nonpreemptive or preemptive. A nonpreemptive kernel will wait for a system call to complete or for a process to block while waiting forI/Oto complete to take place before doing a context switch. This scheme ensures that the kernel structure is simple, since the kernel will not preempt a process while the kernel data structures are in an inconsistent state. Unfortunately, this kernel-execution model is a poor one for supporting real-time computing, where tasks must complete execution within a given time frame. In Section 5.6, we explore scheduling demands of real-time systems. A preemptive kernel requires mechanisms such as mutex locks to prevent race conditions when accessing shared kernel data structures.
Most modern operating systems are now fully preemptive when running in kernel mode.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Because interrupts can, by definition, occur at any time, and because they cannot always be ignored by the kernel, the sections of code affected by inter-rupts must be guarded from simultaneous use

A

use.The operating system needs to accept interrupts at almost all times. Otherwise, input might be lost or output overwritten. So that these sections of code are not accessed concurrently by several processes, they disable interrupts at entry and reenable interrupts at exit. It is important to note that sections of code that disable interrupts do not occur very often and typically contain few instructions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Dispatcher

A

Another component involved in the CPU-scheduling function is the dispatcher.
The dispatcher is the module that gives control of the CPU’s core to the process selected by the CPU scheduler. This function involves the following:
•Switching context from one process to another
•Switching to user mode
•Jumping to the proper location in the user program to resume that program
The dispatcher should be as fast as possible, since it is invoked during every context switch. The time it takes for the dispatcher to stop one process and start another running is known as the dispatch latency

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

voluntary and nonvoluntary context switches

A

voluntary context switch occurs when a process has given up control of the CPU because it requires a resource that is currently unavailable (such as blocking for I/O.) A non voluntary context switch occurs when the CPU has been taken away from a process, such as when its time slice has expired or it has been preempted by a higher-priority process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

CPU scheduling algorithms criteria

A

•CPU utilization. We want to keep the CPU as busy as possible. Conceptually, CPU utilization can range from 0 to 100 percent. In a real system, it should range from 40 percent (for a lightly loaded system) to 90 percent (for a heavily loaded system). (CPU utilization can be obtained by using thetopcommand on Linux, macOS, and UNIX systems.)
•Throughput. If the CPU is busy executing processes, then work is being done. One measure of work is the number of processes that are completed per time unit, called throughput. For long processes, this rate may be one process over several seconds; for short transactions, it may be tens of processes per second.
•Turnaround time. From the point of view of a particular process, the important criterion is how long it takes to execute that process. The interval from the time of submission of a process to the time of completion is the turnaround time. Turnaround time is the sum of the periods spent waiting in the ready queue, executing on the CPU, and doing I/O.
•Waiting time. The CPU-scheduling algorithm does not affect the amount of time during which a process executes or does I/O. It affects only the amount of time that a process spends waiting in the ready queue. Waiting time is the sum of the periods spent waiting in the ready queue.
•Response time. In an interactive system, turnaround time may not be the best criterion. Often, a process can produce some output fairly early and can continue computing new results while previous results are being output to the user. Thus, another measure is the time from the submission of a request until the first response is produced. This measure, called response time, is the time it takes to start responding, not the time it takes to output the response
It is desirable to maximize CPU utilization and throughput and to minimize turnaround time, waiting time, and response time. In most cases, we optimize the average measure. However, under some circumstances, we prefer to optimize the minimum or maximum values rather than the average. For example, to guarantee that all users get good service, we may want to minimize the maximum response time.
Investigators have suggested that, for interactive systems (such as a PC desktop or laptop system), it is more important to minimize the variance in the response time than to minimize the average response time. A system with reasonable and predictable response time may be considered more desirable than a system that is faster on the average but is highly variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

First-Come, First-Served Scheduling

A

By far the simplest CPU-scheduling algorithm is the first-come first-serve (FCFS) scheduling algorithm. With this scheme, the process that requests the CPU first is allocated the CPU first. The implementation of the FCFS policy is easily managed with a FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. The code for FCFS scheduling is simple to write and understand.
On the negative side, the average waiting time under the FCFS policy is often quite long.
In addition, consider the performance of FCFS scheduling in a dynamic situation. Assume we have one CPU-bound process and manyI/O-bound processes. As the processes flow around the system, the following scenario may result. The CPU-bound process will get and hold theCPU. During this time, all the other processes will finish their I/O and will move into the ready queue, waiting for the CPU. While the processes wait in the ready queue, the I/O devices are idle. Eventually, the CPU-bound process finishes its CPU burst and moves to an I/O device. All the I/O-bound processes, which have short CPU bursts, execute quickly and move back to the I/O queues. At this point, the CPU sits idle. TheCPU-bound process will then move back to the ready queue and be allocated theCPU. Again, all the I/O processes end up waiting in the ready queue until theCPU-bound process is done. There is a convoy effect as all the other processes wait for the one big process to get off the CPU. This effect results in lower CPU and device utilization than might be possible if the shorter processes were allowed to go f i rst.
Note also that the FCFSscheduling algorithm is nonpreemptive. Once the CPU has been allocated to a process, that process keeps the CPU until it releases theCPU, either by terminating or by requesting I/O. The FCFS algorithm is thus particularly troublesome for interactive systems, where it is important that each process get a share of the CPU at regular intervals. It would be disastrous to allow one process to keep the CPU for an extended period.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Shortest-Job-First Scheduling

A

A different approach to CPU scheduling is the shortest-job-firs (SJF) scheduling algorithm. This algorithm associates with each process the length of the process’s next CPU burst. When the CPU is available, it is assigned to the process that has the smallest next CPU burst. If the next CPU bursts of two processes are the same,FCFS scheduling is used to break the tie. Note that a more appropriate term for this scheduling method would be the shortest-next-CPU-burst algorithm, because scheduling depends on the length of the nextCPUburst of a process, rather than its total length. We use the term SJF because most people and textbooks use this term to refer to this type of scheduling. The SJF scheduling algorithm is provably optimal, in that it gives the mini-mum average waiting time for a given set of processes. Moving a short process before a long one decreases the waiting time of the short process more than it increases the waiting time of the long process. Consequently, the average waiting time decreases.
Although the SJF algorithm is optimal, it cannot be implemented at the level of CPU scheduling, as there is no way to know the length of the nextCPUburst.
One approach to this problem is to try to approximate SJF scheduling. We may not know the length of the next CPU burst, but we may be able to predict its value. We expect that the nextCPUburst will be similar in length to the previous ones. By computing an approximation of the length of the nextCPUburst, we can pick the process with the shortest predicted CPU burst. Use exponential average. The SJF algorithm can be either preemptive or nonpreemptive. The choice arises when a new process arrives at the ready queue while a previous process is still executing. The next CPU burst of the newly arrived process may be shorter than what is left of the currently executing process. A preemptive SJF algorithm will preempt the currently executing process, whereas a non-preemptive SJF algorithm will allow the currently running process to finish its CPU burst. Preemptive SJF scheduling is sometimes called shortest-remaining-time-firs scheduling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Round-Robin Scheduling

A

The round-robin (RR) scheduling algorithm is similar to FCFS scheduling, but preemption is added to enable the system to switch between processes. A small unit of time, called a time quantum or time slice, is defined. A time quantum is generally from 10 to 100 milliseconds in length. The ready queue is treated as a circular queue. The CPU scheduler goes around the ready queue, allocating the CPU to each process for a time interval of up to 1 time quantum.
To implement RR scheduling, we again treat the ready queue as a FIFO queue of processes. New processes are added to the tail of the ready queue.
The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt after 1 time quantum, and dispatches the process.
One of two things will then happen. The process may have a CPU burst of less than 1 time quantum. In this case, the process itself will release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue. If the CPU burst of the currently running process is longer than 1 time quantum, the timer will go off and will cause an interrupt to the operating system. A context switch will be executed, and the process will be put at the tail of the ready queue. The CPU scheduler will then select the next process in the ready queue.
The average waiting time under the RR policy is often long.
The performance of theRRalgorithm depends heavily on the size of the time quantum. At one extreme, if the time quantum is extremely large, the RRpolicy is the same as theFCFSpolicy. In contrast, if the time quantum is extremely small (say, 1 millisecond), the RR approach can result in a large
process of 10 time units. If the quantum is 12 time units, the process f i nishes in less than 1 time quantum, with no overhead. If the quantum is 6 time units, however, the process requires 2 quanta, resulting in a context switch. If the time quantum is 1 time unit, then nine context switches will occur, slowing the execution of the process accordingly (Figure 5.5).
Thus, we want the time quantum to be large with respect to the context-switch time. If the context-switch time is approximately 10 percent of the time quantum, then about 10 percent of theCPUtime will be spent in context switching. In practice, most modern systems have time quanta ranging from 10 to 100 milliseconds. The time required for a context switch is typically less than 10 microseconds; thus, the context-switch time is a small fraction of the time quantum.
Turnaround time also depends on the size of the time quantum. As we can see from Figure 5.6, the average turnaround time of a set of processes does not necessarily improve as the time-quantum size increases. In general, the average turnaround time can be improved if most processes f i nish their nextCPUburst in a single time quantum. For example, given three processes of 10 time units each and a quantum of 1 time unit, the average turnaround time is 29. If the time quantum is 10, however, the average turnaround time drops to 20. If context-switch time is added in, the average turnaround time increases even more for a smaller time quantum, since more context switches are required.
Although the time quantum should be large compared with the context-switch time, it should not be too large. As we pointed out earlier, if the time quantum is too large,RRscheduling degenerates to anFCFSpolicy. A rule of thumb is that 80 percent of theCPUbursts should be shorter than the time quantum.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Priority Scheduling

A

The SJF algorithm is a special case of the general priority-scheduling algorithm.
A priority is associated with each process, and the CPU is allocated to the process with the highest priority. Equal-priority processes are scheduled in FCFS order. An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse of the (predicted) nextCPUburst. The larger the CPU burst, the lower the priority, and vice versa.
Note that we discuss scheduling in terms of high priority and low priority.
Priorities are generally indicated by some fixed range of numbers, such as 0 to 7 or 0 to 4,095. However, there is no general agreement on whether 0 is the highest or lowest priority. Some systems use low numbers to represent low priority; others use low numbers for high priority. This difference can lead to confusion. In this text, we assume that low numbers represent high priority.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Priorities can be defined either internally or externally

A

Internally defined priorities use some measurable quantity or quantities to compute the priority of a process. For example, time limits, memory requirements, the number of open files, and the ratio of average I/O burst to average CPU burst have been used in computing priorities. External priorities are set by criteria outside the operating system, such as the importance of the process, the type and amount of funds being paid for computer use, the department sponsoring the work, and other, often political, factors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Priority scheduling can be either preemptive or nonpreemptive.

A

When a process arrives at the ready queue, its priority is compared with the priority of the currently running process. A preemptive priority scheduling algorithm will preempt the CPU if the priority of the newly arrived process is higher than the priority of the currently running process. A nonpreemptive priority scheduling algorithm will simply put the new process at the head of the ready queue.

18
Q

A major problem with priority scheduling algorithms is indefinite blocking, or starvation.

A

A process that is ready to run but waiting for the CPU can be considered blocked. A priority scheduling algorithm can leave some low-priority processes waiting in definitely. In a heavily loaded computer system, a steady stream of higher-priority processes can prevent a low-priority process from ever getting theCPU. Generally, one of two things will happen. Either the process will eventually be run (at 2A.M.Sunday, when the system is finally lightly loaded), or the computer system will eventually crash and lose all unfinished low-priority processes. (Rumor has it that when they shut down the IBM 7094 at MITin 1973, they found a low-priority process that had been submitted in 1967 and had not yet been run.) A solution to the problem of indefinite blockage of low-priority processes is aging. Aging involves gradually increasing the priority of processes that wait in the system for a long time. For example, if priorities range from 127 (low) to 0 (high), we could periodically (say, every second) increase the priority of a waiting process by 1. Eventually, even a process with an initial priority of 127 would have the highest priority in the system and would be executed. In fact, it would take a little over 2 minutes for a priority-127 process to age to a priority-0 process.
Another option is to combine round-robin and priority scheduling in such a way that the system executes the highest-priority process and runs processes with the same priority using round-robin scheduling.

19
Q

Multilevel Queue Scheduling

A

With both priority and round-robin scheduling, all processes may be placed in a single queue, and the scheduler then selects the process with the highest priority to run. Depending on how the queues are managed, an O(n) search may be necessary to determine the highest-priority process. In practice, it is often easier to have separate queues for each distinct priority, and priority scheduling simply schedules the process in the highest-priority queue. This is illustrated in Figure 5.7. This approach—known as multilevel queue— also works well when priority scheduling is combined with round-robin: if there are multiple processes in the highest-priority queue, they are executed in round-robin order. In the most generalized form of this approach, a priority is assigned statically to each process, and a process remains in the same queue for the duration of its runtime.
A multilevel queue scheduling algorithm can also be used to partition processes into several separate queues based on the process type (Figure 5.8). For example, a common division is made between foreground (interactive) processes and background (batch) processes. These two types of processes have different response-time requirements and so may have different scheduling needs. In addition, foreground processes may have priority (externally defined) over background processes. Separate queues might be used for foreground and background processes, and each queue might have its own scheduling algorithm. The foreground queue might be scheduled by an bRR algorithm, for example, while the background queue is scheduled by an FCFS algorithm.
In addition, there must be scheduling among the queues, which is commonly implemented as fixed-priority preemptive scheduling. For example, the real-time queue may have absolute priority over the interactive queue.
Let’s look at an example of a multilevel queue scheduling algorithm with four queues, listed below in order of priority:
1. Real-time processes
2. System processes
3. Interactive processes
4. Batch processes
Each queue has absolute priority over lower-priority queues. No process in the batch queue, for example, could run unless the queues for real-time processes, system processes, and interactive processes were all empty. If an interactive process entered the ready queue while a batch process was running, the batch process would be preempted.

20
Q

Multilevel Feedback Queue Scheduling

A

Normally, when the multilevel queue scheduling algorithm is used, processes are permanently assigned to a queue when they enter the system. If there are separate queues for foreground and background processes, for example, processes do not move from one queue to the other, since processes do not change their foreground or background nature. This setup has the advantage of low scheduling overhead, but it is inflexible.
The multilevel feedback queue scheduling algorithm, in contrast, allows a process to move between queues. The idea is to separate processes according to the characteristics of their CPU bursts. If a process uses too much CPU time, it will be moved to a lower-priority queue. This scheme leaves I/O-bound and interactive processes—which are typically characterized by shortCPUbursts —in the higher-priority queues. In addition, a process that waits too long in a lower-priority queue may be moved to a higher-priority queue. This form of aging prevents starvation.
For example, consider a multilevel feedback queue scheduler with three queues, numbered from 0 to 2 (Figure 5.9). The scheduler f i rst executes all processes in queue 0. Only when queue 0 is empty will it execute processes in queue 1. Similarly, processes in queue 2 will be executed only if queues 0 and 1 are empty. A process that arrives for queue 1 will preempt a process in queue 2. A process in queue 1 will in turn be preempted by a process arriving for queue 0.
An entering process is put in queue 0. A process in queue 0 is given a time quantum of 8 milliseconds. If it does not finish within this time, it is moved to the tail of queue 1. If queue 0 is empty, the process at the head of queue 1 is given a quantum of 16 milliseconds. If it does not complete, it is preempted and is put into queue 2. Processes in queue 2 are run on an FCFS basis but are run only when queues 0 and 1 are empty. To prevent starvation, a process that waits too long in a lower-priority queue may gradually be moved to a higher-priority queue.
This scheduling algorithm gives highest priority to any process with a CPU burst of 8 milliseconds or less. Such a process will quickly get the CPU, finish its CPU burst, and go off to its next I/O burst. Processes that need more than 8 but less than 24 milliseconds are also served quickly, although with lower priority than shorter processes. Long processes automatically sink to queue 2 and are served inFCFSorder with anyCPUcycles left over from queues 0 and 1.
In general, a multilevel feedback queue scheduler is def i ned by the follow-ing parameters:
•The number of queues
•The scheduling algorithm for each queue
•The method used to determine when to upgrade a process to a higher-priority queue
•The method used to determine when to demote a process to a lower-priority queue
•The method used to determine which queue a process will enter when that process needs service
The definition of a multilevel feedback queue scheduler makes it the most general CPU-scheduling algorithm. It can be conf i gured to match a specific system under design. Unfortunately, it is also the most complex algorithm, since def i ning the best scheduler requires some means by which to select values for all the parameters.

21
Q

Thread Scheduling

A

On most modern operating systems it is kernel-level threads—not processes—that are being scheduled by the operating system. User-level threads are managed by a thread library, and the kernel is unaware of them. To run on a CPU, user-level threads must ultimately be mapped to an associated kernel-level thread, although this mapping may be indirect and may use a lightweight process (LWP). In this section, we explore scheduling issues involving user-level and kernel-level threads and offer specific examples of scheduling for Pthreads.

22
Q

Contention Scope

A

One distinction between user-level and kernel-level threads lies in how they are scheduled. On systems implementing the many-to-one (Section 4.3.1) and many-to-many (Section 4.3.3) models, the thread library schedules user-level threads to run on an available LWP. This scheme is known as process-contention scope (PCS), since competition for the CPU takes place among threads belonging to the same process. (When we say the thread library sched-ules user threads onto available LWPs, we do not mean that the threads are actually running on aCPUas that further requires the operating system to schedule the LWP’s kernel thread onto a physical CPU core.) To decide which kernel-level thread to schedule onto aCPU, the kernel uses system-contention scope (SCS). Competition for the CPU with SCS scheduling takes place among all threads in the system. Systems using the one-to-one model (Section 4.3.2), such as Windows and Linux schedule threads using onlySCS.
Typically,PCS is done according to priority—the scheduler selects the runnable thread with the highest priority to run. User-level thread priorities are set by the programmer and are not adjusted by the thread library, although some thread libraries may allow the programmer to change the priority of a thread. It is important to note that PCS will typically preempt the thread currently running in favor of a higher-priority thread; however, there is no guarantee of time slicing (Section 5.3.3) among threads of equal priority.

23
Q

Pthread Scheduling

A

We provided a samplePOSIXPthread program in Section 4.4.1, along with an introduction to thread creation with Pthreads. Now, we highlight thePOSIX PthreadAPIthat allows specifyingPCSorSCSduring thread creation. Pthreads identif i es the following contention scope values:
•PTHREAD SCOPE PROCESSschedules threads usingPCSscheduling.
•PTHREAD SCOPE SYSTEMschedules threads usingSCSscheduling.
On systems implementing the many-to-many model, the PTHREAD SCOPE PROCESSpolicy schedules user-level threads onto available LWPs. The number ofLWPs is maintained by the thread library, perhaps using scheduler activations (Section 4.6.5). ThePTHREAD SCOPE SYSTEMscheduling policy will create and bind anLWPfor each user-level thread on many-to-many systems, effectively mapping threads using the one-to-one policy.
The PthreadIPC(Interprocess Communication) provides two functions for setting—and getting—the contention scope policy:
•pthread attr setscope(pthread attr t *attr, int scope) •pthread attr getscope(pthread attr t *attr, int *scope) The f i rst parameter for both functions contains a pointer to the attribute set for the thread. The second parameter for thepthread attr setscope()function is passed either thePTHREAD SCOPE SYSTEMor thePTHREAD SCOPE PROCESS value, indicating how the contention scope is to be set. In the case of pthread attr getscope(), this second parameter contains a pointer to an intvalue that is set to the current value of the contention scope. If an error occurs, each of these functions returns a nonzero value.
In Figure 5.10, we illustrate a Pthread schedulingAPI. The pro-gramf i rstdeterminestheexistingcontentionscopeandsetsitto PTHREAD SCOPE SYSTEM. It then creates f i ve separate threads that will run using theSCSscheduling policy. Note that on some systems, only certain contention scope values are allowed. For example, Linux and macOSsystems allow onlyPTHREAD SCOPE SYSTEM.

24
Q

Multi-Processor Scheduling

A

Our discussion thus far has focused on the problems of scheduling theCPUin a system with a single processing core. If multipleCPUs are available, load sharing, where multiple threads may run in parallel, becomes possible, however scheduling issues become correspondingly more complex. Many possibilities have been tried; and as we saw with CPU scheduling with a single-core CPU, there is no one best solution.
Traditionally, the term multiprocessor referred to systems that provided multiple physical processors, where each processor contained one single-core CPU. However, the def i nition of multiprocessor has evolved signif i cantly, and on modern computing systems, multiprocessor now applies to the following system architectures:
•MulticoreCPUs •Multithreaded cores •NUMAsystems •Heterogeneous multiprocessing Here, we discuss several concerns in multiprocessor scheduling in the con-text of these different architectures. In the f i rst three examples we concentrate on systems in which the processors are identical—homogeneous—in terms of their functionality. We can then use any availableCPUto run any process in the queue. In the last example we explore a system where the processors are not identical in their capabilities.

25
Q

Approaches to Multiple-Processor Scheduling

A

One approach toCPUscheduling in a multiprocessor system has all scheduling decisions, I/O processing, and other system activities handled by a single processor — the master server. The other processors execute only user code.
This asymmetric multiprocessing is simple because only one core accesses the system data structures, reducing the need for data sharing. The downfall of this approach is the master server becomes a potential bottleneck where overall system performance may be reduced.
The standard approach for supporting multiprocessors is symmetric mul-tiprocessing (SMP), where each processor is self-scheduling. Scheduling proceeds by having the scheduler for each processor examine the ready queue and select a thread to run. Note that this provides two possible strategies for organizing the threads eligible to be scheduled:
1. All threads may be in a common ready queue.
2. Each processor may have its own private queue of threads.

26
Q

memory stall

A

Researchers have discovered that when a processor accesses memory, it spends a significant amount of time waiting for the data to become available. This situation, known as a memory stall, occurs primarily because modern processors operate at much faster speeds than memory. However, a memory stall can also occur because of a cache miss (accessing data that are not in cache memory). Figure 5.12 illustrates a memory stall. In this scenario, the processor can spend up to 50 percent of its time waiting for data to become available from memory.
To remedy this situation, many recent hardware designs have implemented multithreaded processing cores in which two (or more) hardware threads are assigned to each core. That way, if one hardware thread stalls while waiting for memory, the core can switch to another thread. Figure 5.13 illustrates a dual-threaded processing core on which the execution of thread 0 and the execution of thread 1 are interleaved. From an operating system perspective, each hardware thread maintains its architectural state, such as instruction pointer and register set, and thus appears as a logical CPU that is available to run a software thread. This technique—known as chip multithreading (CMT) —is illustrated in Figure 5.14. Here, the processor contains four computing cores, with each core containing two hardware threads. From the perspective of the operating system, there are eight logical CPUs.
Intel processors use the term hyper-threading (also known as simultaneous multithreading or SMT) to describe assigning multiple hardware threads to a single processing core. Contemporary Intel processors—such as the i7—support two threads per core, while the Oracle Sparc M7 processor supports eight threads per core, with eight cores per processor, thus providing the operating system with 64 logical CPUs.

27
Q

In general, there are two ways to multithread a processing core: coarse-grained and fine-graine multithreading.

A

With coarse-grained multithreading, a thread executes on a core until a long-latency event such as a memory stall occurs. Because of the delay caused by the long-latency event, the core must switch to another thread to begin execution. However, the cost of switching between threads is high, since the instruction pipeline must be flushed before the other thread can begin execution on the processor core. Once this new thread begins execution, it begins filling the pipeline with its instructions.
Fine-grained (or interleaved) multithreading switches between threads at a much finer level of granularity—typically at the boundary of an instruction. However, the architectural design of fine-grained systems includes logic for thread switching. As a result, the cost of switching between threads is small.
It is important to note that the resources of the physical core (such as caches and pipelines) must be shared among its hardware threads, and therefore a processing core can only execute one hardware thread at a time. Consequently, a multithreaded, multicore processor actually requires two different levels of scheduling, as shown in Figure 5.15, which illustrates a dual-threaded processing core.

28
Q

Levels of scheduling decisions

A

On one level are the scheduling decisions that must be made by the oper-ating system as it chooses which software thread to run on each hardware thread (logicalCPU). For all practical purposes, such decisions have been the primary focus of this chapter. Therefore, for this level of scheduling, the oper-ating system may choose any scheduling algorithm, including those described in Section 5.3.
A second level of scheduling specif i es how each core decides which hard-ware thread to run. There are several strategies to adopt in this situation. One approach is to use a simple round-robin algorithm to schedule a hardware thread to the processing core. This is the approach adopted by the UltraSPARC T3. Another approach is used by the Intel Itanium, a dual-core processor with two hardware-managed threads per core. Assigned to each hardware thread is a dynamic urgency value ranging from 0 to 7, with 0 representing the lowest urgency and 7 the highest. The Itanium identif i es f i ve different events that may trigger a thread switch. When one of these events occurs, the thread-switching logic compares the urgency of the two threads and selects the thread with the highest urgency value to execute on the processor core.
Note that the two different levels of scheduling shown in Figure 5.15 are not necessarily mutually exclusive. In fact, if the operating system scheduler (the f i rst level) is made aware of the sharing of processor resources, it can make more effective scheduling decisions. As an example, assume that a CPU has two processing cores, and each core has two hardware threads. If two software threads are running on this system, they can be running either on the same core or on separate cores. If they are both scheduled to run on the same core, they have to share processor resources and thus are likely to proceed more slowly than if they were scheduled on separate cores. If the operating system is aware of the level of processor resource sharing, it can schedule software threads onto logical processors that do not share resources.

29
Q

Load Balancing

A

On SMP systems, it is important to keep the workload balanced among all processors to fully utilize the benefits of having more than one processor. Otherwise, one or more processors may sit idle while other processors have high workloads, along with ready queues of threads awaiting the bCPU. Load balancing attempts to keep the workload evenly distributed across all processors in an vSMP system. It is important to note that load balancing is typically necessary only on systems where each processor has its own private ready queue of eligible threads to execute. On systems with a common run queue, load balancing is unnecessary, because once a processor becomes idle, it immediately extracts a runnable thread from the common ready queue.

30
Q

There are two general approaches to load balancing: push migration and pull migration.

A

With push migration, a specific task periodically checks the load on each processor and—if it finds an imbalance—evenly distributes the load by moving (or pushing) threads from overloaded to idle or less-busy processors. Pull migration occurs when an idle processor pulls a waiting task from a busy processor. Push and pull migration need not be mutually exclusive and are, in fact, often implemented in parallel on load-balancing systems. For example, the LinuxCFSscheduler (described in Section 5.7.1) and theULE scheduler available forFreeBSDsystems implement both techniques.
The concept of a “balanced load” may have different meanings. One view of a balanced load may require simply that all queues have approximately the same number of threads. Alternatively, balance may require an equal distri-bution of thread priorities across all queues.

31
Q

processor affinit

A

Because of the high cost of invalidating and repopulating caches, most operating systems withSMPsupport try to avoid migrating a thread from one processor to another and instead attempt to keep a thread running on the same processor and take advantage of a warm cache. This is known as processor affinit —that is, a process has an aff i nity for the processor on which it is currently running.
The two strategies described in Section 5.5.1 for organizing the queue of threads available for scheduling have implications for processor aff i nity. If we adopt the approach of a common ready queue, a thread may be selected for execution by any processor. Thus, if a thread is scheduled on a new processor, that processor’s cache must be repopulated. With private, per-processor ready queues, a thread is always scheduled on the same processor and can therefore benef i t from the contents of a warm cache. Essentially, per-processor ready queues provide processor aff i nity for free!
Processor aff i nity takes several forms. When an operating system has a policy of attempting to keep a process running on the same processor—but not guaranteeing that it will do so—we have a situation known as soft affinit .
Here, the operating system will attempt to keep a process on a single processor, but it is possible for a process to migrate between processors during load balancing. In contrast, some systems provide system calls that support hard affinit , thereby allowing a process to specify a subset of processors on which it can run. Many systems provide both soft and hard aff i nity. For example, Linux implements soft aff i nity, but it also provides thesched setaffinity()system call, which supports hard aff i nity by allowing a thread to specify the set ofCPUs on which it is eligible to run.

32
Q

Heterogeneous Multiprocessing

A

sys-tems are now designed using cores that run the same instruction set, yet vary in terms of their clock speed and power management, including the ability to adjust the power consumption of a core to the point of idling the core. Such systems are known as heterogeneous multiprocessing (HMP). Note this is not a form of asymmetric multiprocessing as described in Section 5.5.1 as both system and user tasks can run on any core. Rather, the intention behind HMPis to better manage power consumption by assigning tasks to certain cores based upon the specific demands of the task.
ForARMprocessors that support it, this type of architecture is known as big.LITTLEwhere higher-peformance big cores are combined with energy eff i cientLITTLEcores. Big cores consume greater energy and therefore should only be used for short periods of time. Likewise, little cores use less energy and can therefore be used for longer periods.
There are several advantages to this approach. By combining a number of slower cores with faster ones, avCPU scheduler can assign tasks that do not require high performance, but may need to run for longer periods, (such as background tasks) to little cores, thereby helping to preserve a battery charge.
Similarly, interactive applications which require more processing power, but may run for shorter durations, can be assigned to big cores. Additionally, if the mobile device is in a power-saving mode, energy-intensive big cores can be disabled and the system can rely solely on energy-efficient little cores. Windows 10 supports HMPvscheduling by allowing a thread to select a scheduling policy that best supports its power management demands.

33
Q

CPU scheduling for real-time operating systems involves special issues. In general, we can distinguish between soft real-time systems and hard real-time systems.

A

Soft real-time systems provide no guarantee as to when a critical real-time process will be scheduled. They guarantee only that the process will be given preference over noncritical processes. Hard real-time systems have stricter requirements. A task must be serviced by its deadline; service after the deadline has expired is the same as no service at all. In this section, we explore several issues related to process scheduling in both soft and hard real-time operating systems.

34
Q

Minimize latency

A

Consider the event-driven nature of a real-time system. The system is typically waiting for an event in real time to occur. Events may arise either in software —as when a timer expires—or in hardware—as when a remote-controlled vehicle detects that it is approaching an obstruction. When an event occurs, the system must respond to and service it as quickly as possible. We refer to event latency as the amount of time that elapses from when an event occurs to when it is serviced (Figure 5.17). Usually, different events have different latency requirements. For example, the latency requirement for an antilock brake system might be 3 to 5 millisec-onds. That is, from the time a wheel f i rst detects that it is sliding, the system controlling the antilock brakes has 3 to 5 milliseconds to respond to and control the situation. Any response that takes longer might result in the automobile’s veering out of control. In contrast, an embedded system controlling radar in an airliner might tolerate a latency period of several seconds.

35
Q

Two types of latencies affect the performance of real-time systems:
1. Interrupt latency 2. Dispatch latency

A

Interrupt latency refers to the period of time from the arrival of an interrupt at theCPUto the start of the routine that services the interrupt. When an interrupt occurs, the operating system must f i rst complete the instruction it is executing and determine the type of interrupt that occurred. It must then save the state of the current process before servicing the interrupt using the specif i c interrupt service routine (ISR). The total time required to perform these tasks is the interrupt latency (Figure 5.18).
Obviously, it is crucial for real-time operating systems to minimize inter-rupt latency to ensure that real-time tasks receive immediate attention. Indeed, for hard real-time systems, interrupt latency must not simply be minimized, it must be bounded to meet the strict requirements of these systems.
One important factor contributing to interrupt latency is the amount of time interrupts may be disabled while kernel data structures are being updated. Real-time operating systems require that interrupts be disabled for only very short periods of time.
The amount of time required for the scheduling dispatcher to stop one process and start another is known as dispatch latency. Providing real-time task tasks with immediate access to theCPUmandates that real-time operating systems minimize this latency as well. The most effective technique for keeping dispatch latency low is to provide preemptive kernels. For hard real-time systems, dispatch latency is typically measured in several microseconds.
In Figure 5.19, we diagram the makeup of dispatch latency. The conflic phase of dispatch latency has two components:
1. Preemption of any process running in the kernel 2. Release by low-priority processes of resources needed by a high-priority process Following the conf l ict phase, the dispatch phase schedules the high-priority process onto an availableCPU.

36
Q

Priority-Based Scheduling

A

The most important feature of a real-time operating system is to respond imme-diately to a real-time process as soon as that process requires theCPU. As a result, the scheduler for a real-time operating system must support a priority-based algorithm with preemption. Recall that priority-based scheduling algo-rithms assign each process a priority based on its importance; more important tasks are assigned higher priorities than those deemed less important. If the scheduler also supports preemption, a process currently running on theCPU will be preempted if a higher-priority process becomes available to run.
Preemptive, priority-based scheduling algorithms are discussed in detail in Section 5.3.4, and Section 5.7 presents examples of the soft real-time schedul-ing features of the Linux, Windows, and Solaris operating systems. Each of these systems assigns real-time processes the highest scheduling priority. For example, Windows has 32 different priority levels. The highest levels—priority values 16 to 31—are reserved for real-time processes. Solaris and Linux have similar prioritization schemes.
Note that providing a preemptive, priority-based scheduler only guaran-tees soft real-time functionality. Hard real-time systems must further guarantee that real-time tasks will be serviced in accord with their deadline requirements, and making such guarantees requires additional scheduling features. In the remainder of this section, we cover scheduling algorithms appropriate for hard real-time systems.
Before we proceed with the details of the individual schedulers, however, we must def i ne certain characteristics of the processes that are to be scheduled.
First, the processes are considered periodic. That is, they require theCPUat constant intervals (periods). Once a periodic process has acquired theCPU, it has a f i xed processing time t, a deadline d by which it must be serviced by the CPU, and a period p. The relationship of the processing time, the deadline, and the period can be expressed as 0 ≤ t ≤ d ≤ p. The rate of a periodic task is 1∕p.
Figure 5.20 illustrates the execution of a periodic process over time. Schedulers can take advantage of these characteristics and assign priorities according to a process’s deadline or rate requirements.
What is unusual about this form of scheduling is that a process may have to announce its deadline requirements to the scheduler. Then, using a technique known as an admission-control algorithm, the scheduler does one of two things. It either admits the process, guaranteeing that the process will complete on time, or rejects the request as impossible if it cannot guarantee that the task will be serviced by its deadline.

37
Q

Rate-Monotonic Scheduling

A

The rate-monotonic scheduling algorithm schedules periodic tasks using a static priority policy with preemption. If a lower-priority process is run-ning and a higher-priority process becomes available to run, it will preempt the lower-priority process. Upon entering the system, each periodic task is assigned a priority inversely based on its period. The shorter the period, the higher the priority; the longer the period, the lower the priority. The rationale behind this policy is to assign a higher priority to tasks that require theCPU more often. Furthermore, rate-monotonic scheduling assumes that the process-
ing time of a periodic process is the same for eachCPUburst. That is, every time a process acquires theCPU, the duration of itsCPUburst is the same.
Let’s consider an example. We have two processes, P1and P2. The periods for P1and P2are 50 and 100, respectively—that is, p1= 50 and p2= 100. The processing times are t1= 20 for P1and t2= 35 for P2. The deadline for each process requires that it complete itsCPUburst by the start of its next period.
We must f i rst ask ourselves whether it is possible to schedule these tasks so that each meets its deadlines. If we measure theCPUutilization of a process Pias the ratio of its burst to its period—ti∕pi—theCPUutilization of P1is 20∕50 = 0.40 and that of P2is 35∕100 = 0.35, for a totalCPUutilization of 75 percent. Therefore, it seems we can schedule these tasks in such a way that both meet their deadlines and still leave theCPUwith available cycles.
Suppose we assign P2a higher priority than P1. The execution of P1and P2 in this situation is shown in Figure 5.21. As we can see, P2starts execution f i rst and completes at time 35. At this point, P1starts; it completes itsCPUburst at time 55. However, the f i rst deadline for P1was at time 50, so the scheduler has caused P1to miss its deadline.
Now suppose we use rate-monotonic scheduling, in which we assign P1 a higher priority than P2because the period of P1is shorter than that of P2.
The execution of these processes in this situation is shown in Figure 5.22. P1 starts f i rst and completes itsCPUburst at time 20, thereby meeting its f i rst deadline. P2starts running at this point and runs until time 50. At this time, it is preempted by P1, although it still has 5 milliseconds remaining in itsCPUburst.
P1completes itsCPUburst at time 70, at which point the scheduler resumes P2.
P2completes itsCPUburst at time 75, also meeting its f i rst deadline. The system is idle until time 100, when P1is scheduled again.
Rate-monotonic scheduling is considered optimal in that if a set of pro-cesses cannot be scheduled by this algorithm, it cannot be scheduled by any other algorithm that assigns static priorities. Let’s next examine a set of pro-cesses that cannot be scheduled using the rate-monotonic algorithm.
Assume that process P1has a period of p1= 50 and aCPUburst of t1= 25.
For P2, the corresponding values are p2 = 80 and t2= 35. Rate-monotonic 0 scheduling would assign process P1a higher priority, as it has the shorter period. The totalCPUutilization of the two processes is (25∕50) + (35∕80) = 0.94, and it therefore seems logical that the two processes could be scheduled and still leave theCPUwith 6 percent available time. Figure 5.23 shows the scheduling of processes P1and P2. Initially, P1runs until it completes itsCPU burst at time 25. Process P2then begins running and runs until time 50, when it is preempted by P1. At this point, P2still has 10 milliseconds remaining in its CPUburst. Process P1runs until time 75; consequently, P2 f i nishes its burst at time 85, after the deadline for completion of its CPU burst at time 80.
Despite being optimal, then, rate-monotonic scheduling has a limitation:
CPUutilization is bounded, and it is not always possible to maximizeCPU resources fully. The worst-caseCPUutilization for scheduling N processes is N(21∕N− 1).
With one process in the system,CPUutilization is 100 percent, but it falls to approximately 69 percent as the number of processes approaches inf i nity. With two processes,CPUutilization is bounded at about 83 percent. CombinedCPU utilization for the two processes scheduled in Figure 5.21 and Figure 5.22 is 75 percent; therefore, the rate-monotonic scheduling algorithm is guaranteed to schedule them so that they can meet their deadlines. For the two processes scheduled in Figure 5.23, combinedCPUutilization is approximately 94 per-cent; therefore, rate-monotonic scheduling cannot guarantee that they can be scheduled so that they meet their deadlines.

38
Q

Earliest-Deadline-First Scheduling

A

Earliest-deadline-firs (EDF) scheduling assigns priorities dynamically accord-ing to deadline. The earlier the deadline, the higher the priority; the later the deadline, the lower the priority. Under the vEDF vpolicy, when a process becomes runnable, it must announce its deadline requirements to the system. Priorities may have to be adjusted to ref l ect the deadline of the newly runnable process.
Note how this differs from rate-monotonic scheduling, where priorities are f i xed.
To illustrate EDF scheduling, we again schedule the processes shown in Figure 5.23, which failed to meet deadline requirements under rate-monotonic scheduling. Recall that P1has values of p1= 50 and t1= 25 and that P2has values of p2= 80 and t2= 35. TheEDFscheduling of these processes is shown in Figure 5.24. Process P1has the earliest deadline, so its initial priority is higher than that of process P2. Process P2begins running at the end of theCPUburst for P1. However, whereas rate-monotonic scheduling allows P1 to preempt P2 at the beginning of its next period at time 50,EDFscheduling allows process P2to continue running. P2now has a higher priority than P1because its next deadline (at time 80) is earlier than that of P1(at time 100). Thus, both P1and P2meet their f i rst deadlines. Process P1again begins running at time 60 and completes its secondCPUburst at time 85, also meeting its second deadline at time 100. P2begins running at this point, only to be preempted by P1at the start of its next period at time 100. P2is preempted because P1has an earlier deadline (time 150) than P2(time 160). At time 125, P1completes itsCPUburst and P2resumes execution, f i nishing at time 145 and meeting its deadline as well. The system is idle until time 150, when P1is scheduled to run once again.
Unlike the rate-monotonic algorithm,EDFscheduling does not require that processes be periodic, nor must a process require a constant amount ofCPU time per burst. The only requirement is that a process announce its deadline to the scheduler when it becomes runnable. The appeal ofEDFscheduling is that it is theoretically optimal—theoretically, it can schedule processes so that each process can meet its deadline requirements andCPUutilization will be 100 percent. In practice, however, it is impossible to achieve this level ofCPU utilization due to the cost of context switching between processes and interrupt handling.

39
Q

Proportional Share Scheduling

A

Proportional share schedulers operate by allocating T shares among all appli-cations. An application can receive N shares of time, thus ensuring that the application will have N∕T of the total processor time. As an example, assume that a total of T = 100 shares is to be divided among three processes, A, B, and C. A is assigned 50 shares, B is assigned 15 shares, and C is assigned 20 shares.
This scheme ensures that A will have 50 percent of total processor time, B will have 15 percent, and C will have 20 percent.
Proportional share schedulers must work in conjunction with an admission-control policy to guarantee that an application receives its allocated shares of time. An admission-control policy will admit a client requesting a particular number of shares only if suff i cient shares are available. In our current example, we have allocated 50 + 15 + 20 = 85 shares of the total of 100 shares. If a new process D requested 30 shares, the admission controller would deny D entry into the system.

40
Q

POSIX Real-Time Scheduling

A

ThePOSIXstandard also provides extensions for real-time computing— POSIX.1b. Here, we cover some of thePOSIX APIrelated to scheduling real-time threads.POSIXdef i nes two scheduling classes for real-time threads:
•SCHED FIFO •SCHED RR SCHED FIFOschedules threads according to a f i rst-come, f i rst-served policy using aFIFOqueue as outlined in Section 5.3.1. However, there is no time slic-ing among threads of equal priority. Therefore, the highest-priority real-time thread at the front of theFIFOqueue will be granted theCPUuntil it termi-nates or blocks.SCHED RRuses a round-robin policy. It is similar toSCHED FIFO except that it provides time slicing among threads of equal priority.POSIX provides an additional scheduling class—SCHED OTHER—but its implemen-tation is undef i ned and system specif i c; it may behave differently on different systems.
ThePOSIX APIspecif i es the following two functions for getting and setting the scheduling policy:
•pthread attr getschedpolicy(pthread attr t *attr, int *policy) •pthread attr setschedpolicy(pthread attr t *attr, int policy) The f i rst parameter to both functions is a pointer to the set of attributes for the thread. The second parameter is either (1) a pointer to an integer that is set to the current scheduling policy (forpthread attr getsched policy()) or (2) an integer value (SCHED FIFO,SCHED RR, orSCHED OTHER) for the pthread attr setsched policy()function. Both functions return nonzero values if an error occurs.
In Figure 5.25, we illustrate aPOSIXPthread program using thisAPI. This program f i rst determines the current scheduling policy and then sets the scheduling algorithm toSCHED FIFO.