ENع
→The process
الدرس 06 · Processes and threads

The process: address space, registers and the PCB

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة10 خطوات5 أسئلةBeginner
عن هذا الدرس

A program on disk does nothing. A process is that program while it runs, and the kernel builds it from three parts: an address space (its own memory), the CPU's registers while it runs, and a record called the process control block (PCB). Watch the kernel turn three lines of C into process 42, pause it, run the same file again as process 43, and bring 42 back exactly where it stopped. The Linux lesson Processes and signals shows the commands such as ps and kill; this lesson shows what the kernel keeps for each process. User mode and kernel mode is a good warm-up.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 6 · تفاعلي
01 / 10

A program is just a file

A program is a file on disk: instructions and some starting data, doing nothing. Here it is three lines of C. A process is that program while it runs, and the kernel builds one from the file.

the whole program (C99):

#include <stdio.h>

int main(void) {
    int n = 0;
    n = n + 1;
    printf("%d\n", n);
    return 0;
}

program   a file on disk: lifeless, just bytes
process   the program while it runs (OSTEP ch. 4)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A program is just a file

    A program is a file on disk: instructions and some starting data, doing nothing. Here it is three lines of C. A process is that program while it runs, and the kernel builds one from the file.

    the whole program (C99):
    
    #include <stdio.h>
    
    int main(void) {
        int n = 0;
        n = n + 1;
        printf("%d\n", n);
        return 0;
    }
    
    program   a file on disk: lifeless, just bytes
    process   the program while it runs (OSTEP ch. 4)
  2. The kernel opens a record

    First the kernel gives the new process a number, its process ID (PID), here 42. It writes a record for it in its process table: the process control block, or PCB. The PCB will hold the state, saved registers and open files.

    PCB for PID 42 (xv6 calls it struct proc):
      pid        42
      state      new
      saved regs (filled in when it is paused)
      page table where its memory is
      open files 0 stdin, 1 stdout, 2 stderr
    
    a process can ask for its own PID (POSIX getpid):
    
    #define _POSIX_C_SOURCE 200809L
    #include <stdio.h>
    #include <unistd.h>
    
    int main(void) {
        printf("my pid is %ld\n", (long)getpid());
        return 0;
    }
  3. Its own address space

    Next the kernel builds the process's address space: the memory it may use. It copies the code in from the file and sets up a stack for local variables such as n. There is room for global data and a heap too.

    address space of PID 42:
      code    the instructions, copied from the file
      data    global and static variables
      heap    memory asked for while running (malloc)
      stack   local variables such as n
    
    on Linux, /proc/42/maps lists these regions,
    including [heap] and [stack]
  4. Registers: where it is

    Registers are tiny stores inside the CPU. The kernel loads them from the PCB: the program counter (PC) holds the next line, the stack pointer (SP) points into 42's stack, and a page-table register picks 42's memory. Now 42 runs.

    register   holds                     now
    PC         the next line to run      line 1
    SP         the top of the stack      42's stack
    PT         which address space       42's
    
    PT is satp on RISC-V and CR3 on x86-64
    PCB 42   state new -> running
  5. Lines 1 and 2 run

    The CPU runs line 1, so n is 0 on the stack, then line 2, so n becomes 1. After each line the PC moves on to the next. Everything the process knows is here: in its memory and its registers.

    line 1   int n = 0;     stack: n = 0    PC -> line 2
    line 2   n = n + 1;     stack: n = 1    PC -> line 3
    
    the kernel is not involved: plain work runs
    at full speed on the CPU
  6. Paused: registers saved

    The kernel pauses 42 before line 3 so something else can run. It copies the CPU's registers, PC = line 3 included, into 42's PCB and marks it ready. Its memory stays where it is, with n still 1.

    PCB 42   saved PC = line 3, SP, PT
             state running -> ready
    memory   untouched: n = 1
    
    how and when the kernel takes the CPU back is the
    lesson "The context switch and the timer interrupt"
  7. Same program, new process

    Run the same file again and the kernel makes a second process: PID 43, with its own PCB and its own address space. The code is copied in again. It is one program, but now two processes.

    PCB 43   new, saved PC = line 1
    memory   its own code, data, heap and stack
    
    PID 42   ready    n = 1
    PID 43   new      n not set yet
  8. 43 runs with its own n

    The kernel loads 43's registers: PC at line 1, SP and the page-table register on 43's memory. Line 1 sets 43's n to 0. 42's n is still 1: the same name, in a different address space.

    registers   PC line 1, SP 43, PT 43
    line 1      int n = 0;   43's stack: n = 0
    
    42's stack   n = 1   (not touched)
    43's stack   n = 0
  9. 42 picks up where it stopped

    The kernel saves 43's registers into PCB 43, then loads 42's saved registers back into the CPU. The PC says line 3, so 42 carries on exactly there, ready to print 1. It never noticed the pause.

    save   registers -> PCB 43   (43: ready, PC line 2)
    load   PCB 42 -> registers   (42: running, PC line 3)
    
    line 3   printf("%d\n", n);   prints 1
  10. What a process is

    A process is three things: an address space (its memory), its registers (where it is in the program), and its PCB, the kernel's record that keeps the registers safe while it waits. One program can run as many processes.

    the bookkeeping as a Python simulation:
    
    table = {}                          # the kernel's process table: pid -> PCB
    
    def create(pid):
        memory = {"n": None}            # its own address space
        table[pid] = {"state": "new", "pc": 1, "memory": memory}
    
    def run(pid, lines):                # load registers, run, save them again
        pcb = table[pid]
        pc, mem = pcb["pc"], pcb["memory"]
        pcb["state"] = "running"
        for _ in range(lines):
            if pc == 1: mem["n"] = 0
            if pc == 2: mem["n"] = mem["n"] + 1
            if pc == 3: print(mem["n"])
            pc += 1
        pcb["pc"], pcb["state"] = pc, "ready"
    
    create(42); run(42, 2)              # lines 1 and 2, then paused
    create(43); run(43, 1)              # same program, its own n
    run(42, 1)                          # 42 resumes at line 3
    print(table[42]["memory"], table[43]["memory"])
    
    prints:
    1
    {'n': 1} {'n': 0}

اختبار يتاح بعد الخطوة الأخيرة.