The Operating System Manages the Hardware
When the shell loaded and ran the hello program, and when the hello
program printed its message,
neither program accessed the keyboard, display, disk, or main memory
directly.
Rather, they relied on the services provided by the operating system.
We can think of the operating system as a layer of software
interposed between the application program and the hardware, as shown
in Figure 1.10.
The Operating System Manages the Hardware
All attempts by an application program to manipulate the hardware must
go through the operating system.
The Operating System Manages the Hardware
The operating system has two primary purposes:
(1) To protect the hardware from misuse by runaway applications.
(2) To provide applications with simple and uniform mechanisms for
manipulating complicated and often wildly different low-level hardware
devices.
The operating system achieves both goals via the fundamental abstractions
shown in Figure 1.11: processes, virtual memory, and files.
The Operating System Manages the Hardware
As this figure suggests, files are abstractions for I/O devices,
virtual memory is an abstraction for both the main memory and disk I/O
devices, and
Processes are abstractions for the processor, main memory, and I/O
devices.
The Operating System Manages the Hardware
Processes
A process is the operating system’s abstraction for a running program.
Multiple processes can run concurrently on the same system, and
each process appears to have exclusive use of the hardware.
By concurrently, we mean that the instructions of one process are
interleaved with the instructions of another process.
In most systems, there are more processes to run than there are CPUs to
run them.
The Operating System Manages the Hardware
Processes
Traditional systems could only execute one program at a time,
While newer multicore processors can execute several programs
simultaneously.
In either case, a single CPU can appear to execute multiple processes
concurrently by having the processor switch among them.
The operating system performs this interleaving with a mechanism known
as context switching.
The Operating System Manages the Hardware
Processes
The operating system keeps track of all the state information that the
process needs in order to run.
This state, which is known as the context,
includes information such as the current values of the PC, the register file,
and the contents of main memory.
At any point in time, a uniprocessor system can only execute the code for a
single process.
The Operating System Manages the Hardware
Processes
When the operating system decides to transfer control from the current
process to some new process,
it performs a context switch by saving the context of the current process,
restoring the context of the new process,
and then passing control to the new process.
The new process picks up exactly where it left off.
The Operating System Manages the Hardware
Processes
Figure 1.12 shows this basic idea.
As Figure indicates, the transition from one process to another is managed
by the operating system kernel.
The Operating System Manages the Hardware
Processes
The kernel is the portion of the operating system code that is always
resident in memory.
When an application program requires some action by the operating
system, such as to read or write a file,
It executes a special system call instruction, transferring control to the
kernel.
The kernel then performs the requested operation and returns back to the
application program.
The Operating System Manages the Hardware
Processes
Note that the kernel is not a separate process.
Instead, it is a collection of code and data structures that the system uses
to manage all the processes.
Implementing the process abstraction
requires close cooperation between both the low-level hardware and the
operating system software.
The Operating System Manages the Hardware
Threads
Although we normally think of a process as having a single control flow,
in modern systems a process can actually consist of multiple execution
units, called threads,
each running in the context of the process and sharing the same code and
global data.
Threads are an increasingly important programming model
because of the requirement for concurrency in network servers,
The Operating System Manages the Hardware
Threads
because it is easier to share data between multiple threads than between
multiple processes,
and because threads are typically more efficient than processes.
Multi-threading is also one way to make programs run faster when
multiple processors are available.
The Operating System Manages the Hardware
Virtual Memory
Virtual memory is an abstraction that provides each process with the
illusion that it has exclusive use of the main memory.
Each process has the same uniform view of memory,
Which is known as its virtual address space.
The virtual address space for Linux processes is shown in Figure 1.13.
(Other Unix systems use a similar layout.).
The Operating System Manages the Hardware
Virtual Memory
The Operating System Manages the Hardware
Virtual Memory
In Linux, the topmost region of the address space is reserved for code and
data in the operating system that is common to all processes.
The lower region of the address space holds the code and data defined by
the user’s process.
Note that addresses in the figure increase from the bottom to the top.
The virtual address space seen by each process consists of a number of
well defined areas, each with a specific purpose.
The Operating System Manages the Hardware
Program code and data
Code begins at the same fixed address for all processes,
followed by data locations that correspond to global C variables.
The code and data areas are initialized directly from the contents of an
executable object file.
Heap
The code and data areas are followed immediately by the run-time heap.
Unlike the code and data areas, which are fixed in size.
The Operating System Manages the Hardware
Heap
Once the process begins running,
The heap expands and contracts dynamically at run time,
as a result of calls to C standard library routines such as malloc and free.
Shared libraries
Near the middle of the address space is an area that holds the code and
data for shared libraries
such as the C standard library and the math library.
The Operating System Manages the Hardware
Stack.
At the top of the user’s virtual address space is the user stack
That the compiler uses to implement function calls.
Like the heap, the user stack expands and contracts dynamically during the
execution of the program.
In particular, each time we call a function, the stack grows.
Each time we return from a function, it contracts.
The Operating System Manages the Hardware
Kernel virtual memory.
The top region of the address space is reserved for the kernel.
Application programs are not allowed to read or write the contents of this
area
or to directly call functions defined in the kernel code.
Instead, they must invoke the kernel to perform these operations.
The Operating System Manages the Hardware
Files
A file is a sequence of bytes.
Every I/O device, including disks, keyboards, displays, and even networks,
is modeled as a file.
All input and output in the system is performed by reading and writing
files,
using a small set of system calls known as Unix I/O.
Systems Communicate with Other Systems Using Networks
Modern systems are often linked to other systems by networks.
From the point of view of an individual system,
The network can be viewed as just another I/O device, as shown in Figure
1.14.
When the system copies a sequence of bytes from main memory to the
network adapter,
The data flow across the network to another machine.
Similarly, the system can read data sent from other machines and copy
these data to its main memory.
Systems Communicate with Other Systems Using Networks