Lecture 10 Threads Implementation 1 Review POSIX Standards

  • Slides: 15
Download presentation
Lecture 10: Threads Implementation 1

Lecture 10: Threads Implementation 1

Review: POSIX Standards • pthread_create() • pthread_exit() • pthread_join() • pthread_yield() 2

Review: POSIX Standards • pthread_create() • pthread_exit() • pthread_join() • pthread_yield() 2

In this lecture • Threads implementation – User Level Threads – Kernel Level Threads

In this lecture • Threads implementation – User Level Threads – Kernel Level Threads – Hybrid Implementations 3

What is a Thread? • Execution context – Registers, Stack – Shared text, heap,

What is a Thread? • Execution context – Registers, Stack – Shared text, heap, static data segments • Thread execution context smaller than process execution context 4

User Space Threads • All the thread support provided by a user level library

User Space Threads • All the thread support provided by a user level library • Kernel not even aware that the program has multiple threads • User library also handles synchronization (mutexes, condition variables, etc) 5

Implementing Threads in User Space A user-level threads package 6

Implementing Threads in User Space A user-level threads package 6

Pros of User Space Implementation • Can be used in OSes which don’t implement

Pros of User Space Implementation • Can be used in OSes which don’t implement threads • Thread related operations are fast (no system calls) – creation • Each process can have its own thread scheduling algorithm • Thread scheduling is also faster 7

Cons of User Space Implementation • System calls/page faults block and other threads will

Cons of User Space Implementation • System calls/page faults block and other threads will not be able to execute – No benefits of multi-threading – Possible solutions • Change system calls to be nonblocking • Wrap system calls. Check whether it is safe before making system calls 8

Implementing Threads in the Kernel A threads package managed by the kernel 9

Implementing Threads in the Kernel A threads package managed by the kernel 9

Kernel Space • Pros: – Can take advantage of multiple processors – System call/page

Kernel Space • Pros: – Can take advantage of multiple processors – System call/page fault blocks only the thread which made the call • Cons: – Thread operations involve system calls (expensive) • Solution: recycle threads 10

Hybrid Implementations Multiplexing user-level threads onto kernellevel threads 11

Hybrid Implementations Multiplexing user-level threads onto kernellevel threads 11

Processes & Threads • Resource ownership – Process or Task • Scheduling/execution – Thread

Processes & Threads • Resource ownership – Process or Task • Scheduling/execution – Thread or lightweight process One process, one thread (MS-DOS) Multiple processes, one thread per process (Unix) One process, multiple threads (Java Runtime) Multiple processes, multiple threads (W 2 K, Solaris, Linux) 12

Single Threaded and Multithreaded Process Models 13

Single Threaded and Multithreaded Process Models 13

Key Benefits of Threads 14

Key Benefits of Threads 14

 • User and Kernel-Level Threads Performance • Null fork: the time to create,

• User and Kernel-Level Threads Performance • Null fork: the time to create, schedule, execute, and complete a process/thread that invokes the null procedure. • Signal-Wait: the time for a process/thread to signal a waiting process/thread and then wait on a condition. • Procedure call: 7 us Kernel Trap: 17 us • Thread Operation Latencies Operation Null fork Signal Wait ULT 34 37 KLT 948 441 Process 11, 300 1, 840 • Observation ü While there is a significant speedup by using KLT multithreading compared to single-threaded processes, there is an additional significant speedup by using ULTs. ü However, whether or not the additional speedup is realized depends on the nature of the applications involved. ü If most of the thread switches require kernel mode access, then ULT may not perform much better 15 than KLT.