CSS 430 Book Notes Chapter 4 - Threads and Concurrency

CSS 430 Book Notes

4.1 Overview

Chapter objectives

Overview

A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter (PC), a register set, and a stack.

A traditional process has a single thread of control. If a process has multiple threads of control, it can perform more than one task at a time.

Pasted image 20230111180848.png

Motivation

An application is typically implemented as a separate process with multiple threads of control.

Example

  • An application that creates photo thumbnails from a collection of images may use a separate thread to generate a thumbnail from each separate image.
  • A web browser might have one thread display images or text while another thread retrieves data from the network.
  • A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling and grammar checking in the background.

Additionally, some applications may leverage multiple cores for processing

In general, it's more efficient to use one process that contains multiple threads.

Most OS kernels are also multithreaded with each thread performing a specific task like devices, memory management, or interrupt handling.

ps -ef will display kernel threads on a linux system

Benefits

There are four major categories when talking about threading benefits

1. Responsiveness

Multi threading allows an application to continue even if part of it is blocked or occupied., UI's can take advantage of this to manipulate the UI while a lengthy process runs in the background

2. Resource Sharing

Threads can share resources more easily since they belong to the same parent memory block

3. Economy

Since threads share the same process resources, there is less overhead for context switching

4. Scalability

You can scale greater on multiprocessor architecture.

4.2 Multicore Programming

Multithreaded programming provides a mechanism for more efficient use of multiple computing cores and improved concurrency

on a system with a single core, the execution threads will be "interleaved" over time, kinda shuffled together, as the core is only able to execute one thread at a time.
With multiple cores, concurrency means that some threads can run in parallel because the system can assign a separate thread to each core.

Programming Challenges

There are generally five areas that present challenges in programming for multicore programming

  1. Identifying tasks: This involves examining applications to find areas that can be divided into separate, concurrent tasks.
  2. Balance: programmers must also ensure that the tasks perform equal work of equal value. Using a separate execution core to run a task may not be worth the cost.
  3. Data splitting: As applications are divided into separate tasks, data accessed and manipulated by the tasks must be divided to run on separate cores.
  4. Data dependency: The data accessed by the tasks must be examined for dependencies between two or more tasks.
  5. Testing and debugging: many different execution paths are possible. Testing and debugging such concurrent programs is inherently more difficult than testing and debugging single-threaded applications.

Types of parallelism

There are essentially two types of parallelism:

Note

Data and Task Parallelism
Pasted image 20230112111225.png

4.3 Multithreading Models

Thread support may be implemented at either the user level or the kernel level. User threads are managed without kernel support, kernal threads are supported and managed by the operating system. Almost all modern operating systems support kernel threads

User and Kernel Threads

Pasted image 20230112111539.png

Many-to-one Model

This maps many user level threads to one kernel level thread. It is implemented by a thread library in the user space. If a thread makes a blocking call, the whole process will be blocked. Additionally, since only one thread can access the kernel at any one time, multiple threads cannot run in parallel on multicore systems.

Many-to-one model

Pasted image 20230112111805.png

One-to-one Model

Maps each user thread onto a kernel thread

One-to-one model

Pasted image 20230112112147.png

Many-to many Model

Multiplexes user threads to a smaller or equal number of kernel threads

Many-to-many model

Pasted image 20230112112239.png

One variation of many to many is called the two-leveled model. You can bind a thread to a kernel thread while the rest can multiplex

two-level model

Pasted image 20230112112349.png

4.4 Thread Libraries

A thread library is an api ofr creating and managing threads

Pthreads

Refers to POSIX standard for api for thread creation and synchronization
it's a specification not an implementation

4.5 Implicit Threading

Transfers the creation of management of threading from application developers to compilers and runtime libraries

This requires application devs to identify tasks that can run in parallel

Thread Pools

Create a number of threads at startup and place them into a pool that waits for work. The server submits the request to the thread pool. If there is an available thread, it's awakened and the request is serviced. If no available thread, the task is queued.

Thread Pool Benefits

Fork Join

Pasted image 20230112113157.png

OpenMP

Identifies parallel regions as blocks that may run in parallel.

Grand central dispatch

schedules tasks for run-time execution by placing them in a dispatch queue, there are two queues, serial and concurrent.

4.6 Threading issues

Question

If one thread in a program calls fork(), does the new process duplicate all threads, or is the new process single-threaded?

Some unix systems have two versions of fork. One duplicates all threads, and some duplicates only the thread invoked for the fork() system call.

Signal Handling

A signal is used to notify a process that an event has occured. It may be synchronous or asynchronous

Signals follow this pattern:

A signal may be handled by one of two possible handlers

Where should the signal be delivered?

Thread Cancellation

Involves terminating a thread before it has completed
Target Thread: the thread to be cancelled

Thread Local Storage

Each thread may need its own copy of certain data

Scheduler activations

We may require communication between kernel and thread library. This communication allows kernel threads to be dynamically adjusted

Lightweight Process: an intermediate data structure between user and kernel threads
Pasted image 20230112115047.png

Scheduler activation: a communication scheme between user-thread library and kernel. Kernel provides an application with virtual processors and the application can schedule threads onto available virtual processors.

4.7 Operating system examples

Windows thread

Data structures of a windows thread

Pasted image 20230112115242.png

Linux Threads

some flags passed when clone() is invoked

Pasted image 20230112115314.png