races: outcome depends on arbitrary scheduling decisions deadlock: waiting on each other livelock: going around but with no actual progress starvation/fairness: overall system makes progress but some individuals wait indefinitely
connect
, rio_writen
returns while rio_readlineb
blocks
process-based: kernel interleaves multiple logical flows, with private address space
idea: spawn children to do the work
pros:
fd
correctly, reap children.cons:
event-based: programmer manually interleave logical flows, same address space, I/O multiplexing
idea: keep a set of active connections and use select
function to select pending file descriptors connfd
or listenfd
to generate events.
pros:
cons:
thread-based: kernel interleaves multiple logical flows, share address space. Hybrid of process-based and event-based.
idea:
pros:
cons:
different from signal: signal handlers share everything beside stack, interrupts normal program execution, never parallel, Limited forms of synchronization (block, unblock, wait for signal)
Pthreads
LibraryPthreads: Standard interface for ~60 functions that manipulate threads from C programs
Creating and reaping threads
Determining your thread ID
Synchronizing access to shared variables
Joinable thread: must be reaped with pthread_join()
and killed by other threads
Detached thread: cannot be reaped or killed
resource automatically reaped on termination
use pthread_detach(pthread_self())
to make detached
must not call join
before thread switch to detached
Producer-Consumer Model: a model for pass in argument for sub thread
allocate memory in main thread
free memory in sub thread
Table of Content