Processes

A process is an instance of a running program. A program is a file containing information that describes how to construct a process at runtime.

Programs consist of the following information:

  • binary format identification: metainformation describing the format of an executable file

    • most UNIX implementations use the ELF format

  • machine-language instructions

  • the program entry-point address

  • data (specifically, values for initializing variables and constants)

  • symbol and relocation tables: describe locations and names of functions and variables

  • shared-library and dynamic linking information

    • includes information about shared libraries required to run the program and the path of the needed dynamic linker

  • various other information

One program may construct many processes. In other words, multiple instances of the same program may be running at once.

Process IDs

A process ID (PID) is a positive integer that uniquely identifies a process. Every process has a process ID, which used and returned by a variety of system calls.

There is no fixed relationship between a program and the PID of the process created to run it. In other words, the PID assigned to a process has nothing to do with the specific program being run. There are a few exceptions to this, particularly that the init process always has PID 1.

In Linux, the getpid() system call can be used to fetch the PID of a process.

The Linux kernel limits PIDs to values less than or equal to 32,767. On 64-bit systems, this can be adjusted up to ~4 million. When a new process is created it is assigned the next sequentially available PID. If the 32,767 limit is reached, then the PID counter resets and starts using lower values again. During this reset, the counter resets to 300, not 1, since lower-number PIDs are usually in use by the system.

Parent Processes

Every process has a parent, which is the process the created it. If a parent process is terminated, the child process is then adopted by init.

The PID of a parent process can be obtained using the getppid() system call.

Memory Layout of a Process

Memory allocated to a process is composed of parts called segments. This section describes each segment type.

The text segment consists of the machine-language instructions of the program being run. It is read-only so the program doesn't overwrite itself. This segment is sharable, so multiple processes can map to a single copy of the program. This helps save memory as each process does not need to keep its own copy of the instructions.

The initialized data segment contains global and static variables that are explicitly initialized. These are read from the executable when the program is loaded into memory.

The uninitialized data segment contains global and static variables that are not explicitly initialized. All memory in this segment is initialized to 0.

The stack holds stack frames. A stack frame stored a function's variables, arguments, and return value. One frame is allocated for each currently-called function. The stack dynamically grows and shrinks as needed.

The heap is an area in which memory can be dynamically allocated at runtime.

Virtual Memory Management

The virtual memory scheme splits memory used by a process into pages. A page is a small, fixed-size, contiguous unit of virtual memory. On x86-32, pages are 4096 bytes.

RAM is divided into a series of page frames of the same size. A page frame is the smallest fixed-length block of physical memory into which memory pages are mapped.

For most processes, only some pages need to be present in physical memory at a time. The pages in physical memory form a resident set. Copies of the unused pages are stored in swap space and loaded into memory as needed. A page fault occurs when a process attempts to reference a page not currently stored in physical memory.

The kernel maintains a page table for each process. A page table describes the location of each page in a processes's virtual address space. It's a set of all virtual memory pages available to a process.

Last updated