Class 4 CS 202 06 February 2020 On the board ------------ 1. Last time 2. Pipes, simple C programs 3. Processes: the OS's view 4. Threads 5. Intro to concurrency --------------------------------------------------------------------------- 1. Last time - Process/OS control transfers - Git/lab setup - Process birth - Shell - file descriptors - fork/exec() separation the question from last time: 2^{10} 2. Pipes, simple C programs - handout from last time - look at the code for 'head' and 'yes': an example of file descriptors in action - now, how can we arrange for 'yes' to deliver its output to 'head', and for 'head' to take its input from 'yes'? 3. Implementation of processes Briefly cover the OS's view: PCB ----------------- | process id | | state | (ready, runnable, blocked, etc.) | user id | | IP | | open file | | VM structures | | registers | | ..... | (signal mask, terminal, priority, ...) ---------------- called "proc" in Unix, "task_struct" in Linux, and "process_t" in lab4. draw an array of these. point out that during scheduling, a mechanism that we have not seen, a core switches between processes. will discuss the mechanism for this later. Note: these PCBs will have an analog when considering threads, below. 4. Threads Interface to threads: tid thread_create (void (*fn) (void *), void *); Create a new thread, run fn with arg void thread_exit (); void thread_join (tid thread); Wait for thread 'thread' to exit plus a lot of synchronization primitives, which we'll see in the coming classes Assume for now that threads are: --an abstraction created by OS --preemptively scheduled [draw abstract picture of threads: own registers, share memory] (later, we will explore alternatives) 5. Intro to concurrency There are many sources of concurrency. --what is concurrency? --stuff happening at the same time --sources of concurrency --computers have multiple CPUs and common memory, so instructions in multiple threads can happen at the same time! --on a single CPU, processes/threads can have their instructions interleaved (helpful to regard the instructions in multiple threads as "happening at the same time") --interrupts (CPU was doing one thing; now it's doing another) --why is concurrency hard? *** Hard to reason about all possible interleavings --handout: 1a: x = 1 or x = 2. 1b: x = 13 or x = 25. 1c: x = 1 or x = 2 or x = 3 2: incorrect list structure 3: incorrect count in buffer all of these are called race conditions; not all of them are errors, though --worst part of errors from race conditions is that a program may work fine most of the time but only occasionally show problems. why? (because the instructions of the various threads or processes or whatevever get interleaved in a non-deterministic order.) --and it's worse than that because inserting debugging code may change the timing so that the bug doesn't show up --hardware makes the problem even harder [look at panel 4; what is the correct answer?] [answer: "it depends on the hardware"] --sequential consistency not always in effect --sequential consistency means: --maintain program order on individual processors --ensuring that writes happen to each memory location (viewed separately) in the order that they are issued --assume sequential consistency until we explicitly relax it