Processes, Scheduling, Address Spaces

Problem: Owning the CPU?

The following program is not sane. Why?

int main(void)
{
    while (1);
    return 0;
}

Solution: Scheduling (Time Slices)

  • Owning the CPU?

    ⟶ on an OS, nobody but the kernel (the OS) can own the CPU

  • Scheduling

  • Time slicing

  • Even on a single CPU, many can be given the illusion of CPU ownership

  • ⟶ the more infinite loops, the slower that illusion becomes, obviously

  • ⟶ best to not spin until an event happens, but rather block on that event (see Blocking I/O: What Is That?)

../../../../../../_images/timeslicing.svg

Problem: Owning all Memory?

The following program is also not sane. Why?

#include <stdio.h>

int main(void)
{
    int ten_integers[10];
    int i = 0;
    while (1) {
        printf("%d\n", i);
        ten_integers[i] = i;
        i++;
    }

    (void)ten_integers; // prevent gcc "unused variable" warning
    return 0;
}

Solution: Virtual Memory

  • Owning all Memory?

    ⟶ on an OS nobody but the kernel can own all memory

  • User space has an abstraction for that: process

  • Every process has memory allocated to it

    • Kept track of by the kernel

    • Memory indirection, implemented in hardware: MMU

    • Memory protection: read/write/execute

    ../../../../../../_images/address-space.svg
  • Multiple address spaces

    • CPU is switched between processes ⟶ scheduling

    • Context switch

    • Address space changed at context switch

    ../../../../../../_images/virtual-memory.svg

Process Attributes: User ID (UID)

  • Processes run under a user ID - “as a user”

    $ id -u
    1000
    

    Defined in /etc/passwd (but not necessarily - can come from Kerberos/Active Directory)

    $ grep jfasch /etc/passwd
    jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
    
  • User ID 0 is special

    • root - “The Administrator”

    • Has permissions to do everything that a normal user cannot

    • Can format disk, can view/delete all your files, can be unfair if she wants

Process Attributes: Groups

  • Processes run under a group ID - the primary group

  • Processes have supplementary groups (an addition/extension made at some point in history)

$ id
uid=1000(jfasch) gid=1000(jfasch) groups=1000(jfasch),10(wheel),18(dialout)
  • Primary group defined in /etc/passwd

    $ grep jfasch /etc/passwd
    jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash
    
  • Supplementary groups defined in /etc/group

    $ grep jfasch /etc/group
    wheel:x:10:jfasch
    dialout:x:18:jfasch
    jfasch:x:1000:
    

Processes: More Attributes

  • Current working directory (cwd)

    For example, the shell (which is a program like any other program) changes its CWD with the cd command:

    $ pwd       # "print working directory"
    /home/jfasch
    $ cd /etc
    $ pwd
    /etc
    
  • Resource limits

    • Maximum stack usage (stacks grow dynamically)

    • File size

    $ ulimit -a
    real-time non-blocking time  (microseconds, -R) unlimited
    core file size              (blocks, -c) unlimited
    data seg size               (kbytes, -d) unlimited
    scheduling priority                 (-e) 0
    file size                   (blocks, -f) unlimited
    pending signals                     (-i) 62715
    max locked memory           (kbytes, -l) 8192
    max memory size             (kbytes, -m) unlimited
    open files                          (-n) 1024
    pipe size                (512 bytes, -p) 8
    POSIX message queues         (bytes, -q) 819200
    real-time priority                  (-r) 0
    stack size                  (kbytes, -s) 8192
    cpu time                   (seconds, -t) unlimited
    max user processes                  (-u) 62715
    virtual memory              (kbytes, -v) unlimited
    file locks                          (-x) unlimited
    
  • Open files