Thread - a single flow of control ====== Process - an address space and a single thread of control. Need - multiple threads of control sharing the same address space. Multiple Threads in a program means that any instant the program has multiple points of execution, one in each of its threads. Threads execute within a "single address space" means that they can read and write the same memory location. Threads are "light weight" - creation, existence, destruction and synch. primitives are cheap. Why use concurrency? 1) multi-process system - multiple processes with separate address spaces expenses to set up, comm., etc. A light-weight multi-threading facility is better. 2) threads are useful in dealing with slow devices. -- disks, networks, terminals, printers. 3) humans are good at doing a few things simultaneously. 4) Client/Server model is distributed system -- one server and multiple clients. Organization of threads in a proces (Fig 4-3) a) Dispatcher/workers model - e.g. file server b) Team model - request queue, RPC servers c) Pipeline model - unix commands Design Issues for Threads Faciliites Four major mechanisms 1) thread creation 2) mutual exclusion 3) waiting for events 4) scheduling 1) static vs. dynamic threads -- fork, join exit voluntarily or killed from outside. 2) shared data critical region i) mutex - two states, locked or unlocked lock - atomic, block if already locked unlock - unblock one of waiting threads. ii) condition variable wait, signal, boradcast Fig 4-5 Implementation - Threads in user space vs. in the kernel 1. Put the threads package entirely in user space + kernel knows nothiing about them + need a run-time system within a process to manage threads + program counter & stack pointer efficient thread switching + user can implement its own thread scheduling - how to implement blocking system calls a blocking system call blocks all threads, not first one. - blocking system calls can be modified for non-blocking sys calls -- requires change to O.S (not desirable) - before blocking system call (e.g. READ) check whether it is going to block (e.g SELECT) - thread scheduling is like coroutine. busy waiting results in devergence can schedule using block interrupts. 2. Implementing Threads in the Kernel. + no run-time system is needed by user process + the kernel has one threads table per process. + interthread syn. are implemented as sys. calls -- greater cost + greater cost of creating & destroying threads in the kernel. + do not require any new, nonblocking system call. + return value problem + which thread handles signals SunOS Motivation for Threads 1) multiprocessor hardware 2) application concurrency many applications are best structured as several indep. computations. Threads Lightweight Process (LWP) A thread has a program counter (PC), a stack to keep track of local variables and return addresses. Threads share 1) the process instructions and most of its data. 2) most of the OS state of a process. e.g. open files, exit() forces process terminations. LWP - for control over how the program is mapped onto parallel hardware. - In SunOS, a Unix Process consists mainly of an addr. space and a set of LWPs that share that addr. space. - Each LWP is like a virtual CPU available for executing code or sys. call. - Each LWP is dispatched by the kernel. indep. sys. call, page faults, run in parallel on a multiprocessor. - scheduled by the kernel - threads are executed using LWPs (Fig 2) - a thread executing a sys call is bound to the LWP executing it until the system call is completed. Why both threads & LWP? - threads are not known to the kernel. - creat, destroy, block, etc are cheap. - for connect operation of sys. call (I/O), the kernel need to block & know who is blocked. - LWP parallelism is relatively expensive. - Fig 3.