|
Page 1 of 4 Experiments with the Linux Kernel: Process Segments
Traditionally, a Unix process is divided into segments. The
standard segments are code segment, data segment, BSS (block
started by symbol), and stack segment.
The code segment
contains the binary code of the program which is running as the
process (a "process" is a program in execution). The data segment
contains the initialized global variables and data structures. The
BSS segment contains the uninitialized global data structures and
finally, the stack segment contains the local variables, return
addresses, etc. for the particular process.
Under Linux, a
process can execute in two modes - user mode and kernel mode. A
process usually executes in user mode, but can switch to kernel
mode by making system calls. When a process makes a system call,
the kernel takes control and does the requested service on behalf
of the process. The process is said to be running in kernel mode
during this time. When a process is running in user mode, it is
said to be "in userland" and when it is running in kernel mode it
is said to be "in kernel space". We will first have a look at how
the process segments are dealt with in userland and then take a
look at the bookkeeping on process segments done in kernel
space.
2. Userland's view
of the segments
The code segment
consists of the code - the actual executable program. The code of
all the functions we write in the program resides in this segment.
The addresses of the functions will give us an idea where the code
segment is. If we have a function foo() and let x be the address of
foo (x = &foo;). we know that x will point within the code
segment. The Data segment consists of the initialized global
variables of a program. The Operating system needs to know what
values are used to initialize the global variables. The initialized
variables are kept in the data segment. To get the address of the
data segment we declare a global variable and then print out its
address. This address must be inside the data segment.
The BSS consists of
the uninitialized global variables of a process. To get an address
which occurs inside the BSS, we declare an uninitialized global
variable, then print its address. The automatic variables (or local
variables) will be allocated on the stack, so printing out the
addresses of local variables will provide us with the addresses
within the stack segment.
|