diff --git a/index.html b/index.html index f267190..da83b26 100644 --- a/index.html +++ b/index.html @@ -1290,16 +1290,21 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
  • 21.3. POSIX
  • 21.4. Userland multithreading
  • @@ -22495,6 +22500,9 @@ echo 1 > /proc/sys/vm/overcommit_memory

    It then walks over every page and writes a value in it to ensure that it is used.

    +

    A Fork bomb is another example that can trigger the OOM killer.

    +
    +

    Algorithm used by the OOM: https://unix.stackexchange.com/questions/153585/how-does-the-oom-killer-decide-which-process-to-kill-first

    @@ -22701,9 +22709,61 @@ echo 1 > /proc/sys/vm/overcommit_memory
    -

    21.3.2. pthreads

    +

    21.3.2. fork

    -

    POSIX' multithreading API. This was for a looong time the only "portable" multithreading alternative, until C++11 finally added threads, thus also extending the portability to Windows.

    +

    POSIX' multiprocess API. Contrast with pthreads which are for threads.

    +
    +
    +

    Example: userland/posix/fork.c

    +
    +
    +

    Sample native userland output on Ubuntu 19.04 at 762cd8d601b7db06aa289c0fca7b40696299a868 + 1:

    +
    +
    +
    +
    before fork before fork pid=13038 ppid=4805
    +after fork after fork pid=13038 ppid=4805
    +after (pid == 0) after (pid == 0) pid=13038 ppid=4805
    +after fork after fork pid=13039 ppid=13038
    +inside (pid == 0) inside (pid == 0) pid=13039 ppid=13038
    +after wait after wait pid=13038 ppid=4805
    +fork() return = 13039
    +
    +
    +
    +

    Read the source comments and understand everything that is going on!

    +
    +
    +
    21.3.2.1. Fork bomb
    + +
    +

    DANGER! Only run this on your host if you have saved all data you care about! Better run it inside an emulator! QEMU v4.0.0 user mode is not safe enough either because it is very native does not limit guest memory, so it will still blow up the host!

    +
    +
    +

    So without further ado, let’s rock:

    +
    +
    +
    +
    ./run --eval-after './posix/fork_bomb.out danger'
    +
    +
    + +
    +

    Outcome on LKMC 762cd8d601b7db06aa289c0fca7b40696299a868 + 1: after a few seconds of an unresponsive shell, we get a visit form the Linux out-of-memory killer, and the system is restored!

    +
    +
    +
    +
    +

    21.3.3. pthreads

    +
    +

    POSIX' multithreading API. Contrast with fork which is for processes.

    +
    +
    +

    This was for a looong time the only "portable" multithreading alternative, until C++11 finally added threads, thus also extending the portability to Windows.

    -

    21.3.3. sysconf

    +

    21.3.4. sysconf

    https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html

    @@ -22753,7 +22813,7 @@ echo 1 > /proc/sys/vm/overcommit_memory
    -

    21.3.4. mmap

    +

    21.3.5. mmap

    The mmap system call allows advanced memory operations.

    @@ -22764,7 +22824,7 @@ echo 1 > /proc/sys/vm/overcommit_memory

    Linux adds has several POSIX extension flags to it.

    -
    21.3.4.1. mmap MAP_ANONYMOUS
    +
    21.3.5.1. mmap MAP_ANONYMOUS

    Basic mmap example, do the same as userland/c/malloc.c, but with mmap.

    @@ -22782,7 +22842,7 @@ echo 1 > /proc/sys/vm/overcommit_memory
    -
    21.3.4.2. mmap file
    +
    21.3.5.2. mmap file

    Memory mapped file example: userland/posix/mmap_file.c

    @@ -22794,7 +22854,7 @@ echo 1 > /proc/sys/vm/overcommit_memory
    -
    21.3.4.3. brk
    +
    21.3.5.3. brk

    Previously POSIX, but was deprecated in favor of malloc

    @@ -22810,7 +22870,7 @@ echo 1 > /proc/sys/vm/overcommit_memory
    -

    21.3.5. socket

    +

    21.3.6. socket

    A bit like read and write, but from / to the Internet!