Lecture_6-CSCIU511-Jahangir_Majumder-Spring_2025

Lecture Overview

  • Course: CSCI U511 01 Operating Systems

  • Instructor: AKM Jahangir A Majumder, PhD

  • Date: January 30, 2025

  • Note: Some slides adapted from previous instructors; some figures from the textbook.

Agenda for Today's Lecture

  • Review of Previous Topics: Finished discussion on the Programming Interface.

  • Current Topics: Continuation on Concurrency and Threads:

    • Concurrency

    • Threads

    • Single-threaded Approaches

    • Multithreaded Approaches

    • Implementing Threads

    • Multi-threaded OS Kernel

    • Thread Context Switch

  • Homework: Homework 2 posted on Blackboard, due on Feb. 4.

  • Quiz: Quiz 2 scheduled for Feb. 6, covering lectures 3-5.

  • Presentation: Faculty candidate presentation on Feb. 4.

Thread Lifecycle

  • Creation:

    • Functions involved: sthread_create(), sthread_yield(), sthread_exit(), sthread_join().

  • States:

    • Init: Being created.

    • Ready: In the wait list.

    • Running: Currently executing.

    • Finished: Completed, waiting for cleanup.

  • Yielding: Suspend an active thread.

Per-Thread State Locations

  • Thread Control Block (TCB):

    • Contains thread’s state, registers, and other info.

  • States and Locations:

    • INIT: Being Created → TCB

    • READY: Ready List → TCB

    • RUNNING: Running List → Processor

    • WAITING: Waiting List with Synchronization Variables

    • FINISHED: Finished List → Deleted.

Fork/Join Concurrency

  • Threads can create child threads, wait for their completion.

  • Data Sharing:

    • Only shared before fork/after join.

  • Use Cases:

    • Example: Web server creates a new thread per connection.

    • Example: Parallel sorting and memory operations.

Thread Data Structures

  • Multi-threaded OS kernel has both per-thread and shared state.

  • Thread Control Block (TCB):

    • Stores thread computation, saved registers, stack pointers, current state.

  • Shared State includes global variables and heap.

Single vs. Multithreaded Processes

  • Visual differences between models: Single-threaded and multithreaded.

  • Single-threaded Process:

    • Control Block for user and kernel stacks.

  • Multithreaded Process:

    • Multiple Thread Control Blocks for each thread managing its stack.

Comparison: Threads vs Processes

  • Threads:

    • No standalone data segments, must live within a process.

    • Inexpensive creation/context switching.

    • Stack reclaimed by process if the thread dies.

  • Processes:

    • Has dedicated code, data, and heap.

    • Creation and context switching are more resource-intensive.

    • If a process dies, all associated threads are terminated.

Key Benefits of Threads

  • Faster creation and termination times than processes.

  • Reduced context-switching time enhances efficiency.

  • Allows programs to remain responsive during blockages.

  • Resource sharing effectively reduces memory overhead.

  • Utilizing multi-processing architectures increases concurrency.

Implementing Threads: Roadmap

  • Examine underlying data structures and thread operations.

  • Types of Threads: Kernel threads and user-level threads.

  • Kernel threads mirror single-threaded user processes in functionality.

  • System Calls: Kernel thread operations through system calls versus user-level threads without them.

Thread Implementation Details

  • Address space definition by processes, shared by threads.

  • Process Control Block (PCB) contains process-specific information.

  • Thread Control Block (TCB) holds thread-specific details.

Multithreaded OS Kernel Configuration

  • Kernel threads coexist with user-level processes.

  • Each kernel thread has its own TCB, user processes maintain separate stacks.

Context Switching in Threads

  • Mechanism for switching active threads.

  • Can be voluntary (by yielding) or involuntary (by interrupt).

  • Process includes saving and restoring contextual data between threads.

Programming Dynamics of Threads

  • Execution order of threads is non-deterministic influenced by time-slicing and multiprocessor setups.

  • Atomic operations must be ensured to avoid unpredictable states.

Synchronization Challenges

  • Undefined behavior arises with concurrent read/writes.

  • Essential to maintain deterministic outcomes during operations among shared data.

  • Programming strategies for ensuring effective synchronization through constructs.

Conclusion

  • Recapped discussion on thread data structure and lifecycle.

  • Next discussion will focus on Synchronization based on textbook chapters 4.1 - 4.5.