Process Management[1] – Process Overview

We all know about Linux Architecture Overview in my previous post. To go deep into Linux Kernel roles, I will start with Process Management, then the remain topics one by one.

1. What are processes?

Processes are active programs and related resources. They include:

  • The executing program code: called the text section in Unix
  • A set of resources:
    • open files and pending signals
    • internal kernel data
    • processor state
    • a memory address space with one or more memory mappings
  • data section containing global variables
  • threads of execution: often shortened to threads, are the objects of activity within the process. It includes:
    • unique program counter
    • processor stack
    • set of processor registers

In traditional Unix systems, each process consists of one thread. However, in modern systems, the thread number consisted in each process can be more than one.

One more improved thing on modern operating systems, processes provide two virtualization:

  • Virtual processor: gives the processor the illusion that it alone monopolizes the system.
  • Virtual memory: lets the process allocate and manage memory as if it alone owned all the memory in the system

I will show you more detail about threads, virtual processor, virtual memory in my other posts, because they are also the fundamental concepts in Linux Programming.

2. Process Life Cycle

Occasionally, process has to wait for events from external sources beyond its control, and it cannot run before the event occurs.

Then the scheduler must know the state of every process in the system when switching:

  • Running: the process is executing at the moment.
  • Waiting: the process is able to run but is now allowed to because the CPU is allocated to another process.
  • Sleeping: the process is sleeping and cannot run because it is waiting for external event.

Lets check the transitions with a queued runable process:

  • Path (1): the process has to wait for an events, its state change from “running” to “waiting”
  • Path (3): the waited event occur, then process back to “waiting” state and rejoin normal cycle
  • Path (4): the scheduler grants it CPU time, then process state change from “waiting” to “running”
  • Path (2); the scheduler decides to withdraw CPU resources from the process, its state change from “running” to “waiting”
  • Path (5): the program execution terminates, the process state change from “running” to “stopped”

The system saves all process in a process table – regardless of whether they are running, sleeping or waiting. Then we have a special process state not listed above is the “zombie” state, when processes are dead because their resource have already been released but there are still entries for them in the process table.

3. Process Memory Layout

The memory allocated to each process is composed of a number of parts, usually referred to as segments. These segments are as follows:

  • Text: contains the machine language instructions of the program run by the process. The text segment has 2 important property:
    • Read-only: that the process doesn’t accident modify the structure in it
    • Shareable: a single copy of the program code can be mapped into the virtual address space of all processes.
  • Initialized data: contains global and static variable that are explicitly initialized. Their value are read from the executable file when the program loaded into memory
  • Uninitialized data: (also called bss segment) contains global and static variables that are not explicitly initialized. Before stating the program, the system initializes all memory in this segment to 0.
  • Stack: dynamically growing and shrinking segment containing stack frames. One stack frame is allocated for each currently called function. A frame stores:
    • Function’s local variable (automatic variables)
    • Arguments
    • Return value
  • Heap: memory area to store dynamically allocated variable at run time.

4. What should we do next?

Now we have an overview about processes in Linux. In next post, we will investigate how to create and terminate a process, also do some example with Beagle Bone Black to understand the theory more clearly.

5. Reference

I referred a list of book for this post:

  • Professional Linux Kernel Architecture  Wolfgang Mauerer
  • The Linux Programming Interface Micheal Kerrisk
  • Advanced Programming in the UNIX Environment – W. Richard Stevens Stephen A. Rago

These are very amazing books about Linux kernel to help us go deep into Linux Programming worlds.

Leave a Reply

Your email address will not be published. Required fields are marked *