Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra 2006/2007 3.Threads.

Similar presentations


Presentation on theme: "Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra 2006/2007 3.Threads."— Presentation transcript:

1 Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra pmarques@dei.uc.pt 2006/2007 3.Threads

2 2 Disclaimer This slides and notes are heavily based on the companion material of [Silberschatz05].The original material can be found at: http://codex.cs.yale.edu/avi/os-book/os7/slide-dir/index.html In some cases, material from [Stallings04] may also be used. The original material can be found at: http://williamstallings.com/OS/OS5e.html The respective copyrights belong to their owners.

3 3 What’s a Process? MS Excel (1 process) MS Word (1 process) Process = The living “Image” of a program running

4 4 What’s a Thread? Process Thread 1 Thread 2Thread 3 Thread = A flow of execution inside a process

5 5 Motivation Process switching is an extremely heavy operation It’s necessary to remap address spaces It’s necessary to establish new security contexts It’s necessary to manage all information associated to processes Thread It’s a flow of execution inside a program The address space is the same (…changing a global variable in a thread affects all others) No context switching between processes Commutation among threads is very fast Communication among threads is easy to do and fast

6 6 Threads – Example in C

7 7 And the result is…

8 8 Threads – The same example in Java

9 9 And the result is the same…

10 10 Important! Threads share the same address space and resources! Changing one thing in one thread affects all others! It is the responsibility of the programmer to assure the correctness in the concurrent access to data and resources (more on this later…)

11 11 Why use threads? They are very light weight compared to processes Light context switches Fast to create and terminate Fast to synchronize Fast to communicate among threads Very useful in multiprocessor/multi-core architectures Economy or resources Much easier to program than shared memory! Everything is already shared Be careful to synchronize accesses!

12 12 What it looks like

13 13 PCB vs. Thread Control Blocks

14 14 Multithreading Models User threads (N-to-1 model) Threads are implemented in a library at user space. The kernel is completely unaware of the existence of threads User Mode Kernel Mode Thread Library

15 15 Multithreading Models Kernel threads (1-to-1 model) Threads are implemented exclusively in kernel space The kernel does all the scheduling of threads User Mode Kernel Mode

16 16 Advantages and Disadvantages User Level Threads Commutation among threads is fast since it doesn’t imply a mode switch Thread creation and termination is fast Doesn’t require any special support by the kernel, thus being available in any OS  If one thread blocks all threads block (e.g. on I/O)  Only one system call can happen at a time  Scheduling is not fair  One thread terminates all threads terminate Kernel Threads  Commutation among threads is slower since it implies going to the kernel  Thread creation and termination is slower  Requires that the kernel supports user visible threads If one thread blocks the others can continue Simultaneous system calls Fair scheduling Somewhat independent thread termination

17 17 Multithreading Models (3) The Many-to-Many Model A number of kernel threads can map to a different number of user threads User Mode Kernel Mode

18 18 Current Operating Systems User-level Threads E.g. Solaris before version 9 (Green Threads) E.g. Windows NT/2K with Fibers package Kernel-level Threads Virtually all modern operating systems: LINUX, Solaris, Windows XP, Mac OS X, Tru64 Unix Thread Libraries  Threads must be implemented by an API (library).  Libraries can be user-level or kernel-level  Using a library doesn’t mean that it’s user-level threading  Main libraries in current use: Java, PThreads, Win32

19 19 Why do all modern operating systems use Kernel Threads? Why do all modern operating systems use Kernel Threads? Quick Quiz

20 20 Some technical hurdles… What happens if a thread calls fork(), exec() or exit()? How to perform thread cancellation (a.k.a. kill-a-thread)? What happens if a signal is sent to a process? How can a thread have its own private data?

21 21 Thread Pools Since most operating systems use an 1-to-1 model, it’s important not to waste much time creating and destroying threads E.g. if you have a web server concurrently accepting requests from clients, it makes no sense to: 1) create a thread to serve a client; 2) the thread actually serves the client; 3) the thread dies; 4) start all over again… Thread 4 Thread 3 Thread 2 Thread 1 Thread 0 Thread Pool Some threads are previously created and BLOCKED waiting for work Whenever work appears, a managing thread wakes one thread in the pool and assigns it the work to be done After the thread completes the work it returns to the pool waiting for more work

22 22 PThreads vs Windows Threads PThreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X) Windows Threads Implements the one-to-one mapping Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads

23 23 Linux Threads Linux refers to them as tasks rather than threads. Thread creation is done through clone() system call. Creates a new process with a shared address space Note that in a “normal” fork() you have copy-on-write! In Linux 2.4… (with LinuxThreads) Threads were really processes with shared address spaces… Signal handling is incorrect. An extra management thread is created by the pthreads library ps shows all threads in a process (PID per thread) Core dumps don’t contain the stack and machine register information for all threads. getpid() returns a different result for each thread A thread cannot wait for a thread created by another thread Threads have parent-child, not peer, relationships

24 24 Linux 2.6: NGPT and NPTL IBM and Intel released the first version of the New Generation Posix Thread (NGPT) library. Aimed to solve the problems with LinuxThreads Abandoned in mid-2003 In September 2002, RedHat released the Native POSIX Thread Library (NPTL) NPTL is based on the 1:1 thread model It still uses clone() but now the kernel has the necessary support for e.g. fast synchronization It’s the current standard Nowadays: Typically glibc uses NPTL by default In some cases, you may still decide to revert to LinuxThreads

25 25 Thread Performance Comparison

26 26 Reference Linux threading models compared: LinuxThreads and NPTL, by Vikram Shukla, IBM developerWorks http://www-128.ibm.com/developerworks/linux/library/l- threading.html?ca=dgr-lnxw07LinuxThreadsAndNPTL Chapter 4: Threads All chapter 4! 4.1, 4.2, 4.3, 4.4, 4.5, 4.6


Download ppt "Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra 2006/2007 3.Threads."

Similar presentations


Ads by Google