IPC & Threads Basics
  Process API Wrap-up
  
    - exit
 
    
      - terminate the current process, does not return!
 
      - clean up OS resources it's using
 
      
        - open files should be closed, virtual address space deallocated
 
        - what about its kernel stack? can a process free its own kernel stack?
 
      
    
    - wait
 
    
      - wait for a child process to exit
 
      
        - what happens if no child has exited when the parent has called wait?
 
        - what happens if a child already exited before the parent has called wait?
 
        - can the parent wait on the same child twice?
 
      
      - kernel needs to track the parent child relationship
 
      - parent responsible for cleaning up the rest of child's resources
 
      
        - what if parent exits without waiting on its children?
 
      
    
  
  IPC: Inter Process Communication
  
  - signals
 
  
    - a set of process events defined by the OS
 
    
      - e.g. SIGINT (ctrl-c), SIGPIPE (broken pipe), SIGKILL (kill signal), SIGSTOP (stop process)
 
      - see the signal man page for the full set of signals
    
 
    - sending and delivering signals are done through the OS
 
    
      kill(pid, signal_number) system call to send signal to a process 
      - some signals (e.g. SIGSEGV, SIGBUS) may also be sent as a result of exception
 
      - OS tracks a set of pending signals for each process and delivers them (when?)
 
      - OS defines a default action (terminate, stop, continue, core dump, or ignore) for each signal
 
      - process can define custom signal handlers for all signals except for SIGSTOP and SIGKILL
 
      - via the 
signal(signal_number, handler) system call 
    
    - upon a signal delivery, if a custom handler is defined, run the custom handler, else do the default action
 
    
      - where does the custom signal handler run? user or kernel mode? what happens after?
 
      - can any process send signals to any other process?
 
    
  
  
  - other forms of IPC
 
  
    - pipes: channel managed by the kernel to allow inter-process data transfer
 
    - files: can communicate via read/write, no notification support
 
    - shared memory: processes can explicitly create a shared memory region and communicate through it
 
    - need to worry about memory consistency on multicore systems
 
  
Threads
- unit of task/execution, the abstracted CPU
 
- can divide a program into independent tasks (threads)
 
  - open house example: greeter, presenter, server
  
  - webserver example: one thread per request
 
  - concurrency != parallelism
 
  
    - parallelism: multiple helpers at the open house, serve requests simultaneously
 
    - concurrency: even with 1 person/core, switch between roles/requests, take turn making progress on each role/request
 
  
 
- OS is a multithreaded program
 
  - processes can enter into the kernel and execute different system calls
 
  - system calls are concurrently accessed
 
  
    - two processes can both be in the middle of 
sys_open 
  
- process = address space + os resources + threads (execution states)
 
  - process has at least one thread (the main thread)
 
  - each thread has its own stack, pc, and registers
 
  - threads share code, data, and heap
 
  - cost of creating a thread vs process?
 
- managed and scheduled by the kernel
 
  - context switch: switch from one task to another task (from same or different process)
 
  - Thread Control Block (TCB)
 
  
    - metadata tracked by the kernel
 
    - tid, sp, registers, kernel stack, pointer to PCB
 
  
  - thread life cycle = process life cycle
 
- POSIX threads (pthreads) API
 
  - pthread_create, pthread_join, pthread_exit, pthread_detach