chapter 4: threads

14
Chapter 4: Threads Chapter 4: Threads

Upload: rae-fields

Post on 04-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

Chapter 4: Threads. Typical Program. Processor utilization?. Without Kernel Support. Why discuss in an OS course Without kernel support: Responsibility on the process Requires a thread manager compiled as part of each multithreaded process Appears single-threaded to OS - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 4:  Threads

Chapter 4: ThreadsChapter 4: Threads

Page 2: Chapter 4:  Threads

4.2 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

Typical ProgramTypical Program

Processor utilization?

Page 3: Chapter 4:  Threads

4.6 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

Without Kernel SupportWithout Kernel Support

Why discuss in an OS course

Without kernel support:

Responsibility on the process

Requires a thread manager

compiled as part of each multithreaded process

Appears single-threaded to OS

Responsibilities of thread manager

Primary control of the process

Determines

– Which thread to execute

– When to switch to another thread

Thread ManagerThread Manager

Threads

Operating SystemOperating System

Page 4: Chapter 4:  Threads

4.7 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

Many-to-One ModelMany-to-One Model

Conceptually, no kernel support is many-to-one

Many user-level threads mapped to single kernel thread

Examples:

Solaris Green Threads

GNU Portable Threads

Known as User Level Threading

Page 5: Chapter 4:  Threads

4.8 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

Drawback of User LevelDrawback of User Level

When an application requests I/O it is put in the waiting queue

Even if thread manager suspends a thread just before an I/O request once the TM allows a single I/O request the entire process gets suspended

A single I/O request from any thread causes the entire application

to be suspended

Page 6: Chapter 4:  Threads

4.9 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

Kernel SupportKernel Support

Thread-aware kernel

Can avoid suspending a multi-threaded app on I/O

When I/O complete

Determines if process is running

Informs process of I/O completion

Thread aware kernel allows for more efficient use of resources particularly for

high I/O applications

Page 7: Chapter 4:  Threads

4.10 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

Kernel ThreadsKernel Threads

Idea of threads has even been extended to the OS, itself

Thread models

Many-to-One

One-to-One

Many-to-Many

OS’s that support kernel threads

Windows XP/2000

Solaris

Linux

Tru64 UNIX

Mac OS X

Page 8: Chapter 4:  Threads

4.11 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

One-to-OneOne-to-One

Each user-level thread maps to kernel thread

Examples

Windows NT/XP/2000

Linux

Solaris 9 and later

Drawback: must generate a kernel thread for every user thread

Page 9: Chapter 4:  Threads

4.12 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

Many-to-Many ModelMany-to-Many Model Can generate a kernel thread for each logical task

OS’s that support it

Solaris prior to version 9

Windows NT/2000 with the ThreadFiber package

Page 10: Chapter 4:  Threads

4.13 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

Two-level ModelTwo-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

Page 11: Chapter 4:  Threads

4.14 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

Thread LibrariesThread Libraries

Three primary thread libraries:

POSIX Pthreads

Available with and without kernel

Win32 threads

Kernel support

Java threads

Kernel support depends upon host system

JVM written and compiled for each host

Page 12: Chapter 4:  Threads

4.15 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

ExampleExample

Posix threads example

#include <pthread.h>

pthread_t myThread;

pthread_create( &myThread, pointerToFunction);

pthread_join( myThread);

#include <pthread.h>

pthread_t myThread;

pthread_create( &myThread, pointerToFunction);

pthread_join( myThread);

Page 13: Chapter 4:  Threads

4.16 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th edition, Jan 23, 2005

Parallel calculation exampleParallel calculation example

Create an array of threads

Put jobs in a list

Create a thread for each job

Each thread watches a list of available servers

When a server becomes available, takes ownership of the server, and sends the job to the server

When complete, thread puts job in completed list, and puts server in available server list

Pen

ding

Job

s

Se

rve

rs

Com

plet

ed J

obs

Page 14: Chapter 4:  Threads

End of Chapter 4End of Chapter 4