32
EEL 602 1 Multithreading Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4 Understanding Linux/Unix Programming, Bruce Molay, Prentice-Hall, 2003.

OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

Embed Size (px)

Citation preview

Page 1: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 1

Multithreading

Reading:Silberschatzchapter 5

Additional Reading:Stallingschapter 4Understanding Linux/Unix Programming, Bruce Molay, Prentice-Hall, 2003.

Page 2: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 2

OutlineProcess and ThreadsMultithreadingMotivationAdvantagesRPC using Thread(s)User-Level ThreadsKernel-Level ThreadsCombined ApproachesPhtreadsThreading Issues

System Call SemanticsThread CancellationSignal HandlingThread PoolsThread Specific Data

Introduction: Linux, Win32, Solaris and Java Threads

Page 3: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 3

Process and ThreadsProcess Management

Resource OwnershipMemory, I/O channel, I/O devices and file

Scheduling/ExecutionExecution state and priority

Independent treatment by OSUnit of dispatching

ThreadLightweight Process (LWP, even KLT)

Unit of resource ownershipProcessTask

Page 4: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 4

Single Thread: Traditional approach

OS Support for ThreadsMSDOS - a single user process and a single threadUNIX - multiple user processes but only supports one thread per processWindows, Solaris, Linux, Mach, and OS/2 - multiple threads

ThreadBasic unit of CPU utilization, consisting of

PCRegister setStack

MultithreadingMultiple threads of execution within single process

Page 5: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 5

Multithreading

Page 6: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 6

Web BrowserOne thread to display imagesOther thread retrieves data from network

Word ProcessorOne thread for responding to keystrokesOther thread for spelling and grammar checkingOther thread for displaying graphics

File Server on LANController thread accepts file service requests and spawns worker thread for each requestCan handle many requests concurrently, thread finishes service - destroyed

Examples - Motivation

Page 7: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 7

Process

A virtual address space which holds the process imageProtected access to processors, other processes, files, and I/O resources

Within the process there can be one or more threads

Unit of resource allocation and a unit of protection, associated:

Page 8: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 8

ThreadAn execution state (running, ready, blocked)Saved thread context/state when not runningHas an execution stackSome per-thread static storage for local variablesAccess to the memory and resources of its process

Shared by all threads of the process

Page 9: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 9

Multithreading

• All of the threads of a process share the state and resources of process• They reside in same address space and have access to same data

Page 10: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 10

Advantage Threads!Takes far less time to create a new thread in existing process than a new process; Factor 10

Less time to terminate a thread than a process

Less time to switch between two threads within the same process

Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel

Page 11: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 11

Key States – RRB but no suspend

OperationsSpawnSpawn another threadBlockUnblockFinish

Functionality

Page 12: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 12

RPC Using Single Thread

Page 13: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 13

RPC Using Threads

Page 14: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 14

Example - Multithreading

Page 15: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 15

User-Level ThreadsThread Management → ApplicationNew threads within the same process

Thread LibraryControl, Library ↔ ThreadContext → user reg, pc & sp

Page 16: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 16

User-Level Threads

Page 17: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 17

ULT Vs KLTAdvantages of ULTs

Thread switching → Kernel mode privileges, less overheadScheduling can be application specificThread libraries → application utilities, Can run on any OS

Disadvantages of ULTsHigh blocking, OS → many system calls are blocking, all threads in process are blockedPure ULT strategy → cannot take advantage of multiprocessing

Solutions?JacketingWriting application as multiple processes rather than threads

Page 18: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 18

Kernel-Level ThreadsThread Management → KernelAPI to the kernel thread facility

Thread based scheduling by kernelPure ULT strategy → cannot take advantage of multiprocessingTransfer of control → mode switch

Page 19: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 19

Kernel-Level Threads

Page 20: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 20

Combined Approaches

Thread creation → user space, SolarisBulk of scheduling and synchronization of threads within applicationMultiple threads within the same application → multiple processorsEntire process is not blocked, Design

Page 21: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 21

Relationship Between Threads and Processes

Page 22: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 22

Threading IssuesSemantics of System Calls

fork()exec()

Thread CancellationAsynchronous CancellationDeferred Cancellation

Page 23: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 23

Threading IssuesSignal Handling

To thread to which its applicableTo every threadTo certain threadsTo a specific thread assigned

Thread PoolsSit & WaitWork, Return to poolFaster than waiting to create a threadLimits # that can exists at any point of time

Thread Specific Data

Page 24: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 24

PthreadsA POSIX standard API for thread creation and sync

API implementation dependent on OS

Common in UNIX operating systems

All programs → pthread.hpthread_create()pthread_suspend()pthread_yield()pthread_continue()pthread_join()pthread_exit(); pthread_sigmask(); sigwait()

Page 25: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 25

Example: Running 2 functions simultaneously

Screen output:[ajaykr@lib ~]$ ./hello_single

hellohellohellohellohelloworldworldworldworldworld

Source code:/* hello_single.c - a single threaded hello world program */

#include <stdio.h>#define NUM 5

main(){

void print_msg(char *);

print_msg("hello");print_msg("world\n");

}

void print_msg(char *m){

int i;for(i=0 ; i<NUM ; i++){printf("%s", m);fflush(stdout);sleep(1);}

}

Duration: 10 seconds

Single-threaded version

Page 26: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 26

Example: Running 2 functions simultaneously

Screen output:[ajaykr@lib ~]$ ./hello_multi

helloworldhelloworldhelloworldhelloworldhelloworld

Source code:/* hello_multi.c - a multi-threaded hello world program */

#include <stdio.h>#include <pthread.h>

#define NUM 5

main(){

pthread_t t1, t2; /* two threads */

void *print_msg(void *);

pthread_create(&t1, NULL, print_msg, (void *)"hello");pthread_create(&t2, NULL, print_msg, (void *)"world\n");pthread_join(t1, NULL);pthread_join(t2, NULL);

}

void *print_msg(void *m){

char *cp = (char *) m;int i;for(i=0 ; i<NUM ; i++){printf("%s", m);fflush(stdout);sleep(1);

}return NULL;}

Duration: 5 seconds

Multi-threaded version

Page 27: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 27

Linux ThreadsLinux refers to them as tasks rather than threads

Thread creation → Clone() system call, FlagsCLONE_FSCLONE_VMCLONE_SIGHANDCLONE_FILES

All flags → thread, No flags → fork (no sharing)

Clone() allows a child task to share the address space of the parent task (process)

Page 28: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 28

Win32 ThreadsPrimarily API for Win XP, NT, 2000, 98, 95windows.hThread creation → CreateThread(), …Windows XP application → Separate ProcessComponents of thread

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

1:1 Mapping, fiber library

Page 29: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 29

Solaris 2 Threads

Page 30: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 30

Solaris Process

Page 31: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 31

Java ThreadsJava → language level support

Management → JVM, alternative to user/kernel

Thread CreationExtending Thread class, new class → thread classImplementing the Runnable interface

Java threads mapping → depends on OS

Page 32: OS MP Print - Hong Kong Polytechnic Universitycsajaykr/myhome/teaching/eel602/MT.pdf · Spawn Spawn another ... Writing application as multiple processes rather than threads. EEL

EEL 602 32

Java Thread States