Lecture 4 Threads Operating System Fall 2006 1

  • Slides: 33
Download presentation
Lecture 4: Threads Operating System Fall 2006 1

Lecture 4: Threads Operating System Fall 2006 1

Contents n n n Overview: Processes & Threads Benefits of Threads Thread State and

Contents n n n Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread and Kernel Thread Multithreading Models Threading Issues 2

Process n Resource ownership n n Scheduling/execution n n process is allocated a virtual

Process n Resource ownership n n Scheduling/execution n n process is allocated a virtual address space to hold the process image Process may be allocated control or ownership of resources, e. g. I/O and files Protection function by OS The execution of a process follows an execution path(trace) through one or more programs The execution of a process may be interleaved with other processes Execution state and a dispatching priority These two characteristics are treated independently by the operating system 3

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

Processes & Threads n n 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) 4

Processes & Threads (cont. ) n In a multithreaded environment, the followings are associated

Processes & Threads (cont. ) n In a multithreaded environment, the followings are associated with a process: n n n Address space to hold the process image Protected access to processors, other processes (IPC), files, and I/O resources (devices & channels) Within a process, there may be one or more threads, each with the following: n n n A thread execution state (Running, Ready, etc) A saved context when not running – a separate program counter An execution stack Some static storage for local variables for this thread Access to memory and resources of its process, shared with all other threads in that process (global variables) 5

Single Threaded and Multithreaded Process Models 6

Single Threaded and Multithreaded Process Models 6

Benefits of Threads n Responsiveness n n Multithreading an interactive application may allow a

Benefits of Threads n Responsiveness n n Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the use. Resource Sharing n Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel 7

Benefits of Threads (cont. ) n Economy n n Takes less time to create

Benefits of Threads (cont. ) n Economy n n Takes less time to create a new thread than a process Less time to terminate a thread than a process Less time to switch between two threads within the same process Utilization of Multiprocessor Architectures n Threads within the same process may be running in parallel on different processors 8

Uses of Threads in a Single. User Multiprocessing System n Foreground and background work

Uses of Threads in a Single. User Multiprocessing System n Foreground and background work n n Asynchronous processing n n Asynchronous elements in the program can be implemented as threads. For example, as a protection against power failure, a word processor may write its buffer to disk once every minute. A thread can be created whose sole job is periodic backup and that schedules directly with the OS. Speed execution n n For example, in a spreadsheet program, one thread could display menus and read user input, while another thread executes user commands and updates the spreadsheet. On a multiprocessor system, multiple threads from the same process may be able to execute simultaneously. Modular program structure n Programs that involve a variety of activities or a variety of sources and destinations of input and output may be easier to design and implement using threads. 9

Thread States n Running Ready Blocked n Note: Suspend is at process-level n n

Thread States n Running Ready Blocked n Note: Suspend is at process-level n n 10

Thread Operations n n Spawn – create new thread Block – when a thread

Thread Operations n n Spawn – create new thread Block – when a thread needs to wait for an event, it will block Unblock – when the event for which a thread is blocked occurs, the thread is moved to the ready queue. Finish – when a thread completes, its register context and stack are deallocated. 11

Threads n n Suspending a process involves suspending all threads of the process since

Threads n n Suspending a process involves suspending all threads of the process since all threads share the same address space Termination of a process, terminates all threads within the process 12

Question? n If one thread in a process is blocked, does this prevent other

Question? n If one thread in a process is blocked, does this prevent other threads in the process even if that other thread is in a ready state? 13

Answer n Depends on whether OS is involved when the thread is blocked. If

Answer n Depends on whether OS is involved when the thread is blocked. If OS is involved, then answer is “yes”. 14

Thread Synchronization n n All of the threads of a process share the same

Thread Synchronization n n All of the threads of a process share the same address space and other resources such as open files. Any alternation of a resource by one thread affects the environment of the other threads in the same process. It is therefore necessary to synchronize the activities of the various threads. Will be covered later. 15

User Thread and Kernel Thread 16

User Thread and Kernel Thread 16

User Threads n n n All of the work of thread management is done

User Threads n n n All of the work of thread management is done by the application. The kernel is not aware of the existence of threads An application can be programmed to be multi-threaded by using a threads library, which is a package of routines for user thread management. The thread library contains code for creating and destroying threads, for passing messages and data between threads, for scheduling thread execution and for saving and restoring thread contexts. Three primary thread libraries: n n n POSIX Pthreads Win 32 threads Java threads 17

Pure User Threads n Advantages: n n Thread switching does not require user/kernel mode

Pure User Threads n Advantages: n n Thread switching does not require user/kernel mode switching. Thread scheduling can be application specific. User Threads can run on any OS through a thread library. Disadvantages: n n When a ULT executes a system call, not only the thread is blocked, but all of the threads within the process are blocked. Multithreaded application cannot take advantage of multiprocessing since kernel assign one process to only one processor at a time. 18

Kernel Threads n n n Supported and managed directly by the OS. W 2

Kernel Threads n n n Supported and managed directly by the OS. W 2 K, Linux, and OS/2 are examples of this approach In a pure Kernel Thread facility, all of the work of thread management is done by the kernel. There is no thread management code in the application area, simply an application programming interface to the kernel thread facility. 19

Pure Kernel Threads n Advantages n n n Kernel can simultaneously schedule multiple threads

Pure Kernel Threads n Advantages n n n Kernel can simultaneously schedule multiple threads from the same process on multiple processors If one thread in a process is blocked, kernel can schedule another thread of the same process Disadvantage n More overhead 20

Multithreading Models n Many-to-One n One-to-One n Many-to-Many 21

Multithreading Models n Many-to-One n One-to-One n Many-to-Many 21

Many-to-One n n Many user-level threads mapped to single kernel thread Examples: n n

Many-to-One n n Many user-level threads mapped to single kernel thread Examples: n n Solaris Green Threads GNU Portable Threads 22

One-to-One n n Each user-level thread maps to kernel thread Examples n n n

One-to-One n n Each user-level thread maps to kernel thread Examples n n n Windows NT/XP/2000 Linux Solaris 9 and later 23

Many-to-Many Model n n Allows many user level threads to be mapped to many

Many-to-Many Model n n Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the Thread. Fiber package 24

Two-level Model n n Similar to M: M, except that it allows a user

Two-level Model n n Similar to M: M, except that it allows a user thread to be bound to kernel thread Examples n n IRIX HP-UX Tru 64 UNIX Solaris 8 and earlier 25

Threading Issues n n n Semantics of fork() and exec() system calls Thread cancellation

Threading Issues n n n Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations 26

Semantics of fork() and exec() n Does fork() duplicate only the calling thread or

Semantics of fork() and exec() n Does fork() duplicate only the calling thread or all threads? 27

Thread Cancellation n n Terminating a thread before it has finished Two general approaches:

Thread Cancellation n n Terminating a thread before it has finished Two general approaches: n n Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled 28

Signal Handling n n Signals are used in UNIX systems to notify a process

Signal Handling n n Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals 1. 2. 3. n Signal is generated by particular event Signal is delivered to a process Signal is handled Options: n n Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process 29

Thread Pools n n Create a number of threads in a pool where they

Thread Pools n n Create a number of threads in a pool where they await work Advantages: n n Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool 30

Thread Specific Data n n Allows each thread to have its own copy of

Thread Specific Data n n Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i. e. , when using a thread pool) 31

Scheduler Activations n n n Both M: M and Two-level models require communication to

Scheduler Activations n n n Both M: M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library This communication allows an application to maintain the correct number kernel threads 32

End of lecture 4 Thank you! 33

End of lecture 4 Thank you! 33