Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating System Concepts 7th Edition Abraham SilBerschatz Peter Baer Galvin Greg Gagne Prerequisite: CSE212.

Similar presentations


Presentation on theme: "Operating System Concepts 7th Edition Abraham SilBerschatz Peter Baer Galvin Greg Gagne Prerequisite: CSE212."— Presentation transcript:

1 Operating System Concepts 7th Edition Abraham SilBerschatz Peter Baer Galvin Greg Gagne Prerequisite: CSE212

2 Chapter 4: Threads

3 Chapter 4: Threads Overview Multithreading Models Threading Issues
Pthreads Windows XP Threads Linux Threads Java Threads

4 Overview of Threads A thread is a basic unit of CPU utilization.
A thread consists of: Thread Identification (ID) Program counter (PC) Register Set Stack etc. It shares with other threads belonging to the same process its code section, data section and other operating system resources such as open files and signals. Traditional (heavyweight) process has single thread of control. Multiple threads of control can execute more than one task at the same time.

5 Single and Multithreaded Processes

6 Example of multithreaded process
Software on modern PC are multithreaded A separated process may create several threads of control A web browser may consists of: One thread display image, One thread retrieves data from network A word processor may consists of: One thread for displaying graphics, One thread for responding to keystroke from user, One thread for performing spelling and grammar checking in the background, etc.

7 Threads on Web server Web server accepts client requests for web pages, images, sound and so forth. Web server can’t run only single thread process otherwise it will service only one user at a time. One solution is to have web server has a single process to accept all incoming requests. On each request, it will create a separate process to service such request. Above process creation is time consuming and resource intensive. It is generally to have one process that contains multiple threads. Server would create a separate thread that would listen for client request and create another thread to service such request. This will allow server to service several concurrent requests.

8 Threads on Operating System
Many operating system are now multithreaded Several threads operate in the kernel Each thread performs a specific task such as: Managing devices: System devices: I/O, DMA, etc. System memory (memory management) Interrupt handling: Servicing serial interrupt Servicing disk interrupt, etc. etc.

9 Benefits Responsiveness: Allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to user. Resource Sharing: Threads share memory and resources of process to which they belong. This allows several different threads of activity within the same address (memory) space. Economy: Allocating memory and resources for process creation is more costly. It is more economical to create and context-switch threads. Utilization of Multiprocessor Architectures: Threads can run in parallel on different processors in the multiprocessor architecture.

10 User Threads Support of threads may provide either at user level (user threads) or kernel level (kernel threads). User threads are supported above the kernel and are managed without kernel support. Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads

11 Kernel Threads Kernel threads are supported and managed directly by operating system. Examples of system that support kernel threads: Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X

12 Multithreading Models
Many-to-One: Map many user-level threads to one kernel thread. One-to-One: Map each user thread to a kernel thread. Many-to-Many: Multiplex many user-level threads to smaller or equal number of kernel threads.

13 Many-to-One Many user-level threads mapped to single kernel thread
Thread management is done by thread library in user space, so it is efficient, but the entire process will be blocked if a thread makes a blocking system call. Only one thread can access kernel at a time, multiple threads are unable to run in parallel in multiprocessors Examples: Solaris Green Threads GNU Portable Threads

14 Many-to-One Model

15 One-to-One Each user-level thread maps to kernel thread
It provides more concurrency than the many-to-one model by allowing another thread to run when a thread making a blocking system call, it also allow multiple threads to run in parallel in multiprocessors. Only drawback is we can’t create too many threads due to overhead. Examples Windows NT/XP/2000 Linux Solaris 9 and later

16 One-to-one Model

17 Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads We can create as many user threads as necessary and the corresponding kernel threads can run in parallel in a multiprocessor system. Examples: Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

18 Many-to-Many Model

19 Two-level Model Similar to many-to-many model which multiplexes user-level threads to a smaller or equal to equal number of kernel threads, except that it allows a user thread to be bound to kernel thread. This type of model is sometime called Two-level model. Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

20 Two-level Model

21 Thread Libraries Thread library provides programmer an API (Application Programming Interface) for creating and managing threads. Thread library in user space with no kernel support. All code and data structures for the library in user space. Invoking a function in the library result in a local function call in user space not a system call. Thread library in kernel space, supported directly by operating system. All code and data structures for the library exist in kernel space. Invoking a function in API for a library typically results in a system call to kernel. Example of Thread Libraries POSIX Pthreads Win32 Java

22 Pthreads Pthreads: IEEE c (Pthreads) standard defining as API for thread creation and synchronization. This is a specification standard for thread behavior. It is up to the system designers to implement the specification in any way they wish. Example of operating system based on Pthreads: Solaris Linux Mac OS X Tru64 UNIX Shareware implementations are available in the public domain for the various Windows operating systems as well. Example of Multithreaded C program using Pthreads API is in Figure 4.6. of the text book.

23 Example of Pthreads C program
#include <pthread.h> #include <stdio.h> int sum; /* this data is shared by the thread(s) */ Void *runner(void *param); /* the thread */ int main(int argc, char *argv[ ]) { // get the default attributes // create the thread // wait for the thread is exit } // the thread will begin control in this function Void *runner(void *param)

24 Win32 Threads Win32 API is similar to Pthreads technique in several ways, but it has to include windows.h as additional header file Win32 API use the CreateThread() function (as those of Pthreads which attributes for the thread is passed to this function) Attributes consist of: Security information Size of stack Status flag which indicates: Start thread Suspended thread Ready thread Once the thread is created, the parent must wait for its completion.

25 Java Threads Threads are the fundamental model of program execution in a Java program. The Java language and its API provide a rich set of features for the creation and management of threads. All Java programs comprise at least a single thread.

26 Example of Java Threads program
#include <windows.h> #include <stdio.h> DWORD Sum; //data is shared by the thread(s) DWORD WINAPI Summation (LPVOID Param){ } int main(int arge, char *argv[ ]) { Class Sum{ Class Summation implements Runnable { Public class Driver{

27 Threading Issues There are several issues concerning multithreaded programs, they are: Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations

28 Semantics of fork() and exec()
fork() system call is used to create a separate, duplicate process. The semantics of the fork() and exe() system calls change in a multithreaded program. Does fork() duplicate only the calling thread or all threads? fork() call can duplicate all threads. fork() call can only duplicate only the thread that invoked the fork() system call Restriction: if exe() is call immediately after forking, then duplicating all threads is unnecessary, as the program specified in the parameters to exec() will replace the process. In this instance, duplicating only the calling thread is appropriate. If, however, the separate process does not call exe() after forking, the separate process will duplicate all threads.

29 Thread Cancellation Thread cancellation is an event that terminate a thread before it has finished. For example, if multiple threads are currently searching through a database and one thread returns the result, the remaining threads might be canceled. Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled in an orderly fashion.

30 Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred such as: illegal memory access, division by 0, etc. A signal handler is used to process signals Signal is generated the occurrence of a particular event Signal is delivered to a process Signal is being handled appropriately Options: 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

31 Problem with Threads When web server receives a request, it creates a separate thread to service the request. For multithreaded server has potential problems, they are: The amount of time required to create the thread prior to servicing the request, together with the fact that this thread will be discarded once it has completed its work. Allowing concurrent requests to be serviced in a new thread, there is no bound on the number of threads concurrently active in the system. Unlimited threads could exhausted system resources, such as CPU time or memory. One solution to the above problems is to use a thread pool approach.

32 Thread Pools Create a number of threads in a pool where they await work. Once the server receives a request, it awakens a thread from this pool (if one is available) and passes it the request to service. Once the thread completes its service, it returns to the pool and awaits for more work. If the pool contains no available thread, the server waits until one becomes free and the above steps will be repeated. Advantages: 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

33 Thread Specific Data Threads belonging to a process share the data of the process, this sharing of data provides one of the benefits of multithreaded programming In some circumstances, each thread might need its own copy of data. So there is a need to allow such thread to have its own copy of data, this type of thread is called “Thread Specific Data”. Useful when you do not have control over the thread creation process (i.e., when using a thread pool) Most thread libraries provide some form of support for thread-specific data, such libraries are: Win32 Pthreads Java

34 Scheduler Activations
Both many-to-many (M:M) and Two-level models require to place an immediate data structure communication to maintain the appropriate number of kernel threads allocated to the user application. The above data structure is known as lightweight process (LWP). To user-thread library, the LWP appears to be a virtual processor on which the application can schedule a user thread to run. Each LWP is attached to a kernel thread, and it is kernel threads that the operating system schedules to run on physical processors. If a kernel thread blocks (waiting for an I/O to complete) the LWP will be blocked as well. 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

35 Pthreads A POSIX standard (IEEE c) 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)

36 Windows XP 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 The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block)

37 Linux Threads Linux provides the fork() system call with the traditional functionality of duplicating a process. Linux also provides the ability to create threads using the clone() system call. Linux does not distinguish between processes and threads Linux refers to them as tasks (flow of control within a program) rather than threads Thread creation is done through clone( ) system call clone( ) allows a child task to share the address space of the parent task (process). When clone() is invoked, it is passed a set of flags, which determine how much sharing is to take place between the parent and child tasks.

38 Java Threads Threads are the fundamental model of program execution in a Java program, and the Java language and its API provide a rich set of features for the creation and management of threads. Java threads are managed by the Java Virtual Machine (JVM) Java threads may be created by: Extending Thread class Implementing the Runnable interface

39 Java Thread States

40 End of Chapter 4


Download ppt "Operating System Concepts 7th Edition Abraham SilBerschatz Peter Baer Galvin Greg Gagne Prerequisite: CSE212."

Similar presentations


Ads by Google