Modern Operating Systems by Herbert Bos ...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf-M ODERN O PERATING S YSTEMS
Showing 788-789 out of 1137
Modern Operating Systems by Herbert Bos and Andrew...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf-M ODERN O PERATING S YSTEMS
Modern Operating Systems by Herbert...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf-M ODERN O PERATING S YSTEMS
Page 788
SEC. 10.4
MEMORY MANAGEMENT IN LINUX
757
Text
BSS
Data
Text
Stack pointer
Stack pointer
20K
8K
0K
24K
0K
(a)
(b)
(c)
OS
Physical memory
Mapped file
Mapped file
Process A
Process B
BSS
Data
8K
Unused
memory
Figure 10-13.
Two processes can share a mapped file.
How
malloc
is implemented is thus moved outside the scope of the POSIX stan-
dard. In some circles, this approach is known as passing the buck.
In practice, most Linux systems have system calls for managing memory. The
most common ones are listed in Fig. 10-14.
Brk
specifies the size of the data seg-
ment by giving the address of the first byte beyond it.
If the new value is greater
than the old one, the data segment becomes larger; otherwise it shrinks.
System call
Description
s = brk(addr)
Change data segment size
a = mmap(addr, len, prot, flags, fd, offset)
Map a file in
s = unmap(addr, len)
Unmap a file
Figure 10-14.
Some system calls relating to memory management. The return
code
s
is
1 if an error has occurred;
a
and
addr
are memory addresses,
len
is a
length,
prot
controls protection,
flags
are miscellaneous bits,
fd
is a file descrip-
tor, and
offset
is a file offset.
The
mmap
and
munmap
system calls control memory-mapped files. The first
parameter to
mmap
,
addr
, determines the address at which the file (or portion
thereof) is mapped.
It must be a multiple of the page size.
If this parameter is 0,
the system determines the address itself and returns it in
a
.
The second parameter,
len
, tells how many bytes to map.
It, too, must be a multiple of the page size.
The
third parameter,
prot
, determines the protection for the mapped file. It can be
marked readable, writable, executable, or some combination of these.
The fourth
parameter,
flags
, controls whether the file is private or sharable, and whether
addr
is a requirement or merely a hint. The fifth parameter,
fd
, is the file descriptor for


Page 789
758
CASE STUDY 1: UNIX, LINUX, AND ANDROID
CHAP. 10
the file to be mapped. Only open files can be mapped, so to map a file in, it must
first be opened.
Finally,
offset
tells where in the file to begin the mapping.
It is not
necessary to start the mapping at byte 0; any page boundary will do.
The other call,
unmap
, removes a mapped file. If only a portion of the file is
unmapped, the rest remains mapped.
10.4.3 Implementation of Memory Management in Linux
Each Linux process on a 32-bit machine typically gets 3 GB of virtual address
space for itself, with the remaining 1 GB reserved for its page tables and other ker-
nel data. The kernel’s 1 GB is not visible when running in user mode, but becomes
accessible when the process traps into the kernel. The kernel memory typically
resides in low physical memory but it is mapped in the top 1 GB of each process
virtual address space, between addresses 0xC0000000 and 0xFFFFFFFF (3–4 GB).
On current 64-bit x86 machines, only up to 48 bits are used for addressing, imply-
ing a theoretical limit of 256 TB for the size of the addressable memory.
Linux
splits this memory between the kernel and user space, resulting in a maximum 128
TB per-process virtual address space per process.
The address space is created
when the process is created and is overwritten on an
exec
system call.
In order to allow multiple processes to share the underlying physical memory,
Linux monitors the use of the physical memory, allocates more memory as needed
by user processes or kernel components, dynamically maps portions of the physical
memory into the address space of different processes, and dynamically brings in
and out of memory program executables, files, and other state information as nec-
essary to utilize the platform resources efficiently and to ensure execution progress.
The remainder of this section describes the implementation of various mechanisms
in the Linux kernel which are responsible for these operations.
Physical Memory Management
Due to idiosyncratic hardware limitations on many systems, not all physical
memory can be treated identically, especially with respect to I/O and virtual mem-
ory.
Linux distinguishes between the following memory zones:
1.
ZONE
DMA
and
ZONE
DMA32
: pages that can be used for DMA.
2.
ZONE
NORMAL
: normal, regularly mapped pages.
3.
ZONE
HIGHMEM
: pages with high-memory addresses, which are
not permanently mapped.
The exact boundaries and layout of the memory zones is architecture dependent.
On x86 hardware, certain devices can perform DMA operations only in the first 16
MB of address space, hence
ZONE
DMA
is in the range 0–16 MB.
On 64-bit ma-
chines there is additional support for those devices that can perform 32-bit DMA


Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
Concordia University
Concordia_University
School:
Operating_Systems
Course:
Great resource for chem class. Had all the past labs and assignments
Leland P.
Santa Clara University
Introducing Study Plan
Using AI Tools to Help you understand and remember your course concepts better and faster than any other resource.
Find the best videos to learn every concept in that course from Youtube and Tiktok without searching.
Save All Relavent Videos & Materials and access anytime and anywhere
Prepare Smart and Guarantee better grades

Students also viewed documents