Figure 5.01 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Figure 5.01

Description:

Operating Systems Lecture 13 Threads Read Ch 5.1 - 5.3 Threads A thread (a lightweight process) is a basic unit of CPU utilization. A thread has a single sequential ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 19
Provided by: Marily564
Category:

less

Transcript and Presenter's Notes

Title: Figure 5.01


1
Operating SystemsLecture 13 Threads Read Ch 5.1
- 5.3
2
Threads
  • A thread (a lightweight process) is a basic unit
    of CPU utilization.
  • A thread has a single sequential flow of control.
  • A thread is comprised of A thread ID, a program
    counter, a register set and a stack.
  • A process is the execution environment in which
    threads run.
  • (Recall previous definition of process program
    in execution).
  • The process has the code section, data section,
    OS resources (e.g. open files and signals).
  • Traditional processes have a single thread of
    control
  • Multi-threaded processes have multiple threads of
    control
  • The threads share the address space and resources
    of the process that owns them.

3
Single and Multithreaded Processes
4
Processes vs. Threads
Which of the following belong to the process and
which to the thread?
Process Thread Process Process Thread Process Thre
ad Process Thread Thread
Program code local or temporary data global
data allocated resources execution
stack memory management info Program
counter Parent identification Thread
state Registers
5
Control Blocks
The thread control block (TCB) contains Thread
state, Program Counter, Registers PCB'
everything else (e.g. process id, open files,
etc.) The process control block (PCB) PCB' U
TCP
6
Why use threads?
  • Because threads have minimal internal state, it
    takes less time to create a thread than a process
    (10x speedup in UNIX).
  • It takes less time to terminate a thread.
  • It takes less time to switch to a different
    thread.
  • A multi-threaded process is much cheaper than
    multiple (redundant) processes.

7
Examples of Using Threads
  • Threads are useful for any application with
    multiple tasks that can be run with separate
    threads of control.
  • A Word processor may have separate threads for
  • User input
  • Spell check
  • grammar check
  • displaying graphics
  • document layout
  • A web server may spawn a thread for each client
  • Can serve clients concurrently with multiple
    threads.
  • It takes less overhead to use multiple threads
    than to use multiple processes.

8
Benefits
  • Responsiveness
  • Threads allow a program to continue running even
    if part is blocked.
  • For example, a web browser can allow user input
    while loading an image.
  • Resource Sharing
  • Threads share memory and resources of the process
    to which they belong.
  • Economy
  • Allocating memory and resources to a process is
    costly.
  • Threads are faster to create and faster to switch
    between.
  • Utilization of Multiprocessor Architectures
  • Threads can run in parallel on different
    processors.
  • A single threaded process can run only on one
    processor no matter how many are available.

9
User Threads
  • Thread management may be done at either the user
    level or the kernel (OS) level.
  • User Threads
  • Thread management done by user-level threads
    library
  • The kernel is unaware that the process is
    multithreaded.
  • Thread creation and scheduling is done in user
    space without kernel intervention.

10
Many-to-One Model
11
States of User Threads and Processes
  • What happens when a process or thread is blocked?
  • Suppose process B has two threads, of which
    thread 2 is running. The following conditions
    could occur
  • Thread 2 makes an I/O or other system call
  • The kernel puts process B into a blocked state.
  • The thread data structure will still show thread
    2 in the running state.
  • Thread 2 is not actually running. (The entire
    process is blocked).
  • Process B exhausts its time slice
  • The kernel puts process B into the ready state.
  • Thread 2 still in running state (but not actually
    running!)
  • Thread 2 needs action performed by Thread 1
  • Thread 2 goes into blocked state.
  • Thread 1 starts running.
  • Process B remains in running state.

12
Pros and Cons of User Level Threads
  • Pros
  • Fast switch between threads (kernel is not
    needed).
  • Can have multi-threaded process on a system that
    does not understand threads.
  • Cheap to create and destroy threads.
  • User has complete control over threads (e.g. can
    assign priorities, etc.)
  • Cons
  • System call from one thread blocks all threads in
    the same process.
  • Process cannot use multiple processors.
  • Extra work for user.

13
User Level Thread Libraries
  • User Level Thread Libraries include
  • POSIX Pthreads
  • Mach C-Threads
  • Solaris-2 UI threads.

14
Kernel Level Threads
  • Kernel Level Threads
  • All thread management is done by the Operating
    System.
  • Each user-level thread maps to kernel thread.
  • Kernel does creation, scheduling and management
    of threads.
  • Examples
  • - Windows 95/98/NT/2000
  • - OS/2
  • - Solaris-2
  • - Some flavors of UNIX

15
One-to-one Model
16
Pros and Cons of Kernel Level Threads
  • Pros
  • System call from a thread does not block other
    threads in the same process.
  • One process can use multiple processors.
  • Create/destroy/switch of threads is less
    expensive than for processes.
  • Cons
  • Create/destroy/switch of threads is more
    expensive than for user level threads.
  • CPU scheduling algorithms are unfair Each
    thread is given the same time slice. Tasks with
    more threads are given more CPU time than those
    with fewer threads.

17
Combining User and Kernel Level Threads
  • User and Kernel level threads execute
    simultaneously.
  • Allows many user level threads to be mapped to
    many kernel threads.
  • If one user level thread is blocked, another
    kernel thread may be chosen to execute.
  • Assigning a User Level thread to a Kernel Level
    thread is implicit and hidden from the
    programmer.
  • Examples of systems with combination of User and
    Kernel Level threads
  • Solaris 2
  • Windows NT/2000 with the ThreadFiber package
  • Some other UNIX flavors.

18
Many-to-Many Model
Write a Comment
User Comments (0)
About PowerShow.com