Download presentation
Presentation is loading. Please wait.
1
Operating System (013022) Dr. H. Iwidat
CHAPTER 4: MULTITHREADED PROGRAMMING Dr. H. Iwidat I.T.C.D The Palestinian Academy for Security Sciences
2
Outline 4.1 Overview 4.2 Multithreading Models 4.3 Thread Libraries
4.4 Threading Issues (lightly)
3
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
4
4.1 Overview Single & Multithreaded Processes
An application typically is implemented as separate process with several thread of control. Ex: web browser, one thread display image, another received data.
6
Single and Multithreaded Processes
A thread is a basic unit of CPU utilization; It comprises a Thread ID, A program, Counter, A register set, And a stack. It shares with other threads belonging to the same process its code section. If a process has multiple threads of control, it can perform more than one task at a time. lightweight processes
7
Multithreaded Server Architecture
Create New Process: Run as a single process that accepts requests. When the server receives a request, it creates a separate process to service that request. Time consuming. Resource intensive.
8
Multithreaded Server Architecture (Cont…)
Create New Thread: It is generally more efficient to use one process that contains multiple threads. This approach would multithread the web-server process. The server would create a separate thread that would listen for client requests, when a request was made, rather than creating another process, the server would create another thread to service the request.
9
Life Cycle of a Thread Multithreaded Animation Application
Timed waiting threads (can / cannot) use a processor, even if one is available?
10
Benefits of multithreaded programming
Responsiveness Multithreading an interactive application may allow a program to continue running even if part of it is blocked. (e.x: web browser could still allow user interaction in one thread while an image was being loaded in another thread). Resource Sharing Process: shared resources throw messages pass & shared memory. Thread: threads share the memory & the resources of the process to which they belong. Economy creating a process is about thirty times slower than is creating a thread, and context switching is about five times slower. Scalability (Utilization of multiprocessor architectures.) Where threads may be running in parallel on different processors.
11
Concurrent execution on a single-core system
Parallel Execution on a Multicore System
12
Threading issues Five areas present challenges in programming for multicore system: Dividing activities Balance Data splitting Data dependency Testing and debugging Note: see page 156~154
13
4.2 Multithreading Models
Support of threads may by: User lever= user threads (supported above the kernel, manage without kernel support ) kernel = Kernel threads (supporting & manage directly by OS) (System call) The three common way of establish the threads relations: Many-to-One One-to-One Many-to-Many The user and kernel threads are implemented by kernel There must exist a relationship between user threads and kernel threads.
14
Multithreading Models (Many-to-One)
Many user-level threads to one kernel thread. Thread management is done by the thread library in user space. So it is efficient. But the entire process will block if a thread makes a blocking system call. Because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors.
15
Multithreading Models (One-to-one)
Each user-level thread maps to kernel thread More concurrency than the many-to-one model by allowing another thread to run when a thread makes a blocking system call. Also allows multiple threads to run in parallel on multiprocessors. Drawback: creating user thread requires creating kernel thread.
16
Multithreading Models (Many-to-many)
Multiplexes many user-level threads to a smaller or equal number of kernel threads. Allows the operating system to create a sufficient number of kernel threads. Allow to create as many user-threads as you wishes, true concurrency is not gained, case the kernel can schedule one thread at time.
17
4.3 Thread libraries Provides the programmer an API for creating and managing threads. There are two primary ways of implementing a thread library. Library entirely in user space (No kernel support). All code and data structures for the library exist in user space. Invoking a function in the library results in a local function call in user space & not a system call. Kernel-level library supported 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.
18
Thread libraries Three main thread libraries are in use today:
POSIX Pthreads Java Win32 (self read)
19
Pthreads libraries Pthreads, the threads extension of the POSIX standard, may be provided as either a user- or kernel-level library. A POSIX standard (IEEE c). 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)
20
4.4 Java Threads Java threads are managed by the JVM
Java threads may be created by: create a new class that is derived from the Thread class and to override its run() method. Implementing the Runnable interface (more commonly used) Public interface Runable { Public abstract void run(); }
21
Java thread states Calling start (), allocate memory in JVM
Calling run(), move from New to Runable
22
Java Thread States NEW - this state represents a new thread which is not yet started. RUNNABLE - this state represents a thread which is executing in the underlying JVM. it may wait for a resource from the Operating system like the processor while being in this state. BLOCKED - this state represents a thread which has been blocked and is waiting for a monitor to enter/re-enter a synchronized block/method. WAITING - this state represents a thread in the waiting state and this wait is over only when some other thread performs some appropriate action. A thread can get into this state either by calling - Object.wait (without timeout).
23
Java Thread States TIMED_WAITING - this state represents a thread which is required to wait at max for a specified time limit. A thread can get into this state by calling either of these methods: Thread.sleep, Object.wait (with timeout specified), Thread.join (with timeout specified). TERMINATED - this state represnts a thread which has completed its execution by returning from the run() method after completing the execution.
24
4.5 Threading Issues fork() and exec() system calls.
Thread cancellation of target thread Asynchronous or deferred Thread pools
25
The Fork() system call. A call to fork() will create a completely separate sub-process which will be exactly the same as the parent. The process that initiates the call to fork is called the parent process. The new process created by fork is called the child process. The child gets a copy of the parent's text and memory space. They do not share the same memory .
26
Fork() return values fork() system call returns an integer to both the parent and child processes: -1 this indicates an error with no child process created. A value of zero indicates that the child process code is being executed. Positive integers represent the child’s process identifier (PID) and the code being executed is in the parent’s process. if ( (pid = fork()) == 0) printf(“I am the child\n”); else printf(“I am the parent\n”);
27
Semantics of fork() and exec()
If one thread in a program calls fork(), does the new process duplicate all threads, or is the new process single-threaded? (Does fork() duplicate only the calling thread or all threads?) One duplicates all threads and Another that duplicates only the thread that invoked the fork() system call. Which of the two versions of fork() to use depends on the application.
28
Question? Since every process has its own address space, any modifications will be independent of the others. if the parent changes the value of its variable? Will it affect the variable in child? the modification will only affect the variable in the parent process's address space. Other address spaces created by fork() calls will not be affected even though they have identical variable names.
29
Cancellation Thread cancellation is the task of terminating a thread before it has completed. Examples: Multiple threads are concurrently searching through a database. presses a button on a web browser that stops a web page from loading Two general approaches: (Canceled threads = target thread) Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled
30
Thread pools The general idea behind a thread pool is: Advantages:
To create a number of threads (at process startup and place them into a pool), where they sit and wait for work. When a server receives a request, it awakens a thread from this pool -If one is available- & passes it the request to service. Once the thread completes its service, it returns to the pool and awaits more work. -If the pool contains no available thread- the server waits until one becomes free. 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 limited to the size of the pool. (important on systems that cannot support a large number of concurrent threads.)
31
End of chapter 4 Questions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.