CS 423 Operating System Design: Concurrency (more Threads) Professor Adam Bates Fall 2018 CS423: Operating Systems Design
Goals for Today • Learning Objectives: • Dive yet further into concurrency and threading • Announcements: • MP1 out on Friday! Reminder : Please put away devices at the start of class CS 423: Operating Systems Design 2
HW1 Wrap-Up • Average Score: 88.8%… • Three questions of medium difficulty (<80%): • “Hold-and-wait” situations? • which code snippets are wrong? • UNIX shell? CS 423: Operating Systems Design 3
HW1 Wrap-Up Which of the following systems may never exhibit a "hold-and-wait" situation? You may assume that the only blocking that occurs in these systems occurs on mutexes. a.Systems that ensure that all resources needed for an application are locked in a single atomic operation that either succeeds and locks all requested resources or fails and locks none. b. Systems with only one mutex. c. Systems where all mutexes are numbered. A user cannot lock a mutex with a lower number, X, after they have locked a mutex with a larger number, Y > X. d. Systems of type (a) and (b) only e. Systems of type (a), (b), or (c) CS 423: Operating Systems Design 4
HW1 Wrap-Up Which of the following code snippets are wrong? Case 1 Case 2 Case 3 int *p; char a[2]; int b[10]; *p=10; strcpy (a, “Hi”); *b=11; CS 423: Operating Systems Design 5
HW1 Wrap-Up Which of the following best describes a UNIX shell? a. Part of the UNIX kernel that executes user commands b. A process forked off at UNIX initialization to accept inputs from a user c. A system call executed by the UNIX startup routine to accept commands from users d. A library that implements various UNIX commands e. The UNIX keyboard device driver that interprets keyboard input CS 423: Operating Systems Design 6
Why Concurrency? • Servers – Mul*ple connec*ons handled simultaneously • Parallel programs – To achieve be:er performance • Programs with user interfaces – To achieve user responsiveness while doing computa*on • Network and disk bound programs – To hide network/disk latency CS423: Operating Systems Design 7
Definitions • Thread: A single execution sequence that represents a separately schedulable task. • Single execution sequence : intuitive and familiar programming model • separately schedulable : OS can run or suspend a thread at any time. • Schedulers operate over threads/tasks, both kernel and user threads. • Does the OS protect all threads from one another? CS423: Operating Systems Design 8
The Thread Abstraction • Infinite number of processors • Threads execute with variable speed Programmer Abstraction Physical Reality Threads Processors 1 2 3 4 5 1 2 Running Ready Threads Threads CS423: Operating Systems Design 9
Programmer vs. Processor View Programmer 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 + 5 y ; z = x + 5 y ; . . . . . . . . . . . . . . . Thread is suspended. . . Thread is suspended. Other thread(s) run. . . Other thread(s) run. Thread is resumed. . . . . . . . . . . . . . . . . . Thread is resumed. y = y + x ; . . . . . . . . . . . . . . . . z = x + 5 y ; z = x + 5 y ; Variable Speed: Program must anticipate all of these possible executions CS423: Operating Systems Design 10
Possible Executions Processor View One Execution Another Execution Thread 1 Thread 1 Thread 2 Thread 2 Thread 3 Thread 3 Another Execution Thread 1 Thread 2 Thread 3 Something to look forward to when we discuss scheduling! CS423: Operating Systems Design 11
Thread Ops • thread_create(thread, func, args) Create a new thread to run func(args) • thread_yield() Relinquish processor voluntarily • thread_join(thread) In parent, wait for forked thread to exit, then return • thread_exit Quit thread and clean up, wake up joiner if any CS423: Operating Systems Design 12
Ex: threadHello #define NTHREADS 10 thread_t threads[NTHREADS]; main() { for (i = 0; i < NTHREADS; i++) thread_create(&threads[i], &go, i); for (i = 0; i < NTHREADS; i++) { exitValue = thread_join(threads[i]); printf("Thread %d returned with %ld\n", i, exitValue); } printf("Main thread done.\n"); } void go (int n) { printf("Hello from thread %d\n", n); thread_exit(100 + n); // REACHED? } CS423: Operating Systems Design 13
Ex: threadHello output • Must “thread returned” print in order? • What is maximum # of threads running when thread 5 prints hello? • Minimum? CS423: Operating Systems Design 14
Fork/Join Concurrency • Threads can create children, and wait for their completion • Data only shared before fork/after join • Examples: • Web server: fork a new thread for every new connection • As long as the threads are completely independent • Merge sort • Parallel memory copy CS423: Operating Systems Design 15
Ex: bzero void blockzero (unsigned char *p, int length) { int i, j; thread_t threads[NTHREADS]; struct bzeroparams params[NTHREADS]; // For simplicity, assumes length is divisible by NTHREADS. for (i = 0, j = 0; i < NTHREADS; i++, j += length/NTHREADS) { params[i].buffer = p + i * length/NTHREADS; params[i].length = length/NTHREADS; thread_create_p(&(threads[i]), &go, ¶ms[i]); } for (i = 0; i < NTHREADS; i++) { thread_join(threads[i]); } } CS423: Operating Systems Design 16
Thread Data Structures Shared Thread 1 � s Thread �� s State Per �� hread State Per �� hread State Thread Control Thread Control Block (TCB) Block (TCB) Code Stack Stack Information Information Saved Saved Registers Registers Global Variables Thread Thread Metadata Metadata Stack Stack Heap CS423: Operating Systems Design 17
Thread Lifecycle Scheduler Resumes Thread Thread Creation Thread Exit Init Ready Running Finished sthread_create() s t h r e a d _ e x i t ( ) Thread Yield/Scheduler Suspends Thread s t h r e a d _ y i e l d ( ) Event Occurs Thread Waits for Event s t h r e a d _ j o i n ( ) 0ther Thread Calls s t h r e a d _ j o i n ( ) Waiting CS423: Operating Systems Design 18
Thread Implementations • Kernel threads • Thread abstraction only available to kernel • To the kernel, a kernel thread and a single threaded user process look quite similar • Multithreaded processes using kernel threads • Kernel thread operations available via syscall • User-level threads • Thread operations without system calls CS423: Operating Systems Design 19
Multithreaded OS Kernel Code Kernel Thread 1 Kernel Thread 2 Kernel Thread 3 Process 1 Process 2 Kernel Globals TCB 1 TCB 2 TCB 3 PCB 1 PCB 2 Stack Stack Stack Stack Stack Heap Process 1 Process 2 User-Level Processes Thread Thread Stack Stack Code Code Globals Globals Heap Heap CS423: Operating Systems Design 20
Implementing Threads • Thread_fork(func, args) • Allocate thread control block • Allocate stack • Build stack frame for base of stack (stub) • Put func, args on stack • Put thread on ready list • Will run sometime later (maybe right away!) • stub(func, args): • Call (*func)(args) • If return, call thread_exit() CS423: Operating Systems Design 21
Implementing Threads • Thread_Exit • Remove thread from the ready list so that it will never run again • Free the per-thread state allocated for the thread CS423: Operating Systems Design 22
switchframe How do we switch out thread state? (i.e., ctx switch) # Save caller’s register state # NOTE: %eax, etc. are ephemeral pushl %ebx pushl %ebp pushl %esi pushl %edi # Get offsetof (struct thread, stack) mov thread_stack_ofs, %edx # Save current stack pointer to old thread's stack, if any. movl SWITCH_CUR(%esp), %eax movl %esp, (%eax,%edx,1) # Change stack pointer to new thread's stack # this also changes currentThread movl SWITCH_NEXT(%esp), %ecx movl (%ecx,%edx,1), %esp # Restore caller's register state. popl %edi popl %esi popl %ebp popl %ebx ret CS423: Operating Systems Design 23
A subtlety • Thread_create puts new thread on ready list • When it first runs, some thread calls switchframe • Saves old thread state to stack • Restores new thread state from stack • Set up new thread’s stack as if it had saved its state in switchframe • “returns” to stub at base of stack to run func CS423: Operating Systems Design 24
Recommend
More recommend