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

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

Threads

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

CSC 4103: Operating System

4.1

BB Karki, LSU

CSC 4103: Operating System

4.2

BB Karki, LSU

Overview
s Process can have 3 a single thread of control or activity 3 multiple threads of control or activity. s A thread is a flow of control within a process 3 a basic unit of CPU utilization also called a LWP 3 thread ID, program counter, register set and stack 3 all threads share the same address space of their process. s Multithreaded computer systems are common. 3 e.g., desktop PCs 3 Web browser can have two threads, one for display and the other for data retrieving. s Multithreading models s Threading issues s Thread libraries s OS examples
CSC 4103: Operating System 4.3 BB Karki, LSU

Single and Multithreaded Processes

s 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.4 BB Karki, LSU

Benefits
s Increased responsiveness to user: 3 A program continues running with other threads even if part of it is blocked or performing a lengthy operation in one thread. s Resource sharing 3 Threads share memory and resources of their process. s Economy 3 Less time consuming to create and manage threads than processes as threads share resources, 3 e.g., thread creating is 30 times faster than process creating in Solaris. s Utilization of multiprocessor architectures 3 Increases concurrency because different threads can run in parallel on different processors (CPUs).

CSC 4103: Operating System

4.5

BB Karki, LSU

Thread Types
s User Threads

Threads are implemented at the user level by a thread library


3 Library provides support for thread creation, scheduling and management. 3 User threads can be faster to create and manage. 3 Can be more portable across operating systems. 3 Can have trouble taking full advantage of hardware parallelism.

s Kernel Threads

Supported and managed directly by the OS.


3 Thread creation, scheduling and management take place in kernel space. 3 Can be slower to create and manage. 3 Can provide more direct access to hardware parallelism. 3 Examples: Windows XP+, Linux, Mac OS X.

s Relationship between user threads and kernel threads.

CSC 4103: Operating System

4.6

BB Karki, LSU

CSC 4103: Operating System

4.7

BB Karki, LSU

Multithreading Models

Three common ways of establishing a relationship between user-level threads and kernel-level threads
s Many-to-One s One-to-One s Many-to-Many 3 Two-Level

CSC 4103: Operating System

4.8

BB Karki, LSU

Many-to-One Model
s Many user-level threads

mapped to single kernel thread.


s Easier thread management. s Blocking-problem. s No concurrency. s Examples: 3 Solaris green threads 3 GNU portable threads

CSC 4103: Operating System

4.9

BB Karki, LSU

One-to-One Model
s Each user-level thread maps to a kernel thread. s Overhead of creating kernel threads, one for each user thread. s No blocking problem s Provides concurrency. s Examples: Windows NT/XP/2000, Linux, Solaris 9 or later

CSC 4103: Operating System

4.10

BB Karki, LSU

Many-to-Many Model
s Allows many user level threads to be mapped to many kernel

threads. s Allows the OS to create a sufficient number of kernel threads. s Users can create as many as user threads as necessary.
s No blocking and

concurrency problems.
s Examples: Solaris

prior to version 9, NT/2000 with the ThreadFiber package.


CSC 4103: Operating System 4.11 BB Karki, LSU

Two-Level Model
s Similar to M:M, except that it allows a user thread to be bound to

kernel thread s Examples: IRIX, HP-UX, Tru64 UNIX, Solaris 8 and earlier

CSC 4103: Operating System

4.12

BB Karki, LSU

Threading Issues fork and exec


s Change in semantics of fork() and exec() system calls. s Two versions of fork system call: 3 One duplicates only the thread that invokes the call. 3 Another duplicates all the threads, i.e., duplicates an entire process. s exec system call: 3 Program specified in the parameters to exec will replace the entire process including all threads. s If exec is called immediately after forking, duplicating all

threads is not required.

CSC 4103: Operating System

4.13

BB Karki, LSU

Cancellation

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

CSC 4103: Operating System

4.14

BB Karki, LSU

Signal Handling
s Signal to notify a process that a particular event has occurred. 3 Default or user defined signal handler. s Synchronous signal is related to the operation performed by a running

process.
3 Illegal memory access or division by zero.

s Asynchronous signal is caused by an event external to a running process. 3 Terminating a process (<control><C>) or a timer expires. s Options for delivering signals in a multithreaded process: 3 Signal to the thread to which the signal applies. 3 Signal to all threads. 3 Signal to certain threads. 3 Signal to a specific thread.

CSC 4103: Operating System

4.15

BB Karki, LSU

Thread Pools

s Create a number of threads at process startup and place

them into a pool where they sit and wait for work.
3 e.g. for multithreading a web server.

s A thread from the pool is activated on the request, and it

returns to the pool on completion.


s Benefits of thread pool: 3 Faster service 3 Limitation on the number of threads, according to the need. s Thread-pool-architecture allows dynamic adjustment of

pool size.

CSC 4103: Operating System

4.16

BB Karki, LSU

Specific Data

s Certain data required by a thread. These data are not

shared with the other threads.


s Example of thread-specific data: 3 In a transaction processing system, different transaction services will be provided by different threads.

CSC 4103: Operating System

4.17

BB Karki, LSU

Scheduler Activations
s Communication between the user-thread

library and the kernel threads.


s An intermediate data structure known as LWP

(light-weight process).
3 User thread runs on a virtual processor (LWP) 3 Corresponding kernel thread runs on a physical

processor s Each application gets a set of virtual

LWP

processors from OS
3 Application schedules threads on these

processors 3 Kernel informs an application about certain events issuing upcalls, which are handled by thread library.

CSC 4103: Operating System

4.18

BB Karki, LSU

Thread Libraries
s A thread library provides the programmer with an API for

creating and managing threads.


s Two ways of implementing a thread library 3 User level 3 Kernel level s Three main libraries 3 POSIX Pthreads (a user- or kernel-level library) 3 Win32 Threads (a kernel-level library) 3 Java Threads (neither user nor kernel, provided by JVM) Java thread API is implemented using a thread available on the host OS.

CSC 4103: Operating System

4.19

BB Karki, LSU

Pthreads

s The POSIX standard defining API for thread creation and

synchronization.
s A set of C language programming types and procedure calls. s Implemented with pthread.h header/include file and a thread

library.
s Common in UNIX operating systems.

CSC 4103: Operating System

4.20

BB Karki, LSU

Multithreading with Pthread API


Two threads: initial thread in the main function and a new thread performing summation in the 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 4.21 BB Karki, LSU

Multithreading with Pthread API (Cont.)


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 4.22 BB Karki, LSU

Win32 Threads

s Creating threads using the Win32 thread library is similar to

the Pthreads in several ways:


s Threads are created using the CreateThread() function s A set of attributes for the thread is passed to this function s Once the summation thread is created, the parent thread must

wait using the WaitForSingleObject() function.

CSC 4103: Operating System

4.23

BB Karki, LSU

Java Threads
s Java threads are managed by the JVM 3 All Java programs comprise at least a single thread of control 3 Two ways of creating Java threads s Extending Thread class: 3 Create a new thread that is derived from the Thread class, and override the run() method

class Summation extends Thread


s Implementing the Runnable interface: 3 Define a class that implements the Runnable interface, which must define a run() method containing the code to be run a separate thread class Summation implements Runnable

CSC 4103: Operating System

4.24

BB Karki, LSU

Java Threads (Cont.)


s The start () method creates the new thread and calls the

run()method: thrd.start(); s Two threads created by JVM: 3 The first (parent) thread starts execution of the main() method. 3 The second (child) thread begins execution in the run() method.
s Java thread states

CSC 4103: Operating System

4.25

BB Karki, LSU

OS Examples: Windows XP Threads


s Window XP implements the Win32 API and one-to-one and many-to-

many mappings. s Each thread contains: A thread id and context (register set, separate user and kernel stacks, and private data storage area). s Data structures of a thread:
3 ETHREAD (executive thread block)

Pointer to the belonging process Pointer to the corresponding KTHREAD Address to the routine in which the thread starts control. 3 KTHREAD (kernel thread block) Scheduling and synchronization information Kernel stack Pointer to TEB. 3 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 4.26 BB Karki, LSU

Linux

s Linux refers to them as tasks rather than threads s Thread creation is done through clone() system call s clone() allows a child task to share the address space of

the parent task (process).

CSC 4103: Operating System

4.27

BB Karki, LSU

Summary
s 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.


s Multithreading benefits - increased responsiveness, resource sharing,

economy and concurrency.


s User level threads are visible to programmer and are unknown to kernel a

thread library manages them.


s Kernel level threads are supported by OS. s Three different models: many-to-one, one-to-one, and many-to-many. s Multithreading is challenging: many thread-specific issues. s Thread libraries: Pthreads, Win 32 threads and Java threads.

CSC 4103: Operating System

4.28

BB Karki, LSU

You might also like