Processes and threadsProcess Concept Embodies Unit of Resource ownership - process is allocated a virtual address space to hold the process image Unit of Dispatching - process is an execution path through one or more programs execution may be interleaved with other processes» These two characteristics are treated independently by the operating system2Introduction to Threads Effectiveness of parallel computing depends on the performance of the primitives used to express and control parallelism Separate notion of execution from Process abstraction Useful for expressing the intrinsic concurrency of a program regardless of resulting performance We will discuss Two examples of threading: User threads, Kernel threads3Introduction to Threads 4Threads Separate notion of execution A unit of local dispatching (scheduling) and has priority Has an execution path (is a thread of control) Has a computation state (stack, set of CPU registers) Global variables are shared by all threads System state shared by threads File descriptors, working directory, etc.5Threads continue Thread : Dynamic object representing an execution path and computational state. One or more threads per process, each having: Execution state (running, ready, etc.) Saved thread context when not running Execution stack Per-thread static storage for local variables Shared access to process resources all threads of a process share a common address space.6Thread States Primary states: Running, Ready and Blocked Operations to change state: Spawn: new thread provided register context and stack pointer. Block: event wait, save user registers, PC and stack pointer Unblock: moved to ready state Finish: deallocate register context and stacks7Threading Models8User Level Threads User level threads - supported by user level threads libraries Examples POSIX Pthreads, Mach C-threads, Solaris ui-threads Benefits: no modifications required to kernel flexible and low cost Drawbacks: can not block without blocking entire process no parallelism (not recognized by kernel)9Kernel Level Threads Kernel level threads - directly supported by kernel, thread is the basic scheduling entity Examples: Windows 95/98/NT/2000/XP, Solaris, Tru64 UNIX, BeOS, Linux Benefits: coordination between scheduling and synchronization less overhead than a process suitable for parallel application Drawbacks: more expensive than user-level threads generality leads to greater overhead10Threading Issues fork and exec should fork duplicate one, some or all threads Cancellation – cancel the target thread, issues with freeing resources and inconsistent state asynchronous cancellation – target is immediately canceled deferred cancellation – target checks periodically. Checks at cancellation points11Threading Issues Signals: generation, posting and delivery Every signal handled by a default or user-defined handler Signal delivery: to thread for which it may apply to every thread in process to certain threads specifically designated thread (signal thread)12Threading Issues synchronous signals should go to thread causing the signal what about asynchronous signals? Solaris: deliver to a special thread which forward to first user created thread that has not blocked the signal.13Threading Issues Bounding the number of threads created in a dynamic environment use thread pools All threads share some address space: use of thread specific data14Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, not the implementation Common in UNIX operating systems Programs must include <pthread.h> Documentation and examples
View Full Document