1 of 43

Chapter 4: Threads

2 of 43

Chapter 4: Threads

  • Overview
  • Multithreading Models
  • Thread Libraries
  • Threading Issues

3 of 43

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.

  • To examine issues related to multithreaded programming.

4 of 43

A thread is a path of execution within a process. A process can contain multiple threads.

A thread is also known as lightweight process. The idea is to achieve parallelism by dividing a process into multiple threads.

For example, In a browser, multiple tabs can be different threads.

MS Word uses multiple threads: one thread to format the text,

another thread to process inputs, etc

The primary difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces.

Threads

5 of 43

Threads

Thread is a single sequence stream within a process.

Threads have same properties as of the process so they are called as light weight processes.

Threads are executed one after another but gives the illusion as if they are executing in parallel.

Each thread has different states. Each thread has  

  1. A program counter  
  2. A register set  
  3. A stack space

 

Threads are not independent of each other as they share the code, data, OS resources etc.  

6 of 43

Threads

Similarity between Threads and Processes –  

  • Only one thread or process is active at a time 

  • Within process both execute sequential.

  • Both can create children.

 

Differences between Threads and Processes – 

 

  • Threads are not independent, processes are. 

  • Threads are designed to assist each other, processes may or may not do it. 

7 of 43

Single and Multithreaded Processes

8 of 43

Need of Thread

  • It takes far less time to create a new thread in an existing process than to create a new process.

  • Threads can share the common data, they do not need to use Inter- Process communication.

  • Context switching is faster when working with threads.

  • It takes less time to terminate a thread than a process.

9 of 43

Benefits

  • Responsiveness

A program may be allowed to continue running even if part of it is

blocked. Thus, increasing responsiveness to the user.�

  • Resource Sharing

By default, threads share the memory (and resources) of the process to

which they belong. Thus, an application is allowed to have several

different threads of activity within the same address-space.

  • Economy

Allocating memory and resources for process-creation is costly. Thus, it is

more economical to create and context-switch threads.�

  • Scalability

In a multiprocessor architecture, threads may be running in parallel on

different processors. Thus, parallelism will be increased.

10 of 43

11 of 43

Types of Threads

In the operating system, there are two types of threads.

  1. Kernel level thread.

  • User-level thread.

User-level thread

  • The operating system does not recognize the user-level thread.

  • User threads can be easily implemented and it is implemented by the user.

  • If a user performs a user-level thread blocking operation, the whole process is blocked.

  • The kernel level thread does not know nothing about the user level thread.

  • The kernel-level thread manages user-level threads as if they are single-threaded processes? examples: Java thread, POSIX threads, etc.

12 of 43

User-level thread

13 of 43

Advantages of User-level threads

  1. It is faster and efficient.
  2. Context switch time is shorter than the kernel-level threads.
  3. It does not require modifications of the operating system.
  4. The register, PC, stack, and mini thread control blocks are stored in the

address space of the user-level process.

5. It is The user threads can be easily implemented than the kernel thread.

6. Simple to create, switch, and synchronize threads without the

intervention of the process.

Disadvantages of User-level threads

  1. User-level threads lack coordination between the thread and the kernel.

  • If a thread causes a page fault, the entire process is blocked.

14 of 43

Kernel level thread

  • The kernel thread recognizes the operating system.

  • There is a thread control block and process control block in the system for each thread and process in the kernel-level thread.

  • The kernel-level thread is implemented by the operating system.

  • The kernel knows about all the threads and manages them.

  • The kernel-level thread offers a system call to create and manage the threads from user-space.

  • Context switch time is longer in the kernel thread.

Example: Window Solaris.

15 of 43

16 of 43

Advantages of Kernel-level threads

  1. The kernel-level thread is fully aware of all threads.

  • The scheduler may decide to spend more CPU time in the process of

threads being large numerical.

3.The kernel-level thread is good for those applications that block the

frequency.

Disadvantages of Kernel-level threads

  1. The kernel thread manages and schedules all threads.

  • The implementation of kernel threads is difficult than the user thread.

  • The kernel-level thread is slower than user-level threads.

17 of 43

  • Multicore systems putting pressure on programmers, challenges include

    • Dividing activities

    • Balance

    • Data splitting

    • Data dependency

    • Testing and debugging

Multicore Programming

18 of 43

Concurrent Execution on a Single-core System

19 of 43

User Threads

  • Thread management done by user-level threads library�
  • Three primary thread libraries:
    • POSIX Pthreads
    • Win32 threads
    • Java threads

20 of 43

Kernel Threads

  • Supported by the Kernel�
  • Examples
    • Windows XP/2000
    • Solaris
    • Linux
    • Tru64 UNIX
    • Mac OS X

21 of 43

S.N.

User-Level Threads

Kernel-Level Thread

1

User-level threads are faster to create and manage.

Kernel-level threads are slower to create and manage.

2

Implementation is by a thread library at the user level.

Operating system supports creation of Kernel threads.

3

User-level thread is generic and can run on any operating system.

Kernel-level thread is specific to the operating system.

4

Multi-threaded applications cannot take advantage of multiprocessing.

Kernel routines themselves can be multithreaded.

Difference between User-Level & Kernel-Level Thread

22 of 43

Multithreading Models

Multi threading-It is a process of multiple threads executes at same time.

Multithreading allows the application to divide its task into individual threads.

In multi-threads, the same process or task can be done by the number of threads, or we can say that there is more than one thread to perform the task in multithreading. With the use of multithreading, multitasking can be achieved.

Many operating systems support kernel thread and user thread in a combined way. Example of such system is Solaris.

Application-level multithreads runs on multiple processors controlled by Kernel.

23 of 43

Multithreading Models

24 of 43

Multithreading Models

Importance of Multithreading in Operating System

Multithreading facilitates concurrent execution of multiple program codes and these program codes are easily managed in Java program including its sequence of execution and it plays an important role in

  • Improving Front end response to the users.

  • Enhancing application performance.

  • Effective utilization of Computer resources.

  • Low maintenance cost.

  • Faster completion of tasks due to parallel operation.

  • Simplifying development process and increasing productivity.

25 of 43

Multithreading Models

The relationship between application threads and kernel threads can be categorized into:

  1. Many-to-one model

  • One-to-one model

  • Many-to-many model.

26 of 43

Many to Many Model 

In this model, we have multiple user threads multiplex to same or lesser number of kernel level threads.

Number of kernel level threads are specific to the machine, advantage of this model is if a user thread is blocked we can schedule others user thread to other kernel thread.

Thus, System doesn’t block if a particular thread is blocked. 

This model provides the best accuracy on concurrency.

27 of 43

Many to Many Model 

Example:

Solaris prior to version 9

Windows NT/2000 with the ThreadFiber package

28 of 43

Many to One Model

Many-to-one model maps many user level threads to one Kernel-level thread.

Thread management is done in user space by the thread library. When thread makes a blocking system call, the entire process will be blocked.

Only one thread can access the Kernel at a time, so multiple threads are unable to run in parallel on multiprocessors.

If the user-level thread libraries are implemented in the operating system in such a way that the system does not support them, then the Kernel threads use the many-to-one relationship modes.

29 of 43

Many to One Model

Examples:

Solaris Green Threads

GNU Portable Threads

30 of 43

One to One Model

There is one-to-one relationship of user-level thread to the kernel-level thread.

This model provides more concurrency than the many-to-one model.

It also allows another thread to run when a thread makes a blocking system call.

It supports multiple threads to execute in parallel on microprocessors.

Disadvantage of this model is that creating user thread requires the corresponding Kernel thread.

31 of 43

One to One Model

Examples

Windows NT/XP/2000

Linux

Solaris 9 and later

32 of 43

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

33 of 43

34 of 43

Thread Libraries

  • Thread library provides programmer with API for creating and managing threads.

  • Two primary ways of implementing

  • Library entirely in user space. All code and data structures for the library exist in a local function call in user space and not in a system call.

    • Kernel-level library supported by the OS. In this case the code and data structures for the library exist in kernel space.

35 of 43

Thread Libraries

The main thread libraries which are used are

  • POSIX threads − Pthreads, the threads extension of the POSIX standard, may be provided as either a user level or a kernel level library.

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

  • The Pthread program must always have a pthread.h header file.

36 of 43

Thread Libraries

  • JAVA thread − The JAVA thread API allows threads to be created and managed directly as JAVA programs.

  • Java threads are managed by the JVM.

  • Typically implemented using the threads model provided by underlying OS.

  • Java threads may be created by:�
    • Extending Thread class.
    • Implementing the Runnable interface.

37 of 43

Thread Libraries

  • WIN 32 thread − The windows thread library is a kernel level library available on windows systems.

  • Creation of thread in Win2 library is similar to pthread library.

  • To create a thread using the Win32 library always include windows.h header file in the program.

  • The Win32 thread library is a kernel-level library which means invoking the Win32 library function results in a system call.

38 of 43

Threading Issues

  • Semantics of fork() and exec() system calls

  • Thread cancellation of target thread
    • Asynchronous or deferred

  • Signal handling

  • Thread pools

  • Thread-specific data

39 of 43

Semantics of fork() and exec()

  • Does fork() duplicate only the calling thread or all threads?

  • One that duplication all threads- The child thread does not call exec() after Forking.

  • Only the thread that invoked Fork() system call is duplicated-exec() is called immediately after Forking.

40 of 43

Thread Cancellation

  • Terminating a thread before it has finished.

  • Two general approaches:

    • Asynchronous cancellation terminates the target thread

(a thread which we want to cancel is termed ) immediately.

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

41 of 43

Signal Handling

  • Signals are used in UNIX systems to notify a process that a particular event has occurred.

  • A signal handler is used to process signals
    1. Signal is generated by particular event.
    2. Signal is delivered to a process.
    3. Signal is handled.

  • Options:
    • Deliver the signal to the thread to which the signal applies.
    • Deliver the signal to every thread in the process.
    • Deliver the signal to certain threads in the process.
    • Assign a specific thread to receive all signals for the process.

42 of 43

Thread Pools

  • Create a number of threads in a pool where they await work.

  • 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 bound to the size of the pool.

Whenever the request arrives at the server, it invokes a thread from the pool and assigns it the request to be serviced.

The thread completes its service and return back to the pool and wait for the next request.

43 of 43

Thread Specific Data

  • If allows each thread to have its own copy of data. So the specific data associated with the specific thread is referred to as thread-specific data.

  • Useful when you do not have control over the thread creation process (i.e., when using a thread pool).