Threads in Operating Systems: Multicore Programming and Threading Models, Lecture notes of Operating Systems

An overview of threads in operating systems, covering topics such as multicore programming, multithreading models, thread libraries, implicit threading, and threading issues. It discusses the benefits of using threads, including responsiveness, resource sharing, economy, and scalability. The document also explores different types of parallelism, including data parallelism and task parallelism, and examines amdahls law in relation to multicore programming. Additionally, it covers user threads, kernel threads, multithreading models, and specific threading issues like semantics of fork() and exec(), signal handling, thread cancellation, and thread-local storage. The document also provides examples of windows and linux threads.

Typology: Lecture notes

2021/2022

Uploaded on 07/05/2025

haider-rafi
haider-rafi 🇸🇬

5 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Chapter 4: Threads
4.2 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Chapter 4: Threads
Overview
Multicore Programming
Multithreading Models
Thread Libraries
Implicit Threading
Threading Issues
Operating System Examples
4.3 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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, Windows, and Java
thread libraries
To explore several strategies that provide implicit threading
To examine issues related to multithreaded programming
To cover operating system support for threads in Windows and
Linux
4.4 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Motivation
Most modern applications are multithreaded
Threads run within application
Multiple tasks with the application can be implemented by
separate threads
Update display
Fetch data
Spell checking
Answer a network request
Process creation is heavy-weight while thread creation is light-
weight
Can simplify code, increase efficiency
Kernels are generally multithreaded
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Threads in Operating Systems: Multicore Programming and Threading Models and more Lecture notes Operating Systems in PDF only on Docsity!

Operating System Concepts – 9th^ Edition Silberschatz, Galvin and Gagne ©

Chapter 4: Threads

Operating System Concepts – 9th^ Edition 4.2 Silberschatz, Galvin and Gagne ©

Chapter 4: Threads

 (^) Overview  (^) Multicore Programming  (^) Multithreading Models  (^) Thread Libraries  (^) Implicit Threading  (^) Threading Issues  (^) Operating System Examples Operating System Concepts – 9th^ Edition 4.3 Silberschatz, Galvin and Gagne ©

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, Windows, and Java thread libraries  (^) To explore several strategies that provide implicit threading  (^) To examine issues related to multithreaded programming  (^) To cover operating system support for threads in Windows and Linux Operating System Concepts – 9th^ Edition 4.4 Silberschatz, Galvin and Gagne ©

Motivation

 (^) Most modern applications are multithreaded  (^) Threads run within application  (^) Multiple tasks with the application can be implemented by separate threads  (^) Update display  (^) Fetch data  (^) Spell checking  (^) Answer a network request  (^) Process creation is heavy-weight while thread creation is light- weight  (^) Can simplify code, increase efficiency  (^) Kernels are generally multithreaded

Operating System Concepts – 9th^ Edition 4.6 Silberschatz, Galvin and Gagne ©

Benefits

 (^) Responsiveness – may allow continued execution if part of process is blocked, especially important for user interfaces  (^) Resource Sharing – threads share resources of process, easier than shared memory or message passing  (^) Economy – cheaper than process creation, thread switching lower overhead than context switching  (^) Scalability – process can take advantage of multiprocessor architectures Operating System Concepts – 9th^ Edition 4.7 Silberschatz, Galvin and Gagne ©

Multicore Programming

 (^) Multicore or multiprocessor systems putting pressure on programmers, challenges include:  (^) Dividing activities  (^) Balance  (^) Data splitting  (^) Data dependency  (^) Testing and debugging  (^) Parallelism implies a system can perform more than one task simultaneously  (^) Concurrency supports more than one task making progress  (^) Single processor / core, scheduler providing concurrency Operating System Concepts – 9th^ Edition 4.8 Silberschatz, Galvin and Gagne ©

Multicore Programming (Cont.)

 (^) Types of parallelism  (^) Data parallelism – distributes subsets of the same data across multiple cores, same operation on each  (^) Task parallelism – distributing threads across cores, each thread performing unique operation  (^) As # of threads grows, so does architectural support for threading

  • (^) CPUs have cores as well as hardware threads (Simalteneious Multi Threading SMT or Hypter Threading HT)
  • Consider Oracle SPARC T4 with 8 cores, and 8 hardware threads per core  (^) Analogy  (^) Think of a core as a chef in a kitchen, capable of cooking a dish.  (^) More cores = More chefs, each working on separate dishes.  (^) More threads per core = Each chef has two hands (but still limited by overall resources in the kitchen).

Operating System Concepts – 9th^ Edition 4.13 Silberschatz, Galvin and Gagne ©

Multithreading Models

 (^) Many-to-One  (^) One-to-One  (^) Many-to-Many

Operating System Concepts – 9th^ Edition 4.19 Silberschatz, Galvin and Gagne ©

Thread Libraries

 (^) Thread library provides programmer with API for creating and managing threads  (^) Two primary ways of implementing  (^) Library entirely in user space  (^) Kernel-level library supported by the OS

Operating System Concepts – 9th^ Edition 4.25 Silberschatz, Galvin and Gagne ©

Grand Central Dispatch

 (^) A concurrency framework developed by Apple (macOS/iOS) that uses dispatch queues for managing multiple tasks efficiently.  (^) Tasks are automatically scheduled on available CPU cores.  (^) No need to create/join threads  (^) Automatically balances workload  (^) Example

  • (^) Apple technology for Mac OS X and iOS operating systems
  • (^) Extensions to C, C++ languages, API, and run-time library  (^) Assigned to available thread in thread pool when removed from queue Operating System Concepts – 9th^ Edition 4.26 Silberschatz, Galvin and Gagne ©

Threading Issues

 (^) When working with multithreading, several issues arise due to the complexity of managing multiple threads in a process. Below are some of the key challenges:

  • Semantics of fork() and exec() in Multithreaded Programs
  • Signal Handling in Multithreaded Programs
  • (^) Thread Cancellation
  • (^) Scheduler Activations
  • Thread-Local Storage (TLS) Operating System Concepts – 9th^ Edition 4.27 Silberschatz, Galvin and Gagne ©

Semantics of fork() and exec()

 (^) The behavior of fork() and exec() changes when multiple threads are present. The key question is:  (^) If one thread calls fork(), should the new process copy all threads or just the calling thread?  (^) UNIX Solutions:  (^) UNIX systems provide two versions of fork():  (^) Fork Type Behavior  (^) Full Process Fork Duplicates all threads of the parent process  (^) Threaded Fork Only the calling thread is duplicated in the child process  (^) Best Practice: In a multithreaded program, after fork(), call exec() to start a new process, avoiding inconsistencies caused by duplicated threads. Operating System Concepts – 9th^ Edition 4.28 Silberschatz, Galvin and Gagne ©

Signal Handling

 (^) Signal is an asynchronous notification sent to a process (or thread) by the operating system or another process to inform it that a specific event has occurred.  (^) In a single-threaded program, signals are delivered directly to the process, but in multithreading, handling becomes more complex.  (^) Types of Signal Handlings  (^) Synchronous Signals: Occur as a direct result of thread execution (e.g., division by zero, invalid memory access). These are delivered to the thread that caused them.  (^) Asynchronous Signals: Sent externally (e.g., SIGTERM, SIGINT from the keyboard). These signals must be handled properly in multithreaded environments.  (^) Handling Signals in Threads: Three common strategies for signal delivery:

  • Deliver the signal to all threads in the process.
  • Deliver the signal to a specific thread.
  • Deliver the signal to a single designated thread (most common approach).

Operating System Concepts – 9th^ Edition 4.29 Silberschatz, Galvin and Gagne ©

Thread Cancellation

 (^) Thread cancellation is the ability to terminate a thread before it completes execution. This is useful in tasks like timeout handling or stopping unnecessary background computations.  (^) Two general approaches or types of Thread Cancellation:  (^) Asynchronous cancellation Terminates the thread immediately (unsafe, may leave resources locked).  (^) Deferred cancellation Thread periodically checks for a cancellation request and cleans up before exiting (preferred). Operating System Concepts – 9th^ Edition 4.31 Silberschatz, Galvin and Gagne ©

Thread-Local Storage

 (^) What is Thread-local storage (TLS)  (^) TLS allows each thread to have its own copy of data.  (^) Unlike global variables (shared across threads), TLS is unique to each thread.  (^) Useful when threads need private storage (e.g., per-thread caching, logs).

  • (^) Why Use TLS?  (^) Needed when using thread pools (no direct control over thread creation).  (^) Different from local variables (TLS persists across function calls).  (^) Similar to static data, but each thread gets its own instance.
  • Example:  (^) C (GCC): __thread int myVar;  (^) C++ : thread_local int myVar;  (^) POSIX Threads (Pthreads): pthread_key_create() Operating System Concepts – 9th^ Edition 4.32 Silberschatz, Galvin and Gagne ©

Operating System Examples

 (^) Windows Threads  (^) Linux Threads

  • Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©
    • End of Chapter