Chapter 4: Threads

24
Silberschatz, Galvin and Gagne ©2009 perating System Concepts – 8 th Edition, Chapter 4: Threads

description

Chapter 4: Threads. Single and Multithreaded Processes. Benefits. Responsiveness One part of a program can continue running even if another part is blocked Resource Sharing Threads of the same process share the same memory space and resources. Economy - PowerPoint PPT Presentation

Transcript of Chapter 4: Threads

Page 1: Chapter 4:  Threads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition,

Chapter 4: Threads

Page 2: Chapter 4:  Threads

4.2 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Single and Multithreaded Processes

Page 3: Chapter 4:  Threads

4.3 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Benefits

Responsiveness

One part of a program can continue running even if another part is blocked

Resource Sharing

Threads of the same process share the same memory space and resources.

Economy

Much less time consuming to create and manage threads than processes

Solaris 2: creating a process is 30 times slower than creating a thread, context switching is 5 times slower.

Scalability

Each thread can run in parallel on a different processor

Page 4: Chapter 4:  Threads

4.4 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Multithreaded Server (for instance a Web Server)

Example of Use

Page 5: Chapter 4:  Threads

4.5 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Multicore Programming & Multithreading

Multicore systems putting pressure on programmers, challenges include

Dividing activities

Balance

Data splitting

Data dependency

Testing and debugging

Page 6: Chapter 4:  Threads

4.6 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Concurrent Execution on a Single-core System

Page 7: Chapter 4:  Threads

4.7 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Parallel Execution on a Multicore System

Page 8: Chapter 4:  Threads

4.8 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

User Threads

User threads supported above the kernel and managed without kernel support

Thread management done by user-level threads library

Three primary thread libraries:

POSIX Pthreads

Win32 threads

Java threads

Page 9: Chapter 4:  Threads

4.9 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Kernel Threads

Supported by the Kernel

Examples

Windows XP/2000

Solaris

Linux

Tru64 UNIX

Mac OS X

Page 10: Chapter 4:  Threads

4.10 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Multithreading Models

Many-to-One

One-to-One

Many-to-Many

Page 11: Chapter 4:  Threads

4.11 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Many-to-One

Many user-level threads mapped to single kernel thread

Entire process blocks with a thread blocking system call

Examples:

Solaris Green Threads

GNU Portable Threads

Page 12: Chapter 4:  Threads

4.12 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

One-to-One

Each user-level thread maps to kernel thread

Creation of a user thread requires creation of a kernel thread

Examples

Windows NT/XP/2000

Linux

Solaris 9 and later

Page 13: Chapter 4:  Threads

4.13 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Many-to-Many Model

Allows many user level threads to be mapped to many kernel threads (smaller or equal number)

Allows the operating system to create a sufficient number of kernel threads

Solaris prior to version 9

Windows NT/2000 with the ThreadFiber package

Page 14: Chapter 4:  Threads

4.14 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Many-to-Many Model

Page 15: Chapter 4:  Threads

4.15 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Two-level Model

Similar to M:M, except that it also allows a user thread to be bound to kernel thread

Examples

IRIX

HP-UX

Tru64 UNIX

Solaris 8 and earlier

Page 16: Chapter 4:  Threads

4.16 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Two-level Model

Page 17: Chapter 4:  Threads

4.17 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

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

Page 18: Chapter 4:  Threads

4.18 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Pthreads

Refers to the POSIX standard (IEEE 1003.1c) API for thread creation and synchronization

May be provided either as user-level or kernel-level

POSIX standard specifies behavior of the thread library; Implementation is up to development of the library

Common in UNIX operating systems (Solaris, Linux, Mac OS X)

Page 19: Chapter 4:  Threads

4.19 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Win32 Threads

Also known as Windows API or WinAPI

Win32 is a kernel-level library

Available on Windows systems (Windows 95, 98, NT, 2000 and XP)

Page 20: Chapter 4:  Threads

4.20 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Windows XP Threads

Implements the one-to-one mapping, kernel-level

Each thread contains

A thread id

Register set

user stack or kernel stack

Private data storage area

The register set, stacks, and private storage area are known as the context of the threads

The primary data structures of a thread include:

ETHREAD (executive thread block)

KTHREAD (kernel thread block)

TEB (thread environment block)

Page 21: Chapter 4:  Threads

4.21 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Windows XP Threads

Page 22: Chapter 4:  Threads

4.22 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Linux Threads

Linux refers to them as tasks rather than threads

Thread creation is done through clone() system call

Flags, arguments of clone() specify sharing details

Page 23: Chapter 4:  Threads

4.23 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Linux Threads

If CLONE_FS, CLONE_VM, CLONE_SIGHAND, and CLONE_FILES are passed to clone(), parent and child tasks share every thing

If none of these flags are passed to clone(), no sharing takes place, resulting in functionality similar to that of fork()

Page 24: Chapter 4:  Threads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition,

End of Chapter 4