29
Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com Email : [email protected] Reference Operating System Concepts, ABRAHAM SILBERSCHATZ Operating Systems

Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Designed and Presented by

Dr. Ayman Elshenawy Elsefy

Dept. of Systems & Computer Eng..

AL-AZHAR University

Website : eaymanelshenawy.wordpress.com

Email : [email protected]

Reference

Operating System Concepts, ABRAHAM SILBERSCHATZ

Operating Systems

Page 2: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Chapter 4: Threads

• Overview

• Multithreading Models

• Thread Libraries

• Threading Issues

• Operating System Examples

• Windows XP Threads

• Linux Threads

Page 3: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Thread• A thread is a basic unit of CPU utilization, it consists of:

• Thread ID

• A program counter.

• Register set

• Stack.

• It shares with other threads belonging to the same process its code section, data section, and other operating-system resources, such as open files.

• A single-threaded process and a multithreaded process.

• Multiple threads of control, it can perform more than one task at a time.

Page 4: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Threads Use• Many SW packages are multithreaded (single process with several threads of

control).• A word processor:

• A thread for displaying graphics.• A thread for responding to keystrokes from the user.• A thread for spelling and grammar checking.

• Web browser:

• Thread display images/text.• Thread gets data from the network.

• A Web server Problem:• Accepts client requests for Web pages, images, sound,..• If the server is busy and ran as a traditional single-threaded process, it will serve only one

client at a time, and a client might have to wait long time.

• Single thread Process Solution:

• The server accepts requests and creates a separate process for each request ( process creationis time consuming and resource lost ).

• If the new process will perform the same tasks as the existing process (overhead)

• Multiple threads Process:• The server will create a separate thread that listens for client requests.• When a request is made, the server will create a new thread to service the request and

resume listening for additional requests.

Page 5: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Threads Use• Threads used in remote procedure call (RPC)

• Allow inter-process communication, When a server receives a message, it services the message using a separate thread (allows server to handle several concurrent requests).

• Finally, most operating system kernels are now multithreaded; • Several threads operate in the kernel, Each thread performs a specific task,

such managing devices or interrupt handling.

Page 6: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Benefits

1. Responsiveness: Allow a program to continue running even if partof it is blocked or is performing a lengthy operation(Web browser allowuser interaction in one thread while an image is being loaded inanother).

2. Resource sharing: Threads share memory and resources of the parent

process by default.

3. Economy: Create and context-switch threads is less time consumingthan create and manage processes.

4. Scalability: Threads may be running in parallel on differentprocessors. A single-threaded process can only run on one processor,regardless how many are available. Multithreading on a multi-CPUmachine increases parallelism.

Page 7: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Multicore Programming• Place multiple computing cores on a single chip. Each of these cores appears

as a separate processor to the operating system

• Multithreaded programming provides a mechanism for more efficient useof multiple cores and improved concurrency.

• Designers of OS must write scheduling algorithms that use multiple processingcores to allow the parallel execution.

• Multicore systems challenges on programmer:

• Dividing activities: divide the program into separate tasks run in parallel.

• Balance: The tasks must perform equal work of equal value

• Data splitting: The data accessed and manipulated by the tasks must be divided torun on separate cores.

• Data dependency: The data accessed by the tasks must be examined for

dependencies between two or more tasks.

• Testing and debugging: On multiple cores, there are many different execution

paths. Testing and debugging such concurrent programs is more difficult.

Page 8: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Concurrent Execution on a Single-core System

Parallel Execution on a Multicore System

Page 9: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Multithreading Models

•User threads

• Provided either at the user level

• Supported above the kernel

• Managed without kernel support

•kernel threads:

• Provided by the kernel Level

• Supported and managed directly by the operating system.

• Most operating systems—including Windows XP , Windows Vista, Linux, Mac OS X, Solaris, and Tru64 UNIX support kernel threads.

• A relationship must exist between user threads and kernel threads.

Page 10: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Multithreading Models1. Many-to-One Model: Many user-level threads mapped to single kernel thread

2. One-to-One Model: Each user-level thread maps to kernel thread• Examples: Windows NT/XP/2000, Linux, Solaris 9 and later

3. Many-to-Many Model: Allows many user level threads to be mapped to many kernel threads• Examples: Solaris prior to version 9, Windows NT/2000 with the Thread Fiber package

4. 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

Page 11: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Thread Libraries• A thread library provides the programmer with an API for creating and managing

threads.

• There are two primary ways of implementing a thread library:

• User Level Thread Library:

• Provide a library entirely in user space with no kernel support.

• All code and data structures for the library exist in user space.

• Invoking a function in the library as a local function call in user space and not a system call.

• Kernel Level Thread Library:

• Implement a kernel-level library supported directly by the OS.

• Code and data structures for the library exist in kernel space.

• Invoking a function in the API for the library typically results in a system call to the kernel.

• Three main thread libraries are in use today:

1. POSIX Pthreads: may be provided as either a user- or kernel-level library.

2. Win32: is a kernel-level library available on Windows systems.

3. Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine (JVM) is running on top of ofthe OS, Java threads depends on the OS system that is running on it.

Page 12: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Pthreads• Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread

creation and synchronization.

• May be provided either as user-level or kernel-level

• 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)

Page 13: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Pthreads Example

Page 14: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Pthreads Example

Page 15: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Win32 API Multithreaded C Program (Example)

Page 16: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Win32 API Multithreaded C Program (Example)

Page 17: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Java thread• Java language provide a rich set of features for create and manage threads.

• All Java programs have at least a single thread of control that begins execution in the program’s main() method.

• Creating Java Thread:• Create a new class from the Thread class and to override its run() method. • Define a class that implements the Runnable interface.

• JVM and the underlying Host• JVM is implemented on top of a host OS ( hide the details of the underlying OS )

• allows Java programs to operate on any platform that supports a JVM.

• JVM does not indicate how Java threads are to be mapped to the underlying OS

• For example, the Windows XP OS uses the one-to-one model; therefore, each Java thread for a JVM running on such a system maps to a kernel thread.

• On operating systems that use the many-to-many model (such as Tru64 UNIX), a Java thread is mapped according to the many-to-many model.

• Solaris initially implemented the JVM using the many-to-one model (the green threads library mentioned earlier).

Page 18: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Java Multithreaded Program

Page 19: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Java Multithreaded Program

Page 20: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Java thread States

• A Java thread may be in one of six possible states in the JVM:

1. New: A thread object is created with the new command but has not yet started.

2. Runnable. Calling the start() method allocates memory for the new thread in the JVM and calls the run() method for the thread object. When a thread’s run() method is invoked, the thread moves from the new to the runnable state.

3. Blocked. A thread is in this state as it waits to acquire a lock—a tool used for thread synchronization.

4. Waiting. A thread invoking the join() method enters this state as it waits for the thread it is joining on to terminate.

5. Timed waiting. is similar to waiting, except a thread specifies the maximum amount of time it will wait. For example, the join() method has an optional parameter that the waiting thread can use to specify how long it will wait until the other thread terminates.

6. Terminated. A thread moves to the this state when its run() method terminates.

Page 21: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Java thread States

Page 22: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

A Multithreaded Solution to the Producer–Consumer Problem

Page 23: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

A Multithreaded Solution to the Producer–Consumer Problem

Page 24: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

A Multithreaded Solution to the Producer–Consumer Problem

Page 25: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Threading Issues with multi-thread programs

1. What happens If one thread in a program calls fork() (When creating new Process) ?

• The new process duplicate all threads

• The new process single-threaded.

• Some OS two versions of fork(), one that duplicates all threads and another that duplicates only the thread that invoked the fork() system call.

2. What happens if a Thread is Cancelled before it has finished?

• Asynchronous cancellation terminates the target thread immediately.

• Deferred cancellation allows the target thread to periodically check if it should be cancelled.

3. Signal Handling :

Signals are used to notify a process that a particular event has occurred.

1. Deliver the signal to the thread to which the signal applies

2. Deliver the signal to every thread in the process

3. Deliver the signal to certain threads in the process

4. Assign a specific thread to receive all signals for the process

Page 26: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Threading Issues with multi-thread programs

4. Thread Pools

• in a Web server, whenever the server receives a request, it creates a separate thread to service the request.

• The amount of time required to create the thread prior to servicing the request.

• The number of threads concurrently active in the system.

• Unlimited threads could exhaust system resources, such as CPU time or memory.

• One solution to this issue is to use a thread pool.

• How does Thread Pools works?

1. Create a number of threads at process startup and place them into a pool, an wait for work.

2. When a server receives a request, it assign the request for a thread from this pool to service (if one is available).

3. Once the thread completes its service, it returns to the pool and awaits more work.

4. If the pool contains no available thread, the server waits until one becomes free.

5. Thread Specific Data

• Threads belonging to the same process share the data of the process.

• Some times, each thread might need its own copy data( thread-specific data).

• For example, in a transaction-processing system, we might service each transaction in aseparate thread.

Page 27: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Windows XP Threads• Implements the one-to-one mapping, kernel-level, Consists of:

• Thread ID identifying the thread.

• Thread Context consists of :

• Register set representing the status of the processor

• User stack, employed when the thread is running in user mode.

• kernel stack, employed when the thread is running in kernel mode.

• Private storage area used by various run-time libraries and dynamic link libraries (DLLs)

• The primary data structures of a thread include:• ETHREAD—Executive Thread Block, include:

• A pointer to the process to which the thread belongs

• The address of the routine in which the thread starts control.

• a pointer to the corresponding KTHREAD.

• KTHREAD—Kernel Thread Block – includes:• scheduling and synchronization information for the thread.

• The kernel stack (used when the thread is running in kernel mode) .

• a pointer to the TEB

• TEB—Thread Environment Block- The TEB is a user-space data structure that is accessed when the thread is running in user mode:• the thread identifier,

• a user-mode stack,

• and an array for thread specific dat.

Page 28: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

Windows XP Threads Data Structures

Page 29: Designed and Presented by Dr. Ayman Elshenawy Elsefy · Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com

End of Chapter 4