Chapter 4 Multithreaded Programming

Chapter 4 Multithreaded Programming Threads ƒ Objectives: ƒ Concepts and issues associated with multithreaded computer systems. ƒ Thread – Lightweig...
Author: Juliana Leonard
0 downloads 0 Views 43KB Size
Chapter 4 Multithreaded Programming

Threads ƒ Objectives: ƒ Concepts and issues associated with multithreaded computer systems.

ƒ Thread – Lightweight process(LWP) ƒ a basic unit of CPU utilization ƒ A thread ID, program counter, a register set, and a stack space

ƒ Process – heavyweight process ƒ A single thread of control * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Threads ƒ Motivation

code segment

ƒ A web browser ƒ Data retrieval ƒ Text/image displaying

ƒ A word processor ƒ Displaying ƒ Keystroke reading ƒ Spelling and grammar checking

stack stack stack registers

registers

registers

data segment

ƒ A web server ƒ Clients’ services ƒ Request listening

filesfiles * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Threads ƒ Benefits ƒ Responsiveness ƒ Resource Sharing ƒ Economy ƒ Creation and context switching ƒ 30 times slower in process creation in Solaris 2 ƒ 5 times slower in process context switching in Solaris 2

ƒ Utilization of Multiprocessor Architectures * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

User-Level Threads ƒ User-level threads are implemented by a thread library at the user level. ƒ Examples: ƒ Advantages

ƒ POSIX Pthreads, Mach C-threads, Solaris 2 UI-threads

ƒ Context switching among them is extremely fast

ƒ Disadvantages ƒ Blocking of a thread in executing a system call can block the entire process. * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Kernel-Level Threads ƒ Kernel-level threads are provided a set of system calls similar to those of processes ƒ Examples ƒ Windows 2000, Solaris ƒ Advantage

2, True64UNIX

ƒ Blocking of a thread will not block its entire task.

ƒ Disadvantage ƒ Context switching cost is a little bit higher because the kernel must do the switching. * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Multithreading Models ƒ Many-to-One Model ƒ Many user-level threads to one kernel thread ƒ Advantage: ƒ Efficiency

k

ƒ Disadvantage: ƒ One blocking system call blocks all. ƒ No parallelism for multiple processors

ƒ Example: Green threads for Solaris 2 * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Multithreading Models ƒ One-to-One Model

k

ƒ One user-level thread to one kernel thread ƒ Advantage: One system call blocks one thread. ƒ Disadvantage: Overheads in creating a kernel thread. ƒ Example: Windows NT, Windows 2000, OS/2

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Multithreading Models ƒ Many-to-Many Model ƒ Many user-level threads to many kernel threads ƒ Advantage: k k k

ƒ A combination of parallelism and efficiency

ƒ Example: Solaris 2, IRIX, HPUX,Tru64 UNIX

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Thread Libraries ƒ Goal: Provide an API for creating and managing threads! ƒ Two Approaches: ƒ User Thread Library ƒ Kernel-Level Thread Library

ƒ Well-Known Examples ƒ POSIX Pthreads – User or Kernel Level ƒ Win32 threads – Kernel Level ƒ Java threads – Level Depending on the Thread Library on the Host System * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Pthreads ƒ Pthreads (IEEE 1003.1c) ƒ API Specification for Thread Creation and Synchronization ƒ UNIX-Based Systems, Such As Solaris 2.

ƒ User-Level Library (??) ƒ Header File: ƒ pthread_attr_init(), pthread_create(), pthread_exit(), pthread_join(), etc.

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Pthreads #include main(int argc, char *argv[]) { … pthread_attr_init(&attr); pthread_create(&tid, &attr, runner, argv[1]); pthread_join(tid, NULL); …} void *runner(void *param) { int i, upper = atoi(param), sum = 0; if (upper > 0) for(i=1;i