Threads
1
                                              Threads
   Thread represents a basic unit of CPU utilization, and threads belonging to
    the same process share many of the process resources, including code and
    data
   If   a   process      has     multiple     threads,   it   can   perform   multiple   tasks
    simultaneously
   Most software applications running on modern computers are multithreaded
     Example: A word processor application
     might use
     o one thread to take input from the keyboard
     o another to perform spell checks
     o another to adjust the display layout
                                 Threads
   Multiple tasks with the application can be implemented by separate
    threads
    o Update display
    o Fetch data
    o Spell checking
    o Answer a network request
                                    Threads
   Applications can be designed to maximize the capacity of multicore
    systems
    o A multithreaded web server creates a separate thread for each incoming
      request, while the main process continues listening on the port
    o Many operating system kernels are multithreaded, allowing them to manage
      devices, memory, and interrupts simultaneously
                                                     Threads
▪ How does multithreading work in OS?
▪ Multithreading      divides    the   task     of
  application into separate individual thread
▪ The same process or tasks in multithreading
  can be done by several threads or it can be
  said that more than one thread is used for
  performing the tasks in multithreading
▪ For example, client 1, client 2, and client 3
  in the above example are accessing the web
  server without having to wait for other tasks
  to be complete
                                  Threads
   Concurrent work on the same process for efficiency
   Several activities going on as part of the same process
   Share registers, memory, and other resources
                                            Threads
   Threads vs. Processes
    o Processes form a tree hierarchy
    o Threads form a pool of peers
       •   Each thread can kill any other
       •   Each thread can wait for any
           other thread to terminate
       •   Main thread: first thread to run
           in a process (always exist)          Process hierarchy   Thread pool
                                      Threads
   Threads vs. Processes
   Each process has one or more threads “within” it
    o Each thread has its own stack, CPU registers, etc.
    o All threads within a process share the same
      address space and OS resources
       •   Threads   share   memory,      so   they     can
           communicate directly!
   The thread is now the unit of CPU scheduling
    o A process is just a “container” for its threads
    o Each thread is bound to its containing process
                            Threads
   Threads vs. Processes
                                  Threads
   Benefits of Threads: The benefits of threads can be classified into four
    categories:
    o Responsiveness: In interactive applications, even if part of the
      application is blocked or performing a lengthy operation, another
      part can continue interacting with the user, thus improving system
      responsiveness
    o Resource    Sharing:   Processes   can   share   resources   through
      techniques like shared memory or message passing. Threads within
      the same process can share memory and other resources of that
      process
                              Threads
o Economy: Since threads share the resources of the process they
 belong to, context switches between threads are more economical
 (For example, in the Solaris operating system, creating a thread is 30
 times faster, and context switches are 5 times faster.)
o Scalability: In multiprocessor architectures, threads can run in parallel
 on different cores. However, a process with a single thread can only
 run on one processor
                                 Threads
   Multicore Programming: One of the most important developments in
    computer design is the creation of multiprocessor systems
   Recently, multiple cores have been placed on a single chip, and these
    systems are called multicore or multiprocessor systems
    o Each core appears as a separate processor to the operating system
    o In an application with four threads running on a single core, threads
      are executed concurrently in intervals. In systems with multiple
      cores, threads are executed in parallel, with each core assigned a
      thread
                                                Threads
   Multicore Programming
   Parallelism refers to the simultaneous execution of multiple tasks,
    while concurrency involves making progress on multiple tasks by
    switching between them at short intervals
   As the number of cores increases, the number of tasks that can be
    executed concurrently also increases
            Concurrent execution on single-core system
                                                          Parallelism on a multi-core system
                                 Threads
   Single and Multithreaded Processes
                                          Threads
▪ Amdahl's Law: Amdahl's Law describes the performance improvement in a
 system based on the number of cores:
   o S represents the fraction of the application that must be executed serially
   o N represents the number of cores
▪ For example, if an application runs 75% in parallel and 25% serially (S=0.25), running it on a
 system with 2 cores (N=2) results in a 1.6x speedup. If the system has 4 cores, the speedup is
 2.28x
▪ As the number of cores approaches infinity, the speedup approaches 1/S
▪ Intel CPUs support 2 threads per core
                                 Threads
   Types of Parallelism
   Data Parallelism: Focuses on distributing pieces of the same dataset
    across different cores for simultaneous execution of the same type of
    operation
    o For example, if two cores are used to sum an array of N elements,
      elements [0]..[(N/2)-1] are summed on core 1, and elements
      [N/2]..[N-1] are summed on core 2
         Data Parallelism distributes subsets of the same data across
         different computing cores and performs the same operation on
         each core
                                    Threads
   Task Parallelism: Focuses on distributing tasks (threads) to cores
    o Each thread performs a different operation
    o Different threads may operate on the same data or different data
    o For example, threads performing different statistical calculations on
      the same dataset would run on different cores
           Task parallelism distributes not data but tasks across multiple
           cores. Each task is running a unique operation
                                   Threads
   Multithreading Models
   Thread support can be provided either at the user level (user threads)
    or at the kernel level (kernel threads)
   User threads are managed by user applications, while kernel threads
    are managed by the operating system
   Operating systems such as Windows, Linux, Unix, Mac OS X, and
    Solaris support kernel threads
            User applications create user-level threads, which must
            ultimately be mapped to kernel threads to execute on a CPU
                                        Threads
   Multithreading Models
   The relationship between user and kernel
    threads :
    o Many-to-One Model:
       •   Many user threads are mapped to one
           kernel thread (Used by Solaris.)
       •   If one thread blocks due to a system
           call, the entire process is blocked
       •   Since only one kernel thread is used,
           multithreading cannot be parallelized
           across multiple cores
                                Threads
o One-to-One Model: Each user thread
 is mapped to a kernel thread (Used
 by Linux and Windows)
  •   If one thread blocks, the other
      threads can continue
  •   Multiple kernel threads can run in
      parallel on multicore systems
  •   However, a new kernel thread
      must be created for each user
      thread
                                Threads
o Many-to-Many Model: Many user
 threads are mapped to an equal or
 smaller number of kernel threads
 (Used by Solaris 9 and Unix)
  •   If one thread blocks, the kernel
      can run another thread
                                 Threads
   Thread Libraries provide an API for creating and managing threads.
    There are two different approaches when building a thread library:
    o The entire thread library is created in user space, without any
      kernel support
    o The library is built at the kernel level, directly supported by the
      operating system
                                 Threads
   Popular Thread Libraries
   There are three main thread libraries commonly used today:
    o POSIX Pthreads: Provides user-level or kernel-level thread libraries
    o Windows Threads: Supports kernel-level threads
    o Java Threads: Provides user-level thread libraries
                                    Threads
▪ Pthreads
▪ POSIX Pthreads is an API defined by the IEEE 1003.1c standard for creating
 and managing threads
▪ Pthreads is used in Linux, Unix, Mac OS X, and Solaris operating systems
▪ Windows does not support the Pthreads standard
▪ In Pthreads, each thread is created as a separate function, and all threads share
 global data
                                          Threads
▪ Pthreads
▪ Example of Pthreads Program
▪ A program using Pthreads may include the following:
   o A header file for thread functions
   o Defining an ID for the new thread
   o Setting the attributes for the new thread (e.g., stack size)
   o Specifying the starting point for the thread (a function)
   o Creating the thread with a fork-join strategy
▪ Multiple threads can be created and managed using this method
                                       Threads
▪ Windows Threads
▪ Windows' thread library is similar to Pthreads in many aspects. Each thread is
 created as a separate function, and all threads share global data.
 Windows threads are kernel-level threads
▪ Example of Windows Threads: In Windows, thread creation might involve the
 following steps:
   o Use a header file for thread functions
   o Define the starting point for the thread
   o Use a system call like WaitForMultipleObjects() to allow multiple threads to run
    synchronously
                                      Threads
▪ Java Threads
▪ Java threads are designed to work on any system that supports the Java Virtual
 Machine (JVM). The Java thread API is available for Windows, Linux, Unix, Mac
 OS X, and Android
▪ Data sharing between Java threads is done via parameter passing. Threads
 can be created in two ways:
   o Extending the Thread Class: A new class is created by extending the Thread class
    and overriding the run() method
   o Using the Runnable Interface: A class that implements the Runnable interface is
    created. This method is more commonly used
                                   Threads
▪ Implicit Threading
▪ With the development of multicore processors, applications now often contain
 hundreds or even thousands of threads
▪ Creating and managing such a large number of threads manually is challenging
 and prone to errors
▪ As a result, implicit threading, where the compiler handles the creation of
 threads, is becoming increasingly popular
                                   Threads
▪ Thread Pools
▪ A multithreaded web server, for instance, creates a new thread for each incoming
 request. However, if the number of requests becomes too large, system
 resources like CPU time and memory can be exhausted. To prevent this, a thread
 pool is created, allowing only a fixed number of threads to be created
▪ When a new request arrives, a thread from the pool is used if available;
 otherwise, the system waits for a thread to be released
▪ Using existing threads is faster than creating new ones, which improves system
 performance
                                     Threads
▪ Fork Join
▪ The strategy for thread creation is often known as the fork-join model
▪ Fork Join in Java
▪ Java introduced a fork-join library in Version 1.7 of the API that is designed to be
 used with recursive divide-and-conquer algorithms such as Quicksort and
 Mergesort
                                   Threads
▪ Fork Join
▪ Fork Join in Java
▪ The general recursive algorithm behind Java’s fork-join model is shown below:
                      Threads
▪ Fork Join
▪ Fork Join in Java
                                   Threads
▪ OpenMP
▪ OpenMP is a set of compiler directives for parallel programming in C, C++, and
 Fortran
▪ It supports shared memory architectures and defines parallel sections in code
 using directives like #pragma omp parallel
▪ OpenMP can be used with many open-source and commercial compilers on
 Linux, Windows, and Mac OS X
                                     Threads
▪ Threading Issues
▪ The fork() and exec() System Calls
▪ In some Unix systems, there are two types of fork() system calls:
   o One duplicates all threads
   o The other duplicates only the thread that initiated the fork()
▪ The exec() system call duplicates the entire process, including all threads
                                      Threads
▪ Signal Handling
▪ A signal is used in UNIX systems to notify a process that a particular event
 has occurred. A signal may be received either synchronously or asynchronously
 depending on the source of and the reason for the event being signaled
   o Synchronous signals are delivered to the process that caused the event (e.g.,
    illegal memory access or division by zero)
   o Asynchronous signals are delivered to other processes (e.g., when pressing
    <ctrl><C>)
                                        Threads
▪ Signal Handling
▪ Operating systems can send signals to:
   o Only one thread within a process (synchronous)
   o All threads within a process (e.g., <ctrl><C>)
   o Some threads within a process
   o A specific thread that is designated to handle all signals
                                     Threads
▪ Thread Cancellation
▪ A thread can be canceled before it completes its execution. For example, if one
 thread finds the result needed, the other threads can be canceled. When you
 click the stop button while loading a web page, all threads within the process are
 canceled.
▪ There are two types of thread cancellation:
   o Asynchronous Cancellation: One thread immediately terminates another thread.
   o Deferred Cancellation: One thread allows another thread to terminate itself.
                                                  Threads
          Multithreading                             Multitasking                  Multiprocessing
Involves running multiple threads Involves running multiple tasks Involves             running    multiple
within a single process, sharing or processes concurrently on a processes on multiple CPUs
the same memory space.                     single CPU.                       or CPU cores.
                                           Tasks or processes are independent Processes are independent and
Threads    within    a    process   can
                                           and communicate through inter- can run on separate CPUs or
communicate         and   share     data
                                           process   communication    (IPC) cores, often with no need for
easily.
                                           mechanisms.                       direct communication.