operating system principles

28
Operating System Operating System Principles Principles Ku-Yaw Chang Ku-Yaw Chang [email protected] [email protected] Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information Engineering Computer Science and Information Engineering Da-Yeh University Da-Yeh University

Upload: xantha-beck

Post on 03-Jan-2016

38 views

Category:

Documents


2 download

DESCRIPTION

Operating System Principles. Ku-Yaw Chang [email protected] Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University. Chapter 4 Multithreaded Programming. A process With a single thread of control With multiple threads of control - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Operating System Principles

Operating System Operating System Principles Principles

Ku-Yaw ChangKu-Yaw [email protected]@mail.dyu.edu.tw

Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information EngineeringComputer Science and Information Engineering

Da-Yeh UniversityDa-Yeh University

Page 2: Operating System Principles

22Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

Chapter 4Chapter 4Multithreaded ProgrammingMultithreaded Programming

A processA process With a single thread of controlWith a single thread of control With multiple threads of controlWith multiple threads of control

Multithreaded computer systemsMultithreaded computer systemsPthreadsPthreadsWindows 32 threadsWindows 32 threadsJava thread librariesJava thread libraries

Windows XP and LinuxWindows XP and Linux Support at the kernel levelSupport at the kernel level

Page 3: Operating System Principles

33Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

Chapter 4Chapter 4Multithreaded ProgrammingMultithreaded Programming

1.1. OverviewOverview

2.2. Multithreading ModelsMultithreading Models

3.3. Thread LibrariesThread Libraries

4.4. Threading IssuesThreading Issues

5.5. Operating-System ExamplesOperating-System Examples

6.6. SummarySummary

7.7. ExercisesExercises

Page 4: Operating System Principles

44Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.1 Overview4.1 Overview

A threadA thread A basic unit of CPU utilizationA basic unit of CPU utilization

A thread IDA thread ID

A program counterA program counter

A register setA register set

A stackA stack Called Called a lightweight process (LWP)a lightweight process (LWP)

A traditional process has a single thread of control, A traditional process has a single thread of control, called called a heavyweight process (HWP)a heavyweight process (HWP)

Page 5: Operating System Principles

55Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

Single-threaded and Multithreaded Single-threaded and Multithreaded processesprocesses

Page 6: Operating System Principles

66Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.1.1 Motivation4.1.1 Motivation

A web browserA web browser One thread display images or textOne thread display images or text Another thread retrieves data from the networkAnother thread retrieves data from the network

A word processorA word processor One thread for displaying graphicsOne thread for displaying graphics Another thread for reading keystrokesAnother thread for reading keystrokes Third thread for performing spelling and grammar Third thread for performing spelling and grammar

checkingchecking

Page 7: Operating System Principles

77Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.1.1 Motivation4.1.1 Motivation

If new process will perform the same tasks as If new process will perform the same tasks as the existing process, why incur all that the existing process, why incur all that overhead?overhead? Process creation is very time-consuming and Process creation is very time-consuming and

resource intensive.resource intensive. Have the server run as a single process that accepts Have the server run as a single process that accepts

requests.requests.

Page 8: Operating System Principles

88Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.1.2 Benefits4.1.2 Benefits

ResponsivenessResponsiveness Allow a program to continue even if part of it is Allow a program to continue even if part of it is

blocked or is performing a lengthy operationblocked or is performing a lengthy operation

Resource sharingResource sharing MemoryMemory

Different threads all within the same address spaceDifferent threads all within the same address space ResourcesResources

EconomyEconomy More economical to create and context switch threadsMore economical to create and context switch threads

Utilization of multiprocessor architecturesUtilization of multiprocessor architectures Increase concurrencyIncrease concurrency

Page 9: Operating System Principles

99Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

Chapter 4Chapter 4Multithreaded ProgrammingMultithreaded Programming

1.1. OverviewOverview

2.2. Multithreading ModelsMultithreading Models

3.3. Thread LibrariesThread Libraries

4.4. Threading IssuesThreading Issues

5.5. Operating-System ExamplesOperating-System Examples

6.6. SummarySummary

7.7. ExercisesExercises

Page 10: Operating System Principles

1010Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.2 Multithreading Models4.2 Multithreading Models

User threadsUser threads Implemented by a thread library at the user levelImplemented by a thread library at the user level

Supported above the kernelSupported above the kernel AdvantageAdvantage

Fast to create and manageFast to create and manage DisadvantageDisadvantage

A thread may cause the entire process to blockA thread may cause the entire process to block ExamplesExamples

POSIX PthreadsPOSIX Pthreads

Win32 threadsWin32 threads

Java threadsJava threads

Page 11: Operating System Principles

1111Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.2 Multithreading Models4.2 Multithreading Models

Kernel threadsKernel threads The kernel performs thread creation, scheduling, and The kernel performs thread creation, scheduling, and

managementmanagementSupported directly by OSSupported directly by OS

AdvantageAdvantageKernel can schedule another thread when a thread is blockedKernel can schedule another thread when a thread is blocked

DisadvantageDisadvantageSlow to create and manage Slow to create and manage

ExamplesExamplesWindows XPWindows XPLinuxLinuxMax OS XMax OS X

SolarisSolarisTru64 UnixTru64 Unix

Page 12: Operating System Principles

1212Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.2 Multithreading Models4.2 Multithreading Models

Many systems provide support for both user and Many systems provide support for both user and kernel threadskernel threads Different multithreading modelsDifferent multithreading models

Many-to-one modelMany-to-one model

One-to-one modelOne-to-one model

Many-to-many modelMany-to-many model

Page 13: Operating System Principles

1313Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.2.1 Many-to-One Model4.2.1 Many-to-One Model

Many user-level threads mapped to one kernel threadMany user-level threads mapped to one kernel thread

Page 14: Operating System Principles

1414Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.2.1 Many-to-One Model4.2.1 Many-to-One Model

AdvantageAdvantage EfficientEfficient

DisadvantageDisadvantage Entire process may block if a thread makes a blocking system Entire process may block if a thread makes a blocking system

callcall Unable to run in parallel on multiprocessorsUnable to run in parallel on multiprocessors

Used on systems that do not support kernel threadsUsed on systems that do not support kernel threads Green threads – a thread library available for SolarisGreen threads – a thread library available for Solaris GNU Portable ThreadsGNU Portable Threads

Page 15: Operating System Principles

1515Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.2.2 One-to-One Model4.2.2 One-to-One Model

Each user thread mapped to one kernel threadEach user thread mapped to one kernel thread

Page 16: Operating System Principles

1616Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.2.2 One-to-One Model4.2.2 One-to-One Model

AdvantageAdvantage More concurrencyMore concurrency

DisadvantageDisadvantage Overhead of creating kernel threads can burden the Overhead of creating kernel threads can burden the

performance of an applicationperformance of an applicationRestrict the number of threadsRestrict the number of threads

ExampleExample LinuxLinux Windows operating systemsWindows operating systems

Windows 95, 98, NT, 2000, and XPWindows 95, 98, NT, 2000, and XP

Page 17: Operating System Principles

1717Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.2.3 Many-to-Many Model4.2.3 Many-to-Many Model

Multiplex many user-level threads to a small or equal Multiplex many user-level threads to a small or equal number of kernel threads – number of kernel threads – two-level modeltwo-level model

Page 18: Operating System Principles

1818Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.2.3 Many-to-Many Model4.2.3 Many-to-Many Model

AdvantageAdvantage Developers can create as many as user threads as wishDevelopers can create as many as user threads as wish More concurrencyMore concurrency

ExampleExample IRIXIRIX HP-UXHP-UX Tru64 UnixTru64 Unix Solaris prior to version 9Solaris prior to version 9

Page 19: Operating System Principles

1919Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

Chapter 4Chapter 4Multithreaded ProgrammingMultithreaded Programming

1.1. OverviewOverview

2.2. Multithreading ModelsMultithreading Models

3.3. Thread LibrariesThread Libraries

4.4. Threading IssuesThreading Issues

5.5. Operating-System ExamplesOperating-System Examples

6.6. SummarySummary

7.7. ExercisesExercises

Page 20: Operating System Principles

2020Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.3 Thread Libraries4.3 Thread Libraries

A thread libraryA thread library Provide the programmer an API for creating Provide the programmer an API for creating

and managing threadsand managing threads

Two primary approachesTwo primary approaches A user-level libraryA user-level library

Entirely in user space, with no kernel supportEntirely in user space, with no kernel supportA local function call in user spaceA local function call in user space

A kernel-level libraryA kernel-level librarySupported by the operating systemSupported by the operating systemA system call to the kernelA system call to the kernel

Page 21: Operating System Principles

2121Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

4.3 Thread Libraries4.3 Thread Libraries

Three main thread libraries in useThree main thread libraries in use POSIX PthreadsPOSIX Pthreads

A A specificationspecification for thread behavior for thread behavior not an implementationnot an implementation

Win32Win32 JavaJava

Page 22: Operating System Principles

2222Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

Chapter 4Chapter 4Multithreaded ProgrammingMultithreaded Programming

1.1. OverviewOverview

2.2. Multithreading ModelsMultithreading Models

3.3. Thread LibrariesThread Libraries

4.4. Threading IssuesThreading Issues

5.5. Operating-System ExamplesOperating-System Examples

6.6. SummarySummary

7.7. ExercisesExercises

Page 23: Operating System Principles

2323Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

Be skipped Be skipped

Page 24: Operating System Principles

2424Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

Chapter 4Chapter 4Multithreaded ProgrammingMultithreaded Programming

1.1. OverviewOverview

2.2. Multithreading ModelsMultithreading Models

3.3. Thread LibrariesThread Libraries

4.4. Threading IssuesThreading Issues

5.5. Operating-System ExamplesOperating-System Examples

6.6. SummarySummary

7.7. ExercisesExercises

Page 25: Operating System Principles

2525Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

SummarySummary

P.141 to P.142P.141 to P.142

Page 26: Operating System Principles

2626Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

Chapter 4Chapter 4Multithreaded ProgrammingMultithreaded Programming

1.1. OverviewOverview

2.2. Multithreading ModelsMultithreading Models

3.3. Thread LibrariesThread Libraries

4.4. Threading IssuesThreading Issues

5.5. Operating-System ExamplesOperating-System Examples

6.6. SummarySummary

7.7. ExercisesExercises

Page 27: Operating System Principles

2727Ku-Yaw ChangKu-Yaw Chang Chapter 4 Multithreaded ProgrammingChapter 4 Multithreaded Programming

ExercisesExercises

4.34.3

4.44.4

Page 28: Operating System Principles

The EndThe End