Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.

Similar presentations


Presentation on theme: "Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University."— Presentation transcript:

1 Operating System Concepts Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2 2Ku-Yaw ChangChapter 5 Threads A process With a single thread of control With a single thread of control With multiple threads of control With multiple threads of control Multithreaded computer systems Pthreads Solaris 2 threads Windows 2000 threads Linux threads Java threads

3 3Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

4 4Ku-Yaw ChangChapter 5 Threads 5.1 Overview A thread A basic unit of CPU utilization A basic unit of CPU utilization A thread ID A program counter A register set A stack Called a lightweight process (LWP) Called a lightweight process (LWP) A traditional process has a single thread of control, called a heavyweight process (HWP)

5 5Ku-Yaw ChangChapter 5 Threads Single- and Multithreaded processes

6 6Ku-Yaw ChangChapter 5 Threads 5.1.1 Motivation A web browser One thread display images or text One thread display images or text Another thread retrieves data from the network Another thread retrieves data from the network A word processor One thread for displaying graphics One thread for displaying graphics Another thread for reading keystrokes Another thread for reading keystrokes Third thread for performing spelling and grammar checking Third thread for performing spelling and grammar checking Process creation very heavyweight. If new process will perform the same tasks as the existing process, why incur all that overhead? If new process will perform the same tasks as the existing process, why incur all that overhead?

7 7Ku-Yaw ChangChapter 5 Threads 5.1.2 Benefits Responsiveness Allow a program to continue even if part of it is blocked or is performing a lengthy operation Allow a program to continue even if part of it is blocked or is performing a lengthy operation Resource sharing Memory Memory Different threads all within the same address space Resources ResourcesEconomy More economical to create and context switch threads More economical to create and context switch threads Utilization of multiprocessor architectures Increase concurrency Increase concurrency

8 8Ku-Yaw ChangChapter 5 Threads 5.1.3 User and Kernel Threads User threads Implemented by a thread library at the user level Implemented by a thread library at the user level Supported above the kernel Advantage Advantage Fast to create and manage Disadvantage Disadvantage A thread may cause the entire process to block Examples Examples POSIX Pthreads Mach C-threads Solaris 2 UI-threads

9 9Ku-Yaw ChangChapter 5 Threads 5.1.3 User and Kernel Threads Kernel threads The kernel performs thread creation, scheduling, and management The kernel performs thread creation, scheduling, and management Supported directly by OS Advantage Advantage Kernel can schedule another thread when a thread is blocked Disadvantage Disadvantage Slow to create and manage Examples Examples Windows NT/2000 Solaris 2 BeOS Tru64 Unix

10 10Ku-Yaw ChangChapter 5 Threads 5.1.3 User and Kernel Threads Java threads Created and managed by the Java virtual machine (JVM) Created and managed by the Java virtual machine (JVM) Do not fall under the realm of either user or kernel threads

11 11Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

12 12Ku-Yaw ChangChapter 5 Threads 5.2 Multithreading Models Many systems provide support for both user and kernel threads Different multithreading models Different multithreading models Many-to-one model One-to-one model Many-to-many model

13 13Ku-Yaw ChangChapter 5 Threads 5.2.1 Many-to-One Model Many user-level threads mapped to one kernel thread Advantage Efficient EfficientDisadvantage Entire process may block if a thread makes a blocking system call Entire process may block if a thread makes a blocking system call Unable to run in parallel on multiprocessors Unable to run in parallel on multiprocessors Used on systems that do not support kernel threads Green threads – a thread library available for Solaris 2 Green threads – a thread library available for Solaris 2

14 14Ku-Yaw ChangChapter 5 Threads 5.2.1 Many-to-One Model

15 15Ku-Yaw ChangChapter 5 Threads 5.2.2 One-to-One Model Each user thread mapped to one kernel thread Advantage More concurrency More concurrencyDisadvantage Overhead of creating kernel threads can burden the performance of an application Overhead of creating kernel threads can burden the performance of an application Restrict the number of threads Example Windows NT/2000 and OS/2 Windows NT/2000 and OS/2

16 16Ku-Yaw ChangChapter 5 Threads 5.2.2 One-to-One Model

17 17Ku-Yaw ChangChapter 5 Threads 5.2.3 Many-to-Many Model Multiplex many user-level threads to a small or equal number of kernel threads Advantage Developers can create as many as user threads as wish Developers can create as many as user threads as wish More concurrency More concurrencyExample Solaris 2 Solaris 2 Windows NT/2000 with the ThreadFiber package Windows NT/2000 with the ThreadFiber package IRIX IRIX HP-UX HP-UX Tru64 Unix Tru64 Unix

18 18Ku-Yaw ChangChapter 5 Threads 5.2.3 Many-to-Many Model

19 19Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

20 20Ku-Yaw ChangChapter 5 Threads 5.3 Threading Issues The fork and exec System Calls Cancellation Signal Handling Thread Pools Thread-Specific Data

21 21Ku-Yaw ChangChapter 5 Threads 5.3.1 The fork and exec System Calls One thread in a program calls fork New process duplicate all threads New process duplicate all threads New process is single threaded New process is single threaded The exec system call works in the same way as described before. Some systems have two versions of fork If exec is called immediately, then duplicating all threads is unnecessary. If exec is called immediately, then duplicating all threads is unnecessary. If not, the separate process should duplicate all threads. If not, the separate process should duplicate all threads.

22 22Ku-Yaw ChangChapter 5 Threads 5.3.2 Cancellation Thread cancellation The task of terminating a thread before it has completed The task of terminating a thread before it has completed Target thread Target thread The thread that is to be cancelled Two different scenarios Asynchronous cancellation Asynchronous cancellation One thread immediately terminates the target threads. Deferred cancellation Deferred cancellation The target thread periodically checks if it is should terminate, allowing itself an opportunity to terminate itself in an orderly fashion. Cancellation points (Pthreads)

23 23Ku-Yaw ChangChapter 5 Threads 5.3.3 Signal Handling A signal Used in Unix systems Used in Unix systems Notify a process that a particular event has occurred Notify a process that a particular event has occurred May be received either synchronously or asynchronously May be received either synchronously or asynchronously All signals follow the same pattern A signal is generated by the occurrence of a particular event. A signal is generated by the occurrence of a particular event. A generated signal is delivered to a process. A generated signal is delivered to a process. Once delivered, the signal must be handled. Once delivered, the signal must be handled.

24 24Ku-Yaw ChangChapter 5 Threads 5.3.3 Signal Handling Synchronous signal An illegal memory access or divided by zero An illegal memory access or divided by zero Asynchronous signal Terminating a process with specific keystrokes ( ) Terminating a process with specific keystrokes ( ) Signal may be handled by A default signal handler A default signal handler Every signal has a default signal handler run by the kernel A user-defined signal handler A user-defined signal handler Override the default signal handler

25 25Ku-Yaw ChangChapter 5 Threads 5.3.3 Signal Handling Delivering signals is more complicated in multithreaded programs Deliver the signal to the thread to which the signal applies Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process Assign a specific thread to receive all signals for the process

26 26Ku-Yaw ChangChapter 5 Threads 5.3.3 Signal Handling Windows 2000 Does not explicitly support signals Does not explicitly support signals Emulation using Asynchronous Procedure Calls (APCs) Emulation using Asynchronous Procedure Calls (APCs) Allow a thread to specify a function that is to be called when the thread receives notification of a particular event

27 27Ku-Yaw ChangChapter 5 Threads 5.3.4 Thread Pools Potential problems Amount of time required to create the thread prior to servicing the request Amount of time required to create the thread prior to servicing the request Unlimited threads could exhaust the system resources Unlimited threads could exhaust the system resources Thread pool To create a number of threads at process startup and place them in a pool To create a number of threads at process startup and place them in a pool Sit and wait for work A server receives a request, a thread is awaken from the pool A server receives a request, a thread is awaken from the pool Once the thread completes its work, it returns to the pool Once the thread completes its work, it returns to the pool If the pools contains no available thread, the server waits until one become free If the pools contains no available thread, the server waits until one become free

28 28Ku-Yaw ChangChapter 5 Threads 5.3.5 Thread-Specific Data Threads belonging to a process share the data of the process. One of the benefits of multithreaded programming One of the benefits of multithreaded programming Each thread also needs its own copy of certain data – called thread specific data

29 29Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

30 30Ku-Yaw ChangChapter 5 Threads 5.4 Pthreads a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization A specification for thread behavior, not an implementation A specification for thread behavior, not an implementation Common in UNIX-based systems Common in UNIX-based systems Generally not supported in Windows systems Generally not supported in Windows systems

31 31Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

32 32Ku-Yaw ChangChapter 5 Threads 5.5 Solaris 2 Threads Read it by yourself P. 141 to 143 P. 141 to 143

33 33Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

34 34Ku-Yaw ChangChapter 5 Threads 5.6 Windows 2000 Threads Win32 API Primary API for the family of MS OSs Primary API for the family of MS OSs Provide one-to-one mapping model A fiber library provides the many-to-many model A fiber library provides the many-to-many model Primary data structures ETHREAD (executive thread block) ETHREAD (executive thread block) In kernel space KTHREAD (kernel thread block) KTHREAD (kernel thread block) In kernel space TEB (Thread environment block) TEB (Thread environment block) User-space data structure

35 35Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

36 36Ku-Yaw ChangChapter 5 Threads 5.7 Linux Threads Read it by yourself P. 144 to 145 P. 144 to 145

37 37Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

38 38Ku-Yaw ChangChapter 5 Threads 5.8 Java Threads Java Support threads at the language level Support threads at the language level For the creation and management of threads Threads are managed by JVM Threads are managed by JVM Not by a user-level library or kernel A Java program A Java program Only a main method runs as a single thread in JVM

39 39Ku-Yaw ChangChapter 5 Threads 5.8.1 Thread Creation Thread creation To create a new class that derived from the Thread class To create a new class that derived from the Thread class Override the run method of the Thread class Calling the start method actually creates the new thread Calling the start method actually creates the new thread

40 40Ku-Yaw ChangChapter 5 Threads Java Thread States

41 41Ku-Yaw ChangChapter 5 Threads 5.8.2 The JVM and the Host Operation System JVM does not indicate how Java thread are to be mapped to the underlying OS Leaving the decision to the particular implementation of the JVM Leaving the decision to the particular implementation of the JVM Windows use one-to-one model

42 42Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

43 43Ku-Yaw ChangChapter 5 Threads Summary P.147

44 44Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

45 45Ku-Yaw ChangChapter 5 Threads Exercises 5.15.25.35.8

46 The End


Download ppt "Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University."

Similar presentations


Ads by Google