Presentation is loading. Please wait.

Presentation is loading. Please wait.

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.

Similar presentations


Presentation on theme: "Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads."— Presentation transcript:

1 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads

2 4.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

3 4.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems To discuss the APIs for the Pthreads, Win32, and Java thread libraries To examine issues related to multithreaded programming

4 4.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Threads A thread, sometimes called a lightweight process(LWP), is a basic unit of CPU utilization and executes within the space of the process that creates it. A thread behave like a process with in a process but is does not have a its own PCB..

5 4.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Threads…. It comprises a thread ID, a program counter, register set stack. It shares with other threads belonging to the same process its code sections, data sections Any operating system resources, which are available to the task. A traditional (or heavyweight) process has a single thread of control. If a process has multiple threads of control, it can perform more than one task at a time

6 4.6 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Motivation Threads run within application Multiple tasks with the application can be implemented by separate threads Update display Fetch data Spell checking Answer a network request Process creation is heavy-weight while thread creation is light-weight Can simplify code, increase efficiency Kernels are generally multithreaded

7 4.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multithreading Multithreading allows operating system to execute different thread simultaneously. A Web browser might have one thread display images or text while another thread retrieves data from the network, for example. A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling and grammar checking in the background.

8 4.8 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition In certain situations, a single application may be required to perform several similar tasks. For example, a Web server accepts client requests for Web pages, images, sound, and so forth. A busy Web server may have several (perhaps thousands of) clients concurrently accessing it. If the Web server ran as a traditional single threaded process, it would be able to service only one client at a time, and a client might have to wait a very long time for its request to be serviced.

9 4.9 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Single and Multithreaded Processes

10 4.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Benefits Responsiveness 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 user. Resource Sharing By default, thread share the memory and resources of the process to which they belong, Code sharing allows an application to have several different threads of activity all within the same address space. Economy Allocating memory and resources for process creation is costly. Alternatively, because threads share resources of the process to which they belong, it is more economical to create and context switch threads. Scalability The benefits of multithreading can be greatly increased in a multiprocessor environment, where each thread may be running in parallel on a different processor. A single thread process can only run on one CPU no matter how many are available.

11 4.11 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Disadvantages Resource sharing Whereas resource sharing is one the major advantages of thread. It is also a disadvantage because proper synchronization is needed between threads for accessing the shared resource Difficult Programming model It is difficult to write, debug and maintain multi-threaded programs for an average user. This is particularly true when is comes to writing code for synchronized access to shared resources.

12 4.12 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multicore Programming Multicore systems putting pressure on programmers, challenges include: Dividing activities Balance Data splitting Data dependency Testing and debugging

13 4.13 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Dividing activities. This involves examining applications to find areas that can be divided into separate, concurrent tasks and thus can run in parallel on individual cores.

14 4.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Balance. While identifying tasks that can run in parallel, programmers must also ensure that the tasks perform equal work of equal value. In some instances, a certain task may not contribute as much value to the overall process as other tasks; using a separate execution core to run that task may not be worth the cost. Data splitting. Just as applications are divided into separate tasks, the data accessed and manipulated by the tasks must be divided to run on separate cores.

15 4.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Data dependency. The data accessed by the tasks must be examined for dependencies between two or more tasks. In instances where one task depends on data from another, programmers must ensure that the execution of the tasks is synchronized to accommodate the data dependency. Testing and debugging. When a program is running in parallel on multiple cores, there are many different execution paths. Testing and debugging such concurrent programs is inherently more difficult than testing and debugging single-threaded applications.

16 4.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Concurrent Execution on a Single-core System

17 4.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Parallel Execution on a Multicore System

18 4.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition User and Kernel threads Thread may handled at different levels. There are two types of thread to be managed in a modern system. These are known as user Threads and Kernel threads.

19 4.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition User Threads Thread management done by user-level threads library. User threads are implemented in user level libraries instead of system calls. The threading switching does not need to call operating system. It does not cause interrupt to the kernel. Three primary thread libraries: POSIX Pthreads Win32 threads Java threads

20 4.20 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Kernel Threads Supported within the Kernel of the operating system. All modern operating system support kernel level threads. They allow the kernel to perform multiple tasks and to service multiple kernel system calls simultaneously. Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X

21 4.21 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multithreading Models Many-to-One One-to-One Many-to-Many

22 4.22 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Many-to-One In many-to-one model, many user level threads are mapped to one kernel level thread. It is efficient because it is implemented in user space. A process using this model will blocked entirely if a thread makes a blocking system call. Only one thread can access the kernel at a time so it cannot run in parallel on multiprocessor. Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads

23 4.23 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Many-to-One Model

24 4.24 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition One-to-One In one-to-one model, each user thread is mapped to a kernel thread. It provides more concurrency because it allows an other thread to execute if a thread makes a blocking system call. Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later

25 4.25 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition One-to-one Model

26 4.26 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition 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 Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

27 4.27 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Many-to-Many Model

28 4.28 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

29 4.29 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Two-level Model

30 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition End of Chapter 4


Download ppt "Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads."

Similar presentations


Ads by Google