![](https://csdnimg.cn/release/download_crawler_static/10381642/bg11.jpg)
CHAPTER 1 OPERATING SYSTEM FUNDAMENTALS
11
• The thread can no longer execute because it is waiting for the completion of an
operation, such as for data to be read from disk, or for the result of another thread.
In this case, the scheduler allows another thread to run on the CPU while the
original thread is blocked. This prevents the CPU from sitting idle when a thread
has no work to do and maximizes the time that the CPU is spent executing code. A
thread can also voluntarily give up its time on the CPU by calling one of the
sleep() functions, which delay execution of the current thread for a specified
duration.
One reason for adding multiple threads to an application is to allow it to execute concurrently
across multiple CPU cores so that the application’s execution can be sped up by dividing a complex
operation into smaller steps that are run in parallel. However, multithreading has advantages even on a
computer with a single CPU core. By rapidly switching between active threads, the scheduler gives the
illusion that all threads are running concurrently. This allows a thread to block or sit in a tight loop with
negligible impact on the responsiveness of other threads, so a time-consuming task can be moved to a
background thread while leaving the rest of the application free to respond to user interaction.
A common design used in applications that interface with hardware is to place the code that
accesses the hardware in its own thread. Software code often has to block while it is waiting for the
hardware to respond; by removing this code from the main program thread, the program’s user interface
is not affected when the program needs to wait for the hardware.
Another common use of threads occurs when software needs to respond to an event from hardware
with minimal delay. The application can create a thread that is blocked until it receives notification from
hardware, which can be signaled using techniques discussed in later chapters. While the thread is
blocked, the scheduler does not need to provide it with access to the CPU, so the presence of the thread
has no impact on the performance of the system. However, once the hardware has signaled an event, the
thread becomes unblocked, is scheduled on the CPU, and it is free to take whatever action is necessary
to respond to the hardware.
Hardware and Drivers
In addition to managing essential hardware resources such as the CPU and memory, the operating
system is also responsible for managing hardware peripherals that may be added to the system. This
includes devices such as the keyboard and mouse, a USB flash drive, and the graphics card. Although the
operating system is responsible for managing these devices, it does so with the help of drivers, which can
be thought of as plug-ins that run inside the operating system kernel and allow the system to interface to
hardware devices.
The code to support a hardware device can be found in two places: on the device itself (known as
firmware) and on the computer (known as the driver). The role of the driver is to act on behalf of the
operating system in controlling the hardware device. Driver code is loaded into the operating system
kernel and is granted the same privileges as the rest of the kernel, including the ability to directly access
hardware.
The driver has the responsibility of initializing the hardware when the device is plugged into the
computer (or when the computer boots) and of translating requests from the operating system into a
sequence of hardware-specific operations that the device needs to perform to complete the operating
system’s request.
The type of requests that a driver will receive from the operating system depends on what function
the driver performs. For certain drivers, the operating system provides a framework for driver
developers. For example, a sound card requires an audio driver to be written. The audio driver receives
requests from the operating system that are specific to the world of audio, such as a request to create a
48 kHz audio output stream, followed by requests to output a provided packet of audio.
www.allitebooks.com