COMP ORG_chapter04

Chapter 4: Threads in Operating Systems

Prepared by Silberschatz, Modified by Dr. Yuntong Zhang


4.1 Introduction to Threads

  • Definition of a Thread

    • Basic unit of CPU utilization.

    • Also known as a lightweight process (LWP).

    • Comprises:

      • Thread ID

      • Program counter

      • Register set

      • Stack

    • Shares code, data, and other OS resources with other threads in the same process.


4.2 Motivation for Using Threads

  • Application Scenario: Web Server

    • Solution 1: Single-process server

      • Services one request at a time.

      • Long average waiting time for clients.

    • Solution 2: Forking child processes

      • Each request creates a separate child process.

      • Inefficient due to heavyweight process creation.

    • Solution 3: Multithreaded server

      • One process with multiple threads.

      • Each request handled by a separate thread, improving efficiency.


4.3 Single vs. Multithreaded Processes

  • Single-threaded Process

    • Contains one thread.

  • Multithreaded Process

    • Contains multiple threads.

    • Shares resources more efficiently.


4.4 Benefits of Multithreading

  • Responsiveness

    • Program continues running even if one thread fails.

    • Example: A web browser allows user interaction while loading images in another thread.

  • Resource Sharing

    • Threads share code, data, and files in the same address space.

  • Economy

    • Thread creation is significantly faster than process creation (30 times faster in Solaris 2).

    • Context switching is also faster (5 times faster).

  • Utilization of Multiple Processors

    • Multiple threads can run on multiple CPUs.


4.5 Thread Management Issues

  • Thread Creation and Management

    • Threads are usually created from a single initial thread.

    • Management operations include:

      • Creation

      • Scheduling

      • Destruction

      • Synchronization

  • Management Locations

    • User Space: Managed by applications using libraries.

    • Kernel Space: Managed by the OS, providing more efficiency.


4.6 User Threads

  • Management by User-Level Libraries

    • Examples: POSIX Pthreads, Mach C-threads, Solaris threads, Win32 threads, Java threads.

  • Advantages

    • Fast creation and management.

    • Customizable scheduling algorithms.

    • Portability across different OSs.

  • Disadvantages

    • Blocking system calls can block the entire process.

    • Limited to single CPU usage.


4.7 Kernel Threads

  • Managed by the Kernel

    • Kernel handles creation, scheduling, and management.

  • Advantages

    • Can schedule another thread if one performs a blocking system call.

    • Can utilize multiple CPUs.

  • Disadvantages

    • Slower to create and manage than user threads.

    • Less flexible scheduling policies.

    • Non-portable across different machine types.


4.8 Multithreading Models

  • Combining User and Kernel Threads

    • Many-to-One: Multiple user threads mapped to a single kernel thread.

    • One-to-One: Each user thread maps to a kernel thread.

    • Many-to-Many: Many user threads mapped to a smaller or equal number of kernel threads.


4.9 Many-to-One Model

  • Characteristics

    • Entire process blocks if a thread makes a blocking system call.

    • Cannot run multiple processes in parallel on multiple CPUs.

  • Examples: Solaris Green Threads, GNU Portable Threads.


4.10 One-to-One Model

  • Characteristics

    • Each user-level thread corresponds to a kernel thread.

    • More concurrency but limited number of kernel threads.

  • Examples: Windows 95/98/NT/2000, Linux, Solaris 9 and later.


4.11 Many-to-Many Model

  • Characteristics

    • Allows many user-level threads to be mapped to a smaller or equal number of kernel threads.

    • Enables the OS to create sufficient kernel threads.

  • Examples: Solaris prior to version 9, Windows NT/2000 with ThreadFiber package.


4.12 Thread Pools

  • Concept

    • Pre-creation of a set number of threads at process startup.

    • Threads wait in a pool for work.

  • Benefits

    • Faster request servicing with existing threads.

    • Controlled number of concurrently active threads.

    • Configurable maximum number of concurrent users.


4.13 Conclusion

  • This chapter covered the fundamentals of threads in operating systems, their benefits, management, and various models of implementation. Understanding these concepts is crucial for optimizing performance in concurrent applications.


End of Chapter 4