1 / 24

Threads

Threads. Fred Kuhns fredk@cse.wustl.edu Applied Research Laboratory Department of Computer Science and Engineering, Washington University in St. Louis. Introduction to Threads. Multithreaded Process Model. Single-Threaded Process Model. Thread. Thread. Thread. Thread Control Block.

gezana
Download Presentation

Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Threads Fred Kuhns fredk@cse.wustl.edu Applied Research Laboratory Department of Computer Science and Engineering, Washington University in St. Louis

  2. Introduction to Threads Multithreaded Process Model Single-Threaded Process Model Thread Thread Thread Thread Control Block Thread Control Block Thread Control Block Process Control Block User Stack Process Control Block User Stack User Stack User Stack User Address Space Kernel Stack User Address Space Kernel Stack Kernel Stack Kernel Stack CSE522– Advanced Operating Systems

  3. Some Definitions • Parallelism: degree to which a multiprocessor application achieves parallel execution • Concurrency: Maximum parallelism an application can achieve with unlimited processors • System Concurrency: kernel recognizes multiple threads of control in a program • User Concurrency: User space threads (coroutines) to provide a natural programming model for concurrent applications. Concurrency not supported by system. CSE522– Advanced Operating Systems

  4. Process and Threads • Process: encompasses • Unit of dispatching: process is an execution path through one or more programs set of threads (computational entities) • execution may be interleaved with other processes • Unit of resource ownership: process is allocated a virtual address space to hold the process image • Thread: Dynamic object representing an execution path and computational state. • threads have their own computational state: PC, stack, user registers and private data • Remaining resources are shared amongst threads in a process CSE522– Advanced Operating Systems

  5. Threads • Threads separate the notion of execution from the Process abstraction • Effectiveness of parallel computing depends on the performance of the primitives used to express and control parallelism • Useful for expressing the intrinsic concurrency of a program regardless of resulting performance • Three types: • User threads, N:1 model • kernel threads, 1:1 model • Light Weight Processes (LWP), M:N model CSE522– Advanced Operating Systems

  6. Threading Models • User level threads - user libraries implementation • Benefits: no kernel modifications, flexible and low cost • Drawbacks: thread may block entire process, no parallelism • Kernel level threads - kernel directly supports multiple threads of control in a process. • Benefits: scheduling/synchronization coordination, less overhead than process, suitable for parallel application • Drawbacks: more expensive than user-level threads, generality leads to greater overhead • Light-Weight Processes (LWP), also known as virtual processors. Kernel supported user threads • LWP bound to kernel thread: a kernel thread may not be bound to an LWP • LWP is scheduled by kernel • User threads scheduled by library onto LWPs • Multiple LWPs per process CSE522– Advanced Operating Systems

  7. Thread 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 stacks. CSE522– Advanced Operating Systems

  8. P P P P P P Many-to-One One-to-One Many-to-Many Threading Models • User threads := Many-to-One • kernel threads := One-to-One • Mixed user and kernel := Many-to-Many CSE522– Advanced Operating Systems

  9. Threading Issues • fork and exec • should fork duplicate one, some or all threads • Cancellation – issues with freeing resources and inconsistent state • asynchronous cancellation – target is immediately canceled • deferred cancellation – target checks periodically. check at cancellation points • Signals: generation, posting and delivery • per thread signal masks but process shared disposition • Signal delivery: • to which thread should a signal be delivered • specifically designated thread (signal thread) • 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. • Bounding the number of threads created in a dynamic environment • use thread pools • Al threads share same address space: • use of thread specific data CSE522– Advanced Operating Systems

  10. First Class threads (Psyche OS) • Thread operations in user space: • create, destroy, synch, context switch • kernel threads implement a virtual processor • Course grain in kernel - preemptive scheduling • Communication between kernel and threads library • shared data structures. • Software interrupts (user upcalls or signals). Example, for scheduling decisions and preemption warnings. • Kernel scheduler interface - allows dissimilar thread packages to coordinate. CSE522– Advanced Operating Systems

  11. Scheduler Activations • An activation: • serves as execution context for running thread • notifies thread of kernel events (upcall) • space for kernel to save processor context of current user thread when stopped by kernel • Library schedules user threads on activations. • space for kernel to save processor context of current user thread when stopped by kernel • upall performed when one of the following occurs: • user thread performs blocking system call • blocked thread belonging to process, then its library is notified allowing it to either schedule a new thread or resume the preempted thread. • kernel is responsible for processor allocation => preemption by kernel. • Thread package responsible for scheduling threads on available processors (activations) CSE522– Advanced Operating Systems

  12. Pthreads • a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. • API specifies behavior of the thread library, implementation is up to development of the library. • Common in UNIX operating systems. CSE522– Advanced Operating Systems

  13. Example Kernel Threads (Old Solaris) • Supports: • user threads (uthreads) via libthread and libpthread • LWPs, acts as a virtual CPU for user threads • kernel threads (kthread), every LWP is associated with one kthread, however a kthread may not have an LWP • interrupts as threads • Fundamental scheduling/dispatching object • all kthreads share same virtual address space (the kernels) - cheap context switch • System threads - example STREAMS, callout • kthread_t, /usr/include/sys/thread.h • scheduling info, pointers for scheduler or sleep queues, pointer to klwp_t and proc_t CSE522– Advanced Operating Systems

  14. Old Solaris LWP • Bound to 1 kthread • LWP specific fields from proc are kept in klwp_t (/usr/include/sys/klwp.h) • user-level registers, system call params, resource usage, pointer to kthread_t and proc_t • klwp_t can be swapped • LWP non-swappable info kept in kthread_t • All LWPs in a process share: • signal handlers • Each may have its own • signal mask • alternate stack for signal handling • No global name space for LWPs CSE522– Advanced Operating Systems

  15. User Threads • Implemented in user libraries • library provides synchronization and scheduling facilities • threads may be bound to LWPs • unbound threads compete for available LWPs • Manage thread specific info • thread id, saved register state, user stack, signal mask, priority*, thread local storage • Solaris provides two libraries: libthread and libpthread. • Try man thread or man pthreads CSE522– Advanced Operating Systems

  16. Solaris Thread Data Structures proc_t p_tlist kthread_t t_procp t_lwp klwp_t t_forw lwp_thread lwp_procp (non-swappable) CSE522– Advanced Operating Systems

  17. L L L L ... ... ... P P P Processes, Threads and LWPs Process 2 Process 1 user Int kthr kernel hardware CSE522– Advanced Operating Systems

  18. Solaris User Level Threads Stop Wakeup Runnable Continue Stop Stopped Sleeping Preempt Dispatch Stop Active Sleep CSE522– Advanced Operating Systems

  19. Lightweight Processes Timeslice or Preempt Stop Running Dispatch Wakeup Blocking System Call Runnable Stopped Continue Wakeup Stop Blocked CSE522– Advanced Operating Systems

  20. Solaris Interrupts • One system wide clock kthread • pool of 9 partially initialized kthreads per CPU for interrupts • interrupt thread can block • interrupted thread is pinned to the CPU CSE522– Advanced Operating Systems

  21. Solaris Signals and Fork • Divided into Traps (synchronous) and interrupts (asynchronous) • each thread has its own signal mask, global set of signal handlers • Each LWP can specify alternate stack • fork replicates all LWPs • fork1 only the invoking LWP/thread CSE522– Advanced Operating Systems

  22. Mach • Two abstractions: • Task - static object, address space and system resources called port rights. • Thread - fundamental execution unit and runs in context of a task. • Zero or more threads per task, • kernel schedulable • kernel stack • computational state • Processor sets - available processors divided into non-intersecting sets. • permits dedicating processor sets to one or more tasks • C-Threads • Coroutine-based - multiplex user threads onto a single-threaded task • Thread-based - one-to-one mapping from c-threads to Mach threads. Default. • Task-based - One Mach Task per c-thread. CSE522– Advanced Operating Systems

  23. Mach Continuations • Address problem of excessive kernel stack memory requirements • process model versus interrupt model • one per process kernel stack versus a per thread kernel stack • Thread is first responsible for saving any required state (the thread structure allows up to 28 bytes) • indicate a function to be invoked when unblocked (the continuation function) • Advantage: stack can be transferred between threads eliminating copy overhead. CSE522– Advanced Operating Systems

  24. Threads in Windows NT • Design driven by need to support a variety of OS environments • NT process implemented as an object • executable process contains >= 1 thread • process and thread objects have built in synchronization capabilitiesS • Support for kernel (system) threads • Threads are scheduled by the kernel and thus are similar to UNIX threads bound to an LWP (kernel thread) • fibers are threads which are not scheduled by the kernel and thus are similar to unbound user threads. CSE522– Advanced Operating Systems

More Related