Please enable JavaScript.
Coggle requires JavaScript to display documents.
12.1, 12.3 - Coggle Diagram
12.1, 12.3
12.3 Concurrent Programming with Threads
.
Thread
is a logical flow that runs in the context of a process.
. Threads are scheduled automatically by the kernel.
.
Each thread has its own thread context, TID, stackpointer, program counter, general purpose registers and condition codes. They all share the same virtual address space.
12.3.1 Thread execution Model
Each process starts as single, main, thread.
Main thread creates peer thread
They run concurrently until control passes to peer thread by context switch
This happens main thread executes a slow system call like read or sleep or it gets interrupted by the system interval timer
The context switch is faster because the context is smaller
It doesn't have the rigid parent-child hierarchy
Gets organized into pool or peers which can kill and talk to eachother
12.3.2 Posix Threads
(Pthreads) is an interface for manipulating threads from C programs. Allows to create, kill, reap threads and to share and notify between threads
12.3.3 Creating Threads
Created with pthread_create()
one argument gets the thread Id
A thread can find its id by calling pthread_self
12.3.4 Terminating Threads
It can terminate implicitly when top-level thread routine returns
It can terminate explicitly by calling pthread_exit. If main calls this it waits for all the other peer threads to terminate and then terminates the main thread and the entire process with a return value of thread_return
if a thread calls exit, it terminates the process and all threads associated with the process
a peer thread can terminate current thread by calling pthread_cancel with ID of current thread
12.3.5 Reaping Terminated Threads
pthread_join blocks untill tid terminates. It does something witha pointer then reaps the resources.
unlike wait, it cant just finish any process that happens. It has to specify the tid of the thread. This is a bug in the design.
12.3.6 Detaching Threads
a joinable thread can be reaped or killed by other threads and the memory resources are not freed until another thread reaps it.
Threads are joinable by default. You can make it detachable by pthread_detach
detached threads cannot be killed or reaped by other threads. Memory is freed automatically by the system when it terminates
Detaching is good
12.3.7 Initializing Threads
pthread_once() allows you to initialize the state associated with a thread routine.
Something about global variables shared by multiple threads...
Concurrent Programming with processes
12.1.1 A concurrent Server Based on Processes
remember that SIGCHLD signals are blocked while the SIGCHLD handler is executing.
remember to close file descriptors to not create a memory leak. if not the connection might not be terminated.
12.1.2 Pros and Cons of Processes
Spawning a process is nice because the separate memory wont be overwritten.
But makes it harder to share information between them. They must use
explicit IPC
12.2 Concurrent Programming with I/O Multiplexing
Sometimes a server might wait for input from the command line console and from a client. It can only do one or the other at a time. This is solved with
I/O Multiplexing
it uses select function to ask the kernetl to suspend the process returning control to the application only after one or more I/O events have occurred.
I have no idea how select works
12.2.1 Concurrent Event-Driven Server Based on I/O Multiplexing
Pros and Cons of I/O Multiplexing