Chapter 5: Threads Single and Multithreaded Processes

5 downloads 75 Views 327KB Size Report
1. Silberschatz, Galvin, Gagne ©2002 (with gcw mods). 5.1. Operating System Concepts. Chapter 5: Threads. ▫ Overview. ▫ Multithreading Models.
Chapter 5: Threads

n Overview n Multithreading Models n Threading Issues n Pthreads n Solaris 2 Threads n Windows 2000 Threads n Linux Threads n Java Threads

Operating System Concepts

5.1

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

Single and Multithreaded Processes

Operating System Concepts

5.2

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

1

Benefits

n Responsiveness n Resource Sharing n Economy n Utilization of MP Architectures

Operating System Concepts

5.3

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

User Threads

n Thread management done by user-level threads library n Examples

- POSIX Pthreads - Mach C-threads - Solaris threads

Operating System Concepts

5.4

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

2

Kernel Threads

n Supported by the Kernel n Examples

- Windows 95/98/NT/2000 - Solaris - Tru64 UNIX - BeOS - Linux

Operating System Concepts

5.5

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

Multithreading Models

n Many-to-One n One-to-One n Many-to-Many

Operating System Concepts

5.6

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

3

Many-to-One

n Many user-level threads mapped to single kernel thread. n Used on systems that do not support kernel threads.

Operating System Concepts

5.7

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

Many-to-One Model

Operating System Concepts

5.8

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

4

One-to-One

n Each user-level thread maps to kernel thread. n Examples

- Windows 95/98/NT/2000 - OS/2

Operating System Concepts

5.9

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

One-to-one Model

Operating System Concepts

5.10

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

5

Many-to-Many Model

n Allows many user level threads to be mapped to many

kernel threads. n Allows the operating system to create a sufficient number of kernel threads. n Solaris 2 n Windows NT/2000 with the ThreadFiber package

Operating System Concepts

5.11

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

Many-to-Many Model

Operating System Concepts

5.12

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

6

Threading Issues

n Semantics of fork() and exec() system calls. n Thread cancellation. n Signal handling n Thread pools n Thread specific data

Operating System Concepts

5.13

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

Pthreads

n a POSIX standard (IEEE 1003.1c) API for thread creation

and synchronization. n API specifies behavior of the thread library,

implementation is up to development of the library. n Common in UNIX operating systems.

Operating System Concepts

5.14

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

7

Solaris 2 Threads

Operating System Concepts

5.15

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

Solaris Process

Operating System Concepts

5.16

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

8

Windows 2000 Threads

n Implements the one-to-one mapping. n Each thread contains

- a thread id - register set - separate user and kernel stacks - private data storage area

Operating System Concepts

5.17

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

Linux Threads

n Linux refers to them as tasks rather than threads. n Thread creation is done through clone() system call. n Clone() allows a child task to share the address space of

the parent task (process)

Operating System Concepts

5.18

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

9

Creating thread in Windows

HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes DWORD dwStackSize, // initial thread stack size LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function LPVOID lpParameter, // argument for new thread DWORD dwCreationFlags, // creation flags LPDWORD lpThreadId // pointer to receive thread ID );

Operating System Concepts

5.19

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

Thread creation example

Int threadNumber; DWORD id; //DWORD == unsigned long const HANDLE h = CreateThread(0, //default security 0, //default the stack size Greeter, //our function &threadNumber, //void * to arbitrary user information 0, //no flags &id);

Operating System Concepts

5.20

Silberschatz, Galvin, Gagne 2002 (with gcw mods)

10

Thread function

DWORD WINAPI Greeter(void *arg) { const int *pval = static_cast(arg); const int & val = *pval; cout