Lesson 4 (Threads and Concurrency)

Cards (24)

  • What is a Thread?
    The smallest unit of execution within a process in an operating system.
    • It allows for concurrent execution of tasks within a program, sharing resources like memory and files with other threads in the same process.
  • Why thread instead of process creation?
    Process creation is heavy-weight while thread creation is light-weight
    Can simplify code, increase efficiency
  • Benefits of Threads
    • Economy
    • Resource Sharing
    • Scalability/Responsivenes.
  • Benefits of Threads: Economy
    • cheaper than process creation, thread switching lower overhead than context switching. In general,
    • It is significantly more time consuming to create and manage processes than threads
  • Benefits of Threads: Resource Sharing
    threads share memory and the resources of the process to which they belong to, easier than shared memory or message passing.
    The benefit of sharing code and data is that it allows an application to have several different threads of activity within the same address space.
  • Benefits of Threads: Scalability
    process can take advantage of multiprocessor architectures where threads could be running in parallel on different processing cores.
    A single-threaded process can run on only one processor.
  • Benefits of Threads: Responsiveness
    Allows for continued execution if part of process is blocked, especially important for user interfaces.
    A single-threaded application would be unresponsive to the user until the operation had completed. However, if the time-consuming operation is performed in a separate thread, the application remains responsive to the user.
  • Concurrent execution on single-core system
    more than one task making progress. Scheduler providing concurrency
  • Parallelism on a multi-core system
    A system can perform more than one task simultaneously.
  • Challlenges on Multicore/processor systems
    • Dividing activities : need to find areas that can be divided into separate, concurrent tasks.
    • Balance : to ensure that concurrent tasks perform equal work of equal value.
    • Data splitting : the data accessed by tasks must be divided to run on separate cores.
    • Data dependency : ensure that the execution of the tasks is synchronized for data dependency.
    • Testing and debugging : different execution paths are possible, testing and debugging such concurrent programs is inherently more difficult.
  • Multicore Programming: Data Parallelism
    • distributes subsets of the same data across multiple cores, same operation on each
  • Multicore Programming: Task Parallelism
    • distributing threads across cores, each thread performing unique operation
  • User Threads
    • management done by user-level threads library
    Three primary thread libraries:
    1. POSIX Pthreads
    2. Win32 threads
    3. Java threads
  • Kernel threads
    Supported by the Kernel
    Examples – virtually all general purpose operating systems, including:
    § Windows
    § Solaris
    § Linux
    § Tru64 UNIX
    § Mac OS X
  • Thread Models
    1. Many-to-One
    2. One-to-One
    3. Many-to-Many
    4. Two-level Model
  • Thread Model: Many-to-One
    • Many user-level threads mapped to single kernel thread
    • One thread blocking causes all to block
    • As only one thread can access the kernel at a time, multiple threads may not run in parallel on multicore system
    • Does not result in true concurrency as kernel can schedule only one thread at a time §
    • Few systems currently use this model (eg, Solaris Green Threads, GNU Portable Threads)
  • Thread Models: One-to-One
    • Each user-level thread maps to kernel thread §
    • Creating a user-level thread creates a kernel thread
    • More concurrency than many-to-one.
    • Also allows multiple threads to run in parallel on multiprocessors
    • Number of threads per process sometimes restricted.
    • Why ? Due to the overhead of creating kernel threads can burden the performance of an application
    • Examples: Windows NT/XP/2000,Linux § Solaris 9 and later
  • Thread Models: Many-to-Many
    • Allows many user level threads to be mapped to many kernel threads
    • Allows the operating system to create a sufficient number of kernel threads
    • When a thread performs a blocking system call, the kernel can schedule another thread for execution
    • Example: Solaris prior to version 9, Windows NT/2000 with the ThreadFiber package
  • Thread-Model: Two-level Model
    • Similar to M:M, except that it allows a user thread to be bound to kernel thread
    • Examples: IRIX, HP-UX, Tru64 UNIX, Solaris 8 and earlier
  • Thread Libraries
    provides programmer with API for creating and managing thread
  • Ways to Implement Thread Libraries
    • Library entirely in user space with no kernel support. All code and data structures for the library exist in user space and invoking a function in the library results in a local function call in user space and not a system call. E.g., Posix Pthreads.
    • Kernel-level library supported by the OS. Code and data structures for the library exist in kernel space. Invoking a function in the API for the library typically results in a system call to the kernel. E.g., Windows Threads or Linux System V.
  • Pthread Library
    • May be provided either as user-level or kernel-level
    • A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
    • Specification, not implementation
    • API specifies behavior of the thread library, implementation is up to development of the library
    • Common in UNIX operating systems (Solaris, Linux, Mac OS X)
  • Pthread System Calls
    • pthread_create(): Creates a new thread
    • pthread_attr_init(): Initialize thread attributes like stack size, scheduling size and priority.
    • pthread_exit() Terminates the calling thread.
    • pthread_join(): Waits for a thread to finish. It is equivalent to wait in processes
    • pthread_detach() detaches a thread releasing the resources after termination. It used to manage resources.
    • pthread_cancel() Requests cancellation of a thread.
  • Threading Issues
    • Semantics of fork() and exec() system calls
    • Signal handling (Synchronous and asynchronous)
    • Thread cancellation of target thread (Asynchronous or deferred)
    • Thread-local storage
    • Thread Safety