0% found this document useful (0 votes)
32 views45 pages

Lecture Thread

This document discusses threads and multithreaded programming. It begins by defining a thread as a sequential execution stream within a process that shares the same address space. It then covers multithreading models like many-to-one, one-to-one, and many-to-many. The document also discusses thread libraries like Pthreads, Win32 threads, and Java threads. Finally, it examines issues like signal handling, thread cancellation, and thread pools in multithreaded programming.

Uploaded by

Mohamad Yassine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
32 views45 pages

Lecture Thread

This document discusses threads and multithreaded programming. It begins by defining a thread as a sequential execution stream within a process that shares the same address space. It then covers multithreading models like many-to-one, one-to-one, and many-to-many. The document also discusses thread libraries like Pthreads, Win32 threads, and Java threads. Finally, it examines issues like signal handling, thread cancellation, and thread pools in multithreaded programming.

Uploaded by

Mohamad Yassine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 45

Threads

Dr. Mageda Sharafeddin


Chapter 4: Threads
• Overview
• Multithreading Models
• Thread Libraries
• Threading Issues
• Operating System Examples
• Windows XP Threads
• Linux Threads
Objectives
• To introduce the notion of a thread — a fundamental unit of CPU
utilization that forms the basis of multithreaded computer systems
• To discuss the APIs for the Pthreads, Win32, and Java thread libraries
• To examine issues related to multithreaded programming
Single and Multithreaded Processes

A web browser might have one


thread display images or text while
another thread retrieves data from
the network, for example. A word
processor may have a thread for
displaying graphics, another thread
for responding to keystrokes from
the user, and a third thread for
performing spelling and grammar
checking in the background.
Benefits of Multi-Threaded Programming

• Responsiveness

• Resource Sharing

• Economy

• Scalability
Multicore Programming
• Multicore systems putting pressure on programmers, challenges include
• Dividing activities
• Balance
• Data splitting
• Data dependency
• Testing and debugging
Multithreaded Server Architecture
Concurrent Execution on a Single-core System
Parallel vs Concurrency Execution on a Multicore System

A system is parallel if it can perform more than one task simultaneously: T1, T2, T3, T4.
In contrast, a concurrent system supports more than one task by allowing the tasks to
make progress: T1 & T2 then T3 & T4, ….
Amdahl’s Law

• S is the portion of the application that must be performed serially on a system with N
processing cores.
• 75 percent parallel and 25 percent serial. If we run this application on a system with two
processing cores, we can get a speedup of 1.6 times. If we add two additional cores (for a
total of four), the speedup is 2.28 times.
• This is the fundamental principle behind Amdahl’s Law: the serial portion of an application
can have a disproportionate effect on the performance we gain by adding additional
computing cores.
Programming Challenges
• Identifying tasks.
• Balance.
• Data splitting.
• Data dependency: task execution must be synchronized
• Testing and debugging: testing concurrent programs is inherently more
difficult
• Data parallelism versus Task parallelism
Before Threads…
• Recall that a process consists of:
• program(s)
• data
• stack
• PCB
• all stored in the process image
• Process (context) switch is pure overhead
Process Characterization
• Resource ownership
• address space to hold process image
• I/O devices, files, etc.
• Execution
• a single execution path (thread of control)
• execution state, PC & registers, stack
Refining Terminology
• Distinguish the two characteristics
• Process: resource ownership
• Thread: unit of execution (dispatching) - AKA lightweight process (LWP)
• Multi-threading: support multiple threads of execution within a single
process
• Process, as we have known it thus far, is a single-threaded process
Threads and Processes
• Decouple the resource allocation aspect from the control aspect
• Thread abstraction - defines a single sequential instruction stream (PC,
stack, register values)
• Process - the resource context serving as a “container” for one or more
threads (shared address space)
Threads
• A sequential execution stream within a process (also called lightweight
process)
• Threads in a process share the same address space
• Easier to program I/O overlapping with threads than signals
• Responsive user interface
• Run some program activities “in the background”
• Multiple CPUs sharing the same memory
Threads and Processes
• Thread abstraction - defines a single sequential instruction stream (PC,
stack, register values)
• Process - the resource context serving as a “container” for one or more
threads (shared address space)
Process vs. Threads
• Processes do not usually share memory
• Process context switch changes page table and other memory
mechanisms
• Threads in a process share the entire address space
• Processes have their own privileges (file accesses, e.g.)
• Threads in a process share all privileges
• Threads have each a stack and a register set
Example
Thread Control Block (TCB)

• State : Ready: ready to run, Running: currently running,


Blocked: waiting for resources
• Registers, Status (EFLAGS), Program counter (EIP),
Stack, Code
User Threads
• Thread management done by user-level threads library

• Three primary thread libraries:


• POSIX Pthreads
• Win32 threads
• Java threads
Kernel Threads
• Supported by the Kernel

• Examples
• Windows XP/2000
• Solaris
• Linux
• Tru64 UNIX
• Mac OS X
User-Level Threads Advantages

• Thread switch does not require kernel privileges


• Thread switch more efficient than kernel call
• Scheduling can be process (app) specific without
disturbing OS: can run on any OS, scales easily
Kernel-Level Threads
• Thread management done by kernel
• Process as a whole (process table)
• Individual threads (thread table)
• Kernel schedules on a per-thread basis
• Most kernels are now multithreaded: managing devices,
managing memory, or interrupt handling.
Kernel-Level Threads
Addresses disadvantages of ULT:
• Schedule multi threads from one process on multiple CPUs
• If one thread blocks, schedule another (no process switch)
Disadvantage of KLT:
• Thread switch causes mode switch to kernel
Multithreading Models
• Many-to-One

• One-to-One

• Many-to-Many
Many-to-One
• Many user-level threads mapped to single kernel thread
• Examples:
• Solaris Green Threads
• GNU Portable Threads
Many-to-One Model
One-to-One
• Each user-level thread maps to kernel thread
• Examples
• Windows NT/XP/2000
• Linux
• Solaris 9 and later
One-to-one Model
Many-to-Many Model

• Allows many user level threads to be mapped to many kernel threads


• Allows the operating system to create a sufficient number of kernel threads
• Solaris prior to version 9
• Windows NT/2000 with the ThreadFiber package
Many-to-Many 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
Two-level Model
Thread Libraries
• Thread library provides programmer with API for creating and managing
threads
• Two primary ways of implementing
• Library entirely in user space
• Kernel-level library supported by the OS
Pthreads
• May be provided either as user-level or kernel-level
• A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
• 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)
Java Threads

• Java threads are managed by the JVM


• Typically implemented using the threads model provided by underlying OS
• Java threads may be created by:
• Extending Thread class
• Implementing the Runnable interface
Threading Issues
• Semantics of fork() and exec() system calls
• Thread cancellation of target thread
• Asynchronous or deferred
• Signal handling
• Thread pools
• Thread-specific data
• Scheduler activations
Thread Cancellation

• Terminating a thread before it has finished


• Two general approaches:
• Asynchronous cancellation terminates the target thread immediately
• Deferred cancellation allows the target thread to periodically check if it
should be cancelled
Signal Handling
• Signals are used in UNIX systems to notify a process that a particular event has occurred
• A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled
• Options:
• Deliver the signal to the thread to which the signal applies
• Deliver the signal to every thread in the process
• Deliver the signal to certain threads in the process
• Assign a specific thread to receive all signals for the process
Thread Pools

• Create a number of threads in a pool where they await for work


• Advantages:
• Usually slightly faster to service a request with an existing thread than create a new thread
• Allows the number of threads in the application(s) to be bound to the size of the pool
Scheduler Activations
• Both M:M and Two-level models require communication
to maintain the appropriate number of kernel threads
allocated to the application
• Scheduler activations provide upcalls - a communication
mechanism from the kernel to the thread library
• This communication allows an application to maintain the
correct number kernel threads
Linux Threads

• Linux refers to them as tasks rather than threads

• Thread creation could be done through clone() system call

• clone() allows a child task to share the address space of the


parent task (process)
Linux Threads
Summary

• Concurrency: CPU and I/O, Among applications, Within


an application
• Processes: Abstraction for application concurrency
• Threads : Abstraction for concurrency within an
application

You might also like