Introduction to Multithreading - Chad Salinas ::: Data Scientist
Life and times of Chad Salinas
Chad Salinas, golf caddy, data scientist, chad rPubs, recovering chess addict, daddy caddy
950
post-template-default,single,single-post,postid-950,single-format-standard,qode-listing-1.0.1,qode-social-login-1.0,qode-news-1.0,qode-quick-links-1.0,qode-restaurant-1.0,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,qode-theme-ver-12.0.1,qode-theme-bridge,bridge,wpb-js-composer js-comp-ver-5.4.2,vc_responsive

Introduction to Multithreading

Multithreading

Attribution:  I thank Jerry Cain at Stanford for inspiring the following content

Virtual Memory
Translation Lookaside Buffer (TLB)
Text Segment
  • doesn’t change  in size
Global Data Segment
  • doesn’t change  in size
Segments chunked up into pages.  Need to use the last 10 bits to determine the range of addresses covered by the single page.
Handle Page Faults when no physical memory is backing the virtual address hash table.
Addresses are laid out contiguously in virtual memory not physical memory.
Multi-Processing
Need a data structure to hold meta-data about the running process in a single cpu environment.
OS has to decide which processes are capable of getting work done and then round-robin through each of the processes giving each a time-slice to make progress.
Context Switch – think about what info we need to take a snapshot of so that one process can be swapped off the CPU to allow another process a time-slice to make progress.
Caller-Saved Registers
Callee-Saved Registers
Take a memcpy of the whole of the register space as a snapshot to re-instantiate a process at its next time-slice.
Freeze-dried state gets memcpy’ied back onto the CPU when process gets the next time-slice from the CPU.  The only thing that may have changes is we might have some misses in the L1/L2 cache if we haven’t been running for a while — the cost of multi-processing.
3 Types of Process Control Blocks (PCB’s)
  1. Running – bit of meta data like PID, rest of state is already on CPU
  2. Ready – think of a linked list of Ready Process state in PCB’s (Ready Queue)
  3. Blocked – sleeping into OS is signaled that these processes can now run
Only a small number of processes can really get any work done at any given time!
Multi-Threading
Think about downloading multiple songs in iTunes.  Process needs to sub-divide its own time-slice into mini time-slices and then rotate through each mini-processes (threads).
Slow System Calls:  waiting to read input, open a network connection where file descriptor abstraction deals with the sink and source of bytes from a remote source, a sleep timer…
Thread schedules to be informed when progress can be made.
Need a pthread_t data structure to keep track of all of the threads.  pthread_t holds the analog to the PID which is a thread id.
pthread_create()
pthread_join()

No Comments

Sorry, the comment form is closed at this time.