Chapter 4 threads
Download
1 / 56

Chapter 4 Threads - PowerPoint PPT Presentation


  • 65 Views
  • Uploaded on
  • Presentation posted in: General

Operating Systems: Internals and Design Principles. Chapter 4 Threads. Seventh Edition By William Stallings. The basic idea is that the several components in any complex system will perform particular subfunctions that contribute to the overall function. — THE SCIENCES OF THE ARTIFICIAL,

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha

Download Presentation

Chapter 4 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 4 threads

Operating Systems:Internals and Design Principles

Chapter 4Threads

Seventh Edition

By William Stallings


Operating systems internals and design principles

The basic idea is that the several components in any complex system will perform particular subfunctions that contribute to the overall function.

—THE SCIENCES OF THE ARTIFICIAL,

Herbert Simon

Operating Systems:Internals and Design Principles


Processes and threads
Processes and Threads system will perform particular subfunctions that contribute to the overall function.

Resource Ownership

Execution Path

Traditional processes have two characteristics:

  • Virtual address space, files, shared memory, …

  • the OS provides protection to prevent unwanted interference between processes with respect to resources

  • Follows an execution path (trace) that may be interleaved with other processes

  • a process has an execution state (Running, Ready, etc.) and a dispatching priority and is scheduled and dispatched by the OS

  • Traditional processes were sequential; i.e. only one execution path


Processes and threads1
Processes and Threads system will perform particular subfunctions that contribute to the overall function.

  • Multithreading - The ability of an OS to support multiple, concurrent paths of execution within a single process

  • The unit of resource ownership is referred to as a process or task

  • The unit of dispatching is referred to as a thread or lightweight process


Single threaded approaches
Single Threaded Approaches system will perform particular subfunctions that contribute to the overall function.

A single execution path per process, in which the concept of a thread is not recognized, is referred to as a single-threaded approach

MS-DOS, some versions of UNIX supported only this type of process.


Multithreaded approaches
Multithreaded Approaches system will perform particular subfunctions that contribute to the overall function.

The right half of Figure 4.1 depicts multithreaded approaches

A Java run-time environment is a system of one process with multiple threads; Windows, some UNIXes, also support multiple multithreaded processes.


Multithreaded processes
Multithreaded Processes system will perform particular subfunctions that contribute to the overall function.

  • Multithreaded processes separate the concept of the process from the concept of its executable threads

  • The process owns resources and the OS provides protection at the process level

  • Processes have

    • A virtual address space that holds the process image

    • Protected access to

    • processors

    • other processes

    • Files & physical memory

    • I/O resources


One or more threads in a process
One or More Threads system will perform particular subfunctions that contribute to the overall function.in a Process


Threads vs processes
Threads vs. Processes system will perform particular subfunctions that contribute to the overall function.


Benefits of threads
Benefits of Threads system will perform particular subfunctions that contribute to the overall function.


Thread use in a single user system
Thread Use in a system will perform particular subfunctions that contribute to the overall function.Single-User System

  • Foreground and background work

  • Asynchronous processing

  • Speed of execution

  • Modular program structure


Threads
Threads system will perform particular subfunctions that contribute to the overall function.

  • In an OS that supports threads, scheduling and dispatching is done on a thread basis

  • suspending a process involves suspending all threads of the process

  • termination of a process terminates all threads within the process


Thread execution states
Thread Execution States system will perform particular subfunctions that contribute to the overall function.

Thread operations associated with a change in thread state are:

  • Spawn (create)

  • Block

  • Unblock

  • Terminate

The key states for

a thread are:

  • Running

  • Ready

  • Blocked


Thread execution
Thread Execution system will perform particular subfunctions that contribute to the overall function.

  • So far, we have assumed that threads can be scheduled independently of the process to which they belong.

    • In other words, it is possible to block one thread in a process without blocking the entire process.

  • Examples:


Rpc using single thread
RPC Using Single Thread system will perform particular subfunctions that contribute to the overall function.


Rpc using one thread per server
RPC Using One system will perform particular subfunctions that contribute to the overall function.Thread per Server


Multithreading on a uniprocessor
Multithreading on a Uniprocessor system will perform particular subfunctions that contribute to the overall function.


Thread synchronization
Thread Synchronization system will perform particular subfunctions that contribute to the overall function.

  • The threads of a process share the same address space and other resources

    • any alteration of a shared resource by one thread affects the other threads in the same process

    • Threads share global data, but each has its own stack with its own private data

    • Access to shared resources (memory, files, …) must be synchronized for correct results.


Other types of threads
Other Types of Threads system will perform particular subfunctions that contribute to the overall function.

  • Separate thread scheduling must be supported at the OS level

    • Some operating systems don’t provide the capability

  • Some programs don’t need the overhead of having threads that are managed by the kernel.

  • So, another type of thread …


Types of threads
Types of Threads system will perform particular subfunctions that contribute to the overall function.

NOTE: we are talking about threads for user processes. Both ULT & KLT execute in user mode. An OS may also have threads but that is not what we are discussing here.


User level threads ults
User-Level Threads (ULTs) system will perform particular subfunctions that contribute to the overall function.

Thread management is done by the application

The kernel is not even aware of the existence of threads

OS schedules the process, process decides which thread(s) to run


Relationships between ult states and process states
Relationships Between ULT system will perform particular subfunctions that contribute to the overall function.States and Process States

Possible transitions from 4.6a:

4.6a→4.6b

4.6a→4.6c

4.6a→4.6d

Figure 4.6 Examples of the Relationships between User-Level Thread States and Process States


Advantages of ults
Advantages of ULTs system will perform particular subfunctions that contribute to the overall function.


Disadvantages of ults
Disadvantages of ULTs system will perform particular subfunctions that contribute to the overall function.

  • In a typical OS many system calls are blocking

    • as a result, when a ULT executes a system call, not only is that thread blocked, but all of the threads within the process are blocked

  • In a pure ULT strategy, a multithreaded application cannot take advantage of multiprocessing


  • Overcoming ult disadvantages
    Overcoming ULT Disadvantages system will perform particular subfunctions that contribute to the overall function.


    Kernel level threads klts
    Kernel-Level Threads (KLTs) system will perform particular subfunctions that contribute to the overall function.

    • Thread management is done by the kernel (could call them KMT)

      • no thread management is done by the application

    • Windows supports KLT

  • KLT do NOT have kernel level privileges, however


  • Advantages of klts compared to ult
    Advantages of KLTs system will perform particular subfunctions that contribute to the overall function.(compared to ULT)

    In an SMP, the kernel can simultaneously schedule multiple threads from the same process on multiple processors

    If one thread in a process is blocked, the kernel can schedule another thread of the same process (asynchronous processing)

    Background/foreground processing

    Modular program development.


    Disadvantage of klts
    Disadvantage of KLTs system will perform particular subfunctions that contribute to the overall function.

    • The transfer of control from one thread to another within the same process requires a mode switch to the kernel


    Combined approaches
    Combined Approaches system will perform particular subfunctions that contribute to the overall function.

    Thread creation is done in the user space

    Bulk of scheduling and synchronization of threads is by the application

    Solaris was an example


    Relationship between threads and processes
    Relationship Between system will perform particular subfunctions that contribute to the overall function.Threads and Processes

    Table 4.2 Relationship between Threads and Processes


    Multiple cores multithreading
    Multiple Cores & Multithreading system will perform particular subfunctions that contribute to the overall function.

    • Multithreading and multicore chips have the potential to improve performance of applications that have large amounts of parallelism

    • Gaming, simulations, etc. are examples

    • Performance doesn’t necessarily scale linearly with the number of cores …


    Amdahl s law
    Amdahl’s Law system will perform particular subfunctions that contribute to the overall function.

    • Speedup is limited by the amount of code that must be executed sequentially

    • Formula:

      • Speedup = time to run on single processor

      • time to execute on N || processors

      • 1

      • = (1 – f ) + f / N

      • (where f is the amount of parallelizable code)


    Performance effect of multiple cores
    Performance Effect system will perform particular subfunctions that contribute to the overall function.of Multiple Cores

    Figure 4.7 (a)

    Figure 4.7 (b)


    Database workloads on multiple processor hardware
    Database Workloads on system will perform particular subfunctions that contribute to the overall function.Multiple-Processor Hardware

    Figure 4.8 Scaling of Database Workloads on Multiple Processor Hardware


    Applications that benefit mcdo06
    Applications That Benefit system will perform particular subfunctions that contribute to the overall function.[MCDO06]

    • Multithreaded native applications

      • characterized by having a small number of highly threaded processes

  • Multiprocess applications

    • characterized by the presence of many single-threaded processes

  • Java applications

  • Multi-instance applications

    • multiple instances of the application in parallel


  • Windows processes
    Windows Processes system will perform particular subfunctions that contribute to the overall function.


    Relationship between process and resource
    Relationship Between system will perform particular subfunctions that contribute to the overall function.Process and Resource

    Figure 4.10 A Windows Process and Its Resources


    Process and thread objects
    Process and Thread system will perform particular subfunctions that contribute to the overall function.Objects

    Windows makes use of two types of process-related objects:


    Windows process and thread objects
    Windows Process and system will perform particular subfunctions that contribute to the overall function.Thread Objects


    Multithreaded process
    Multithreaded Process system will perform particular subfunctions that contribute to the overall function.


    Thread states
    Thread States system will perform particular subfunctions that contribute to the overall function.

    Figure 4.12 Windows Thread States


    Symmetric multiprocessing support smp
    Symmetric Multiprocessing Support (SMP) system will perform particular subfunctions that contribute to the overall function.


    Solaris process
    Solaris Process system will perform particular subfunctions that contribute to the overall function.

    • - makes use of four thread-related concepts:


    Processes and threads in solaris
    Processes and Threads system will perform particular subfunctions that contribute to the overall function. in Solaris

    Figure 4.13 Processes and Threads in Solaris [MCDO07]


    Traditional unix vs solaris
    Traditional Unix vs Solaris system will perform particular subfunctions that contribute to the overall function.

    Figure 4.14 Process Structure in Traditional UNIX and Solaris [LEWI96]


    A lightweight process lwp data structure includes
    A Lightweight Process (LWP) Data Structure Includes: system will perform particular subfunctions that contribute to the overall function.

    An LWP identifier

    The priority of this LWP

    A signal mask

    Saved values of user-level registers

    The kernel stack for this LWP

    Resource usage and profiling data

    Pointer to the corresponding kernel thread

    Pointer to the process structure


    Solaris thread states
    Solaris Thread States system will perform particular subfunctions that contribute to the overall function.

    This figure has typos – the bottom 3 states should be Stop, Zombie, & Free

    Figure 4.15 Solaris Thread States


    Interrupts as threads
    Interrupts as Threads system will perform particular subfunctions that contribute to the overall function.

    • Most operating systems contain two fundamental forms of concurrent activity:


    Solaris solution
    Solaris Solution system will perform particular subfunctions that contribute to the overall function.

    • Solaris employs a set of kernel threads to handle interrupts

      • an interrupt thread has its own identifier, priority, context, and stack

      • the kernel controls access to data structures and synchronizes among interrupt threads using mutual exclusion primitives

      • interrupt threads are assigned higher priorities than all other types of kernel threads


    Linux tasks
    Linux Tasks system will perform particular subfunctions that contribute to the overall function.


    Linux process thread model
    Linux system will perform particular subfunctions that contribute to the overall function.Process/Thread Model

    Figure 4.16 Linux Process/Thread Model


    Linux threads
    Linux Threads system will perform particular subfunctions that contribute to the overall function.


    Linux clone flags
    Linux system will perform particular subfunctions that contribute to the overall function.Clone () Flags


    Mac os x grand central dispatch gcd
    Mac OS X Grand Central Dispatch (GCD) system will perform particular subfunctions that contribute to the overall function.

    Provides a pool of available threads

    Designers can designate portions of applications, called blocks, that can be dispatched independently and run concurrently

    Concurrency is based on the number of cores available and the thread capacity of the system


    Block
    Block system will perform particular subfunctions that contribute to the overall function.

    • A simple extension to a language

    • A block defines a self-contained unit of work

    • Enables the programmer to encapsulate complex functions

    • Scheduled and dispatched by queues

    • Dispatched on a first-in-first-out basis

    • Can be associated with an event source, such as a timer, network socket, or file descriptor


    Summary
    Summary system will perform particular subfunctions that contribute to the overall function.

    • User-level threads

      • created and managed by a threads library that runs in the user space of a process

      • a mode switch is not required to switch from one thread to another

      • only a single user-level thread within a process can execute at a time

      • if one thread blocks, the entire process is blocked

    • Kernel-level threads

      • threads within a process that are maintained by the kernel

      • a mode switch is required to switch from one thread to another

      • multiple threads within the same process can execute in parallel on a multiprocessor

      • blocking of a thread does not block the entire process

    • Process/related to resource ownership

    • Thread/related to program execution


    ad
  • Login