cps 310 threads and concurrency
play

CPS 310 Threads and Concurrency Jeff Chase Duke - PowerPoint PPT Presentation

D D u k e S y s t t e m s CPS 310 Threads and Concurrency Jeff Chase Duke University h<p://www.cs.duke.edu/~chase/cps310 Threads I draw my threads like this. A thread is a stream of


  1. D D u k e S y s t t e m s CPS ¡310 ¡ Threads ¡and ¡Concurrency ¡ Jeff ¡Chase ¡ Duke ¡University ¡ ¡ h<p://www.cs.duke.edu/~chase/cps310 ¡

  2. Threads I draw my threads like this. • A thread is a stream of control … . – Executes a sequence of instructions. – Thread identity is defined by CPU register context (PC, SP, … , page table base registers, … ) – Generally: a thread’s context is its register values and referenced memory state (stacks, page tables). • Multiple threads can execute independently: – They can run in parallel on multiple cores... • physical concurrency – … or arbitrarily interleaved on some single core. • logical concurrency Some people draw threads as • A thread is also an OS abstraction to spawn and squiggly lines. manage a stream of control. 310

  3. Portrait of a thread In an implementation, each thread is represented by a data struct. We call it a “ thread object ” or “ Thread Control Block ”. It stores information about the thread, and may be linked into other system data structures. “Heuristic Thread Control fencepost”: try to Block (“TCB”) detect stack overflow errors 0xdeadbeef Storage for context name/status etc (register values) Stack ucontext_t when thread is not running. Each thread also has a runtime stack for its own use. As a running thread calls procedures in the code, frames are pushed on its stack.

  4. Processes and their threads main thread virtual address space other threads (optional) + + … stack Each process has a Each process has a main virtual address space On real systems, a process thread bound to the VAS, (VAS): a private name can have multiple threads. with a stack. space for the virtual memory it uses. We presume that they can If we say a process does all make system calls and something, we really mean The VAS is both a block independently. its thread does it. “ sandbox ” and a “ lockbox ”: it limits The kernel can suspend/ what the process can STOP wait restart a thread wherever see/do, and protects and whenever it wants. its data from others.

  5. Two threads sharing a CPU/core concept reality context switch

  6. Thread ¡Abstrac-on ¡ • Infinite ¡number ¡of ¡processors ¡ • Threads ¡execute ¡with ¡variable ¡speed ¡ – Programs ¡must ¡be ¡designed ¡to ¡work ¡with ¡any ¡schedule ¡ Programmer Abstraction Physical Reality Threads 1 2 3 4 5 1 2 3 4 5 Processors 1 2 3 4 5 1 2 Running Ready Threads Threads

  7. Possible ¡Execu-ons ¡ Thread 1 Thread 1 Thread 2 Thread 2 Thread 3 Thread 3 a) One execution b) Another execution Thread 1 Thread 2 Thread 3 c) Another execution These executions are “schedules” chosen by the system.

  8. Thread APIs (rough/pseudocode) • Every thread system has an API for applications to use to manage their threads. Examples: Pthreads, Java threads, C#, Taos … – Create and start (“spawn” or “generate”) a thread. – Wait for a thread to complete (exit), and obtain a return value. – Break into the execution of a thread (optional). – Save and access data specific to each thread. • References to thread objects may be passed around in the API. Self operations (child) Thread operations (parent) a rough sketch: a rough sketch: exit (result); t = create (); yield (); t. start (proc, argv); t = self (); Details vary. t. alert (); (optional) setdata (ptr); result = t. join (); (wait) ptr = selfdata (); alertwait (); (optional)

  9. Threads lab (due 2/25) #define STACK_SIZE 262144 /* size of each thread's stack */ typedef void (*thread_startfunc_t) (void *); extern int thread_libinit(thread_startfunc_t func, void *arg); extern int thread_create(thread_startfunc_t func, void *arg); extern int thread_yield(void);

  10. Shared ¡vs. ¡Per-­‑Thread ¡State ¡ Shared Per − Thread Per − Thread State State State Thread Control Thread Control Heap Block (TCB) Block (TCB) Stack Stack Information Information Saved Saved Global Registers Registers Variables Thread Thread Metadata Metadata Stack Stack Code

  11. Thread context switch switch switch Virtual memory out in x program code library data R0 1. save registers CPU Rn (core) y x PC stack y SP registers 2. load registers stack Running code can suspend the current thread just by saving its register values in memory. Load them back to resume it at any time.

  12. Programmer ¡vs. ¡Processor ¡View ¡ Programmer’s Possible Possible Possible View Execution Execution Execution #1 #2 #3 . . . . . . . . . . . . x = x + 1; x = x + 1; x = x + 1 x = x + 1 y = y + x; y = y + x; .............. y = y + x z = x +5y; z = x + 5y; thread is suspended ............... . . other thread(s) run thread is suspended . . thread is resumed other thread(s) run . . ............... thread is resumed y = y + x ................ z = x + 5y z = x + 5y

  13. Example: Unix fork The Unix fork() system call creates/launches a new thread, in its own fresh virtual address space: it creates a new process. (Thread + VAS == Process.) Strangely, the new (“child”) process is an exact clone of the calling (“parent”) process. fork Oh Ghost of Walt, please don’t sue me.

  14. Unix fork/exit syscalls int pid = fork(); Create a new process that is a clone of its parent. Return child process ID ( pid ) parent to parent, return 0 to child. fork parent child exit(status); Exit with status, destroying the process. Status is returned to the parent. Note: this is not the only way for a process to exit! time data data exit exit p pid: 5587 pid: 5588

  15. fork The fork syscall returns twice: int pid; It returns a zero in int status = 0; the context of the new child process. if (pid = fork ()) { /* parent */ It returns the new … .. child process ID } else { /* child */ (pid) in the context of the parent. … .. exit(status); }

  16. wait syscall Parent uses wait to sleep until the child int pid; exits; wait returns child int status = 0; pid and status. if (pid = fork()) { Wait variants allow wait /* parent */ on a specific child, or … .. notification of stops and pid = wait(&status); other “signals”. } else { Recommended: use waitpid() . /* child */ … .. exit(status); }

  17. wait Process states (i.e., states of the main thread of the process) “sleep” “wakeup” Note : in modern Unix systems the wait syscall has many variants and options.

  18. A simple program: parallel … Parallel creates N child processes and waits int for them all to complete. main( … arg N … ) { Each child performs a computation that for 1 to N takes, oh, 10-15 seconds, storing values dofork(); repeatedly to a global variable, then it exits. for 1 to N wait( … ); How does N affect completion time? } void child() { chase$ cc –o parallel parallel.c BUSYWORK {x = v;} chase$ ./parallel exit(0); ??? } chase$ …

  19. A simple program: parallel 70000 Three different machines 60000 50000 Completion time 40000 (ms) 30000 20000 10000 0 0 5 10 15 20 25 30 N (# of children)

  20. Parallel: some questions • Which machine is fastest? • How does the total work grow as a function of N? • Does completion time scale with the total work ? Why? • Why are the lines flatter for low values of N? • How many cores do these machines have? • Why is the timing roughly linear, even for “odd” N? • Why do the lines have different slopes? • Why would the completion time ever drop with higher N? • Why is one of the lines smoother than the other two? • Can we filter out the noise?

  21. Thread states and transitions If a thread is in the ready state thread, then the system may choose to run it “at any time”. The kernel can switch threads whenever it gains control on a core, e.g., by a timer interrupt. If the current thread takes a fault or system call trap, and blocks or exits, then the scheduler switches to another thread. But it could also preempt a running thread. From the point of view of the program, dispatch and preemption are nondeterministic : we can’t know the schedule in advance. running These preempt and yield dispatch transitions are preempt controlled by the kernel sleep scheduler. dispatch Sleep and wakeup transitions are initiated wakeup blocked ready by calls to internal waiting sleep/wakeup APIs by a running thread. STOP wait

  22. Thread ¡Lifecycle ¡ Scheduler Thread Creation Thread Exit Resumes Thread Running Finished Init Ready e.g., e.g., sthread_exit() sthread_create() Thread Yields/ Scheduler Suspends Thread Event Occurs Thread Waits for Event e.g., sthread_yield() e.g., e.g., other thread sthread_join() calls sthread_join() Waiting

  23. What cores do Idle loop scheduler pause idle getNextToRun() nothing? get put sleep? thread thread exit? ready queue timer got (runqueue) quantum thread expired? switch in switch out run thread

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend