Page 430 - DCAP103_Principle of operating system
P. 430
Unit 14: Case Study of Linux Operating System
When a program starts up, its stack is not empty. Instead, it contains all the environment (shell) Notes
variables as well as the command line typed to the shell to invoke it. In this way a program can
discover its arguments. For example, when the command cp src dest is typed, the cp program
is run with the string ‘’cp src dest’’ on the stack, so it can find out the names of the source and
destination files. The string is represented as an array of pointers to the symbols in the string,
to make parsing easier. When two users are running the same program, such as the editor, it
would be possible, but inefficient, to keep two copies of the editor’s program text in memory at
once. Instead, most Linux systems support shared text segments. In Figure 14.12, we see two
processes, A and B, that have the same text segment. In Figure 14.12, we see a possible layout
of physical memory, in which both processes share the same piece of text. The mapping is done
by the virtual memory hardware. Data and stack segments are never shared except after a fork,
and then only those pages that are not modified. If either one needs to grow and there is no
room adjacent to it to grow into, there is no problem since adjacent virtual pages do not have
to map onto adjacent physical pages.
On some computers, the hardware supports separate address spaces for instructions and
data. When this feature is available, Linux can use it. For example, on a computer with 32-bit
addresses, if this feature is available, there would be 232 bits of address space for instructions
and an additional 232 bits of address space for the data and stack segments to share. A jump
to 0 goes to address 0 of text space, whereas a move from 0 uses address 0 in data space. This
feature doubles the address space available.
In addition of dynamically allocating more memory, processes in Linux can access file data
through memory-mapped files. This feature makes it possible to map a file onto a portion of a
process’ address space so the file can be read and written as if it were a byte array in memory.
Mapping a file in makes random access to it much easier than using I/O system calls such
as read and write. Shared libraries are accessed by mapping them in using this mechanism.
In Figure 14.13, we see a file that is mapped into two processes at the same time, at different
virtual addresses.
Figure 14.13: Two Processes can Share a Mapped File
Process A Physical memory Process B
Stack pointer Stack pointer
Mapped file
Mapped file
Unused
memory
24K
20K
BSS BSS
Data Data 8K
8K OS
Text Text
0K 0K
(a) (b) (c)
We can map in the same file at the same time. In fact, by mapping in a scratch file (which will
be discarded after all the processes exit), this mechanism provides a high bandwidth way for
multiple processes to share memory. In the most extreme case, two or more processes could map
in a file that covers the entire address space, giving a form of sharing that is partway between
separate processes and threads. Here the address space is shared (like threads), but each process
maintains its own open files and signals, for example, which is not like threads. In practice,
making two address spaces exactly correspond is never done, however.
LOVELY PROFESSIONAL UNIVERSITY 423