1. Home
  2. Explore
  3. Computer Science
  4. Process Scheduling

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Process Scheduling

Process Scheduling

Purpose of scheduling

Scheduling is the assignment of a resource to a requester at a particular time. In an operating system, process scheduling means deciding which process or thread should use the CPU next.

Many processes may be ready to run, but a system has a limited number of CPUs. The operating system must choose among those waiting processes and arrange access to the CPU. Its decisions affect how quickly work completes, how responsive applications feel, and whether processes receive fair opportunities to run.

A scheduler must balance several goals:

  • Keep the CPU and other resources usefully occupied.

  • Allow processes to make progress rather than wait indefinitely.

  • Provide acceptable response times to users.

  • Make performance sufficiently predictable for the system’s intended use.


CPU bursts, I/O bursts, and overlapping work

A program’s execution commonly alternates between periods of computation and periods of input/output:

CPU burst→I/O burst→CPU burst→I/O burst→⋯\text{CPU burst} \rightarrow \text{I/O burst} \rightarrow \text{CPU burst} \rightarrow \text{I/O burst} \rightarrow \cdotsCPU burst→I/O burst→CPU burst→I/O burst→⋯
  • A CPU burst is a period during which a process performs computation on the CPU.

  • An I/O burst is a period during which the process waits for an operation such as reading from a disk or communicating with a device.

Even a simple program can include both kinds of work: it may compute a value and then write it to a screen. In general, I/O operations are much slower than CPU operations, so a process can spend substantial time waiting for I/O.

When a process is waiting for an I/O operation, it usually cannot use the CPU. The I/O device may transfer data to or from memory, often with support from direct memory access (DMA). Without other work to perform, the CPU would sit idle during this time.

Why scheduling multiple processes improves utilization

Suppose Process 1 requests disk I/O. While the disk handles that request, Process 1 cannot make progress on the CPU. The operating system can schedule Process 2 to use the CPU during that interval. Later, Process 2 might request I/O, allowing Process 1 to run again if it is ready.

This overlapping of one process’s CPU work with another process’s I/O work allows the CPU and I/O devices to operate more effectively. Scheduling helps coordinate that overlap.

A process waiting for I/O does not need to hold the CPU. The operating system can run another ready process while the I/O operation proceeds.


CPU-bound and I/O-bound processes

Processes differ in how much computation and I/O they perform.

CPU-bound processes

A CPU-bound process spends a relatively large portion of its time computing and tends to have long CPU bursts. Matrix multiplication is one example: it can perform many arithmetic operations relative to the amount of data it must read or write.

I/O-bound processes

An I/O-bound process spends a relatively large portion of its time performing or waiting for I/O. It often has short CPU bursts separated by I/O bursts. A process that frequently reads data from a device or waits for network communication may be I/O-bound.

Why a mixture can be useful

When a CPU-bound process is running, an I/O-bound process may be waiting for a device. When the I/O-bound process becomes ready to use the CPU, the CPU-bound process may be waiting for its own I/O or may be scheduled later. A mixture of process types can therefore help keep both processors and I/O devices busy.


Multiprogramming

Degree of multiprogramming describes how many processes the system has admitted for concurrent progress. Those processes may be in different states, such as running, ready, or waiting for I/O; they do not all have to be executing on a CPU at the same instant.

With one CPU, at most one process can execute on that CPU at a time. With several CPUs, multiple processes can execute simultaneously, up to the number of available CPUs. Additional processes can still be resident in the system, waiting for a CPU or for an event such as I/O completion.

Multiprogramming makes it possible to switch to useful work when one process cannot currently proceed. Scheduling determines which ready process gets CPU time.


Scheduler and dispatcher

The operating system’s scheduling work involves two related components.

Scheduler

The scheduler examines the processes available for execution and selects which one should run next. In particular, a CPU scheduler selects a process from the ready processes.

Dispatcher

The dispatcher gives control of the CPU to the process selected by the scheduler. Its work includes switching from the current process to the selected one, which may require saving the current process’s state and restoring the next process’s state.

The time required to stop one process and start another is called dispatch latency. Context switching and the other steps required to transfer control contribute to this latency.


Scheduling types by time scale

Operating systems can make scheduling decisions at different time scales. The distinctions are conceptual; exact timing depends on the system and workload.

Long-term scheduling

A long-term scheduler decides which jobs are admitted into the system for execution. It is especially relevant to batch workloads, in which jobs may be submitted and run without continuous interaction from a user. Its decisions influence the overall mix of processes in the system.

Medium-term scheduling

A medium-term scheduler manages processes between the long-term and short-term levels. It may help control how many processes remain active in memory, for example by temporarily removing a process from active execution and later restoring it.

Short-term scheduling

A short-term scheduler selects a ready process to run on the CPU. It operates frequently, often on a scale of milliseconds or tens of milliseconds, and is important for interactive systems where users expect quick responses.

Long-term decisions generally happen less frequently than short-term CPU scheduling decisions. The scheduling algorithms discussed in operating systems can apply to different workloads and time scales, although their suitability depends on the system’s purpose.


Process states and scheduling decisions

A scheduler’s decisions are connected to transitions among process states:

  • Running to waiting: A running process requests a service, such as I/O, and must wait for it to complete.

  • Running to ready: A process stops running but is still able to execute. This can happen when the system preempts it or when an interrupt changes the scheduling situation.

  • Waiting to ready: The event or service for which a process was waiting completes, allowing it to compete for the CPU again.

  • Running to terminated: A process finishes, or an unrecoverable condition causes it to be stopped.

A process in the ready state is eligible to use the CPU but is not currently running. A process in the waiting state cannot run until the event it depends on occurs.


Non-preemptive and preemptive scheduling

Non-preemptive scheduling

In non-preemptive scheduling, a process that has been given the CPU is not forcibly removed from it while it is executing. It gives up the CPU voluntarily, for example by finishing its CPU burst, terminating, or requesting I/O.

This approach is also associated with cooperative scheduling: the process or an event initiated by the process allows the operating system to regain control. If a process keeps using the CPU and does not yield, other ready processes may have to wait.

Preemptive scheduling

In preemptive scheduling, the operating system can interrupt a running process and take the CPU away from it. This lets the system schedule another ready process even if the current process has not voluntarily yielded.

A common mechanism is a hardware timer interrupt. The operating system programs a timer; when the timer fires, control transfers to an interrupt service routine in the operating system. The operating system can then decide whether to continue running the current process or schedule another one.

Preemption can help prevent a process from monopolizing the CPU and can reduce indefinite delays for other processes. It also introduces coordination challenges, especially when processes share data or when an interrupt occurs while another interrupt is being handled.

Traps and interrupts

A trap is an exception or event caused by a program’s execution, such as a divide-by-zero operation or an invalid memory access. Examples of invalid accesses include writing to a read-only region or accessing a page that is not present. A trap transfers control to operating-system code, which can handle the condition and may terminate the process.

A timer interrupt, by contrast, is a hardware-generated mechanism that can give the operating system control so it can preempt a process. Traps and interrupts both transfer control to operating-system code, but they arise for different reasons.

Security considerations

Scheduling and context switching must preserve the boundary between operating-system privileges and user programs. The dispatcher performs the necessary state changes and switches to user mode before transferring control to the selected user program. Allowing a user program to execute with kernel-level privileges would create a security vulnerability.

Preemption also matters to security and reliability because it limits how long a process can retain the CPU without yielding. Non-preemptive execution can make it harder for the operating system to regain control if a process does not cooperate.


Goals and measures of scheduling

No single scheduling policy is best for every system. Common measures include CPU utilization, throughput, turnaround time, waiting time, and response time.

CPU utilization

CPU utilization is the proportion of time the CPU is busy doing useful work. Scheduling generally aims to keep utilization high, but high utilization alone is not enough: a system could keep one process busy while other processes wait excessively.

Throughput

Throughput is the number of processes completed per unit of time. A scheduling policy that completes more processes over a given period has higher throughput, assuming comparable measures of completed work.

Turnaround time

Turnaround time is the total time from a process’s arrival or submission until its completion:

Turnaround time=completion time−arrival time\text{Turnaround time} = \text{completion time} - \text{arrival time}Turnaround time=completion time−arrival time

It includes the process’s execution time and any time spent waiting or otherwise unable to complete.

Waiting time

Waiting time is the total time a process spends in the ready queue, waiting for CPU access. It does not mean time spent waiting for I/O.

For a simplified, single-CPU-burst, non-preemptive process, waiting time is the time from arrival until its first start:

Waiting time=start time−arrival time\text{Waiting time} = \text{start time} - \text{arrival time}Waiting time=start time−arrival time

For a process whose only relevant execution time is CPU time, total waiting time can be calculated as:

Waiting time=completion time−arrival time−CPU time\text{Waiting time} = \text{completion time} - \text{arrival time} - \text{CPU time}Waiting time=completion time−arrival time−CPU time

For processes with I/O bursts, the calculation must also distinguish time spent waiting for I/O from time spent waiting in the ready queue.

Response time

Response time measures how long it takes to produce the first response after a request is submitted. In interactive CPU scheduling, it is often measured as the time from arrival until the process first receives CPU service:

Response time=first start time−arrival time\text{Response time} = \text{first start time} - \text{arrival time}Response time=first start time−arrival time

Response time is not the same as turnaround time: a user may receive an initial response well before the full task is complete. Quick response is especially important in interactive and real-time systems.

Fairness and starvation

Starvation occurs when a process waits indefinitely or for an unreasonably long time because other processes repeatedly receive priority. A policy must consider whether every process can make progress, rather than focusing only on the speed of the currently favored process.

Predictability and variance

A scheduling policy should also consider the variance in performance. If a process sometimes receives a response almost immediately and at other times waits a very long time, users may not be able to rely on the system. Predictable behavior can be as important as good average performance, particularly when the system must meet timing expectations.


Choosing criteria for a system’s purpose

The most appropriate scheduling measure depends on the workload and the needs of the people using the system.

  • Interactive systems often place strong emphasis on response time, because users expect quick feedback after an action.

  • Batch systems often value throughput and turnaround time, because they process submitted jobs and users care about how many jobs finish and how long each takes.

  • Real-time systems place particular importance on meeting response-time or timing requirements.

  • General-purpose systems commonly seek high CPU utilization and throughput while keeping waiting time, turnaround time, response time, and starvation under control.

A scheduling algorithm can optimize different measures in different ways. An algorithm might provide excellent average performance while allowing poor worst-case performance, or it might place greater emphasis on limiting the worst-case delay. The system’s purpose determines which trade-offs are acceptable.


Key relationships

  • Programs alternate between CPU bursts and I/O bursts.

  • While one process waits for I/O, another ready process may use the CPU.

  • The scheduler selects the next process; the dispatcher transfers control to it.

  • Preemptive scheduling allows the operating system to interrupt a running process, commonly using a hardware timer interrupt.

  • Non-preemptive scheduling relies on a process yielding the CPU or otherwise reaching a point where it can no longer continue running.

  • Effective scheduling balances resource use and process progress with measures such as utilization, throughput, waiting time, turnaround time, response time, fairness, and predictability.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent