Presentation is loading. Please wait.

Presentation is loading. Please wait.

Source: Operating System Concepts by Silberschatz, Galvin and Gagne.

Similar presentations


Presentation on theme: "Source: Operating System Concepts by Silberschatz, Galvin and Gagne."— Presentation transcript:

1 Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Threads Source: Operating System Concepts by Silberschatz, Galvin and Gagne. CSC 4103: Operating System

2 Overview Process can have
a single thread of control or activity multiple threads of control or activity. A thread is a flow of control within a process a basic unit of CPU utilization – also called a LWP thread ID, program counter, register set and stack all threads share the same address space of their process. Multithreaded computer systems are common. e.g., desktop PCs Web browser can have two threads, one for display and the other for data retrieving. Pthreads Win32 Threads Java Threads CSC 4103: Operating System

3 Single and Multithreaded Processes
Threads belonging to a given process share with each other code section, data section and other resources, e.g., open files. CSC 4103: Operating System

4 Benefits Increased responsiveness to user: Resource Sharing Economy
A program continues running with other threads even if part of it is blocked or performing a lengthy operation in one thread. Resource Sharing Threads share memory and resources of their process. Economy Less time consuming to create and manage threads than processes as threads share resources, e.g., thread creating is 30 times faster than process creating in Solaris. Utilization of Multiprocessor Architectures Increases concurrency because each thread can run in parallel on a different processor. CSC 4103: Operating System

5 Thread Types User Threads
Threads are implemented at the user level by a thread library Library provides support for thread creation, scheduling and management. User threads are fast to create and manage. Kernel Threads Supported and managed directly by the OS. Thread creation, scheduling and management take place in kernel space. Slower to create and manage. Relationship between user threads and kernel threads. CSC 4103: Operating System

6 Multithreading Models
Three common ways of establishing a relationship between user-level threads and kernel-level threads Many-to-One One-to-One Many-to-Many CSC 4103: Operating System

7 Many-to-One Many user-level threads mapped to single kernel thread.
Easier thread management. Blocking-problem. No concurrency. Examples: Green threads for Solaris CSC 4103: Operating System

8 One-to-One Each user-level thread maps to a kernel thread.
Overhead of creating kernel threads, one for each user thread. No blocking problem Provides concurrency. Examples: Linux, family of Windows CSC 4103: Operating System

9 Many-to-Many Model Allows many user level threads to be mapped to many kernel threads. Allows the OS to create a sufficient number of kernel threads. Users can create as many as user threads as necessary. No blocking and concurrency problems. Two-level model. CSC 4103: Operating System

10 Threading Issues – fork and exec
Change in semantics of fork() and exec() system calls. Two versions of fork system call: One duplicates only the thread that invokes the call. Another duplicates all the threads, i.e., duplicates an entire process. exec system call: Program specified in the parameters to exec will replace the entire process – including all threads. If exec is called immediately after forking, duplicating all threads is not required. CSC 4103: Operating System

11 Cancellation Task of terminating a thread before it has completed.
Canceling one thread in a multithreaded searching through a database. Stopping a web page from loading. Asynchronous cancellation One thread immediately terminates the target thread (one to be canceled). Deferred cancellation The target thread can periodically check if it should terminate, allowing a normal exit. CSC 4103: Operating System

12 Signal Handling Signal to notify a process that a particular event has occurred. Default or user defined signal handler. Synchronous signal is related to the operation performed by a running process. Illegal memory access or division by zero. Asynchronous signal is caused by an event external to a running process. Terminating a process (<control><C>) or a timer expires. Options for delivering signals in a multithreaded process: Signal to the thread to which the signal applies. Signal to all threads. Signal to certain threads. Signal to a specific thread. CSC 4103: Operating System

13 Thread Pools Create a number of threads at process startup and place them into a pool where they sit and wait for work. e.g. for multithreading a web server. A thread from the pool is activated on the request, and it returns to the pool on completion. Benefits of thread pool: Faster service Limitation on the number of threads, according to the need. Thread-pool-architecture allows dynamic adjustment of pool size. CSC 4103: Operating System

14 Specific Data Certain data required by a thread. These data are not shared with the other threads. Example of thread-specific data: In a transaction processing system, different transaction services will be provided by different threads. CSC 4103: Operating System

15 Scheduler Activations
Communication between the user-thread library and the kernel threads. An intermediate data structure known as LWP. User thread runs on a virtual processor (LWP) Corresponding kernel thread runs on a physical processor Each application gets a set of virtual processors from OS Application schedules threads on these processors Kernel informs an application about certain events issuing upcalls which are handled by thread library. CSC 4103: Operating System

16 Pthreads The POSIX standard defining API for thread creation and synchronization. A set of C language programming types and procedure calls. Implemented with pthread.h header/include file and a thread library. Common in UNIX operating systems. CSC 4103: Operating System

17 Multithreading with Pthread API
Two threads: initial thread in main function and a new thread performing summation in runner function # include <pthread.h> void *runner(void *param); main (int argc, char *argv[1]){ pthread_t tid; pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&tid, &attr, runner, argv[1]); pthread_join(tid, Null); } CSC 4103: Operating System

18 Multithreading with Pthread API (Contd.)
The new thread begins control in the runner function to perform summation of a non-negative integer. void *runner(void *param) { int upper = atoi(param); int I; sum = 0; if (upper > 0){ for (I = 1; I <= upper; I++) sum += i); } pthread_exit(0); CSC 4103: Operating System

19 Java Threads Summation thrd = new Summation(); thrd.start();
Create a new thread that is derived from the Thread class: Summation thrd = new Summation(); Override the run method of the Thread class by calling start method thrd.start(); Two threads: one starts execution of main method. second begins execution in its run method. CSC 4103: Operating System

20 Win32 Threads: Windows XP Implementation
Window XP implements the Win32 API and one-to-one and many-to-many mappings. Data structures of a thread: ETHREAD (executive thread block) Pointer to the belonging process Pointer to the corresponding KTHREAD Address to the routine in which the thread starts control. KTHREAD (kernel thread block) Scheduling and synchronization information Kernel stack Pointer to TEB. TEB (thread environment block) User-space data structure for user-level thread User stack Array of thread-specific data (called thread-local storage). CSC 4103: Operating System

21 Summary A thread is a flow of control within the process. A process can have several different flows of control or activity within the same address space. Multithreading benefits - increased responsiveness, resource sharing, economy and concurrency. User level threads are visible to programmer and are unknown to kernel – a thread library manages them. Kernel level threads are supported by OS. Three different models: many-to-one, one-to-one, and many-to-many. Multithreading is challenging: many thread-specific issues. Thread libraries: Pthreads, Win 32 threads and Java threads. CSC 4103: Operating System


Download ppt "Source: Operating System Concepts by Silberschatz, Galvin and Gagne."

Similar presentations


Ads by Google