Threads in Operating Systems

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following is NOT a typical benefit of using multithreading in applications?

  • Simplified code
  • Increased efficiency
  • Reduced memory footprint (correct)
  • Improved responsiveness

In the context of multithreading, what does concurrency primarily refer to?

  • The division of a program into multiple, independent processes.
  • The ability of a system to perform multiple tasks simultaneously using multiple cores.
  • The ability of a single processor to handle multiple tasks making progress. (correct)
  • The actual simultaneous execution of multiple tasks.

With respect to parallelism, what is meant by 'data splitting'?

  • Splitting a large task into subtasks and assigning each to a different thread.
  • Dividing the processor workload equally between threads.
  • Dividing data into smaller segments for parallel processing. (correct)
  • Splitting a code into different functions.

What does Parallelism imply in the context of multi-core processors?

<p>Tasks can execute at the same time. (B)</p> Signup and view all the answers

According to Amdahl's Law, what factor has the most disproportionate effect on performance gains when adding additional cores?

<p>The serial portion of the application (C)</p> Signup and view all the answers

In the context of threading, what is a 'user thread'?

<p>A thread managed by a user-level threads library. (A)</p> Signup and view all the answers

What is a key difference between kernel threads and user threads?

<p>Kernel threads are visible to the operating system, while user threads are not. (A)</p> Signup and view all the answers

Which of the following is a characteristic of the many-to-one multithreading model?

<p>Many user-level threads are mapped to a single kernel thread. (C)</p> Signup and view all the answers

In the context of multithreading models, what is the primary advantage of the one-to-one model?

<p>It allows for greater concurrency than the many-to-one model. (C)</p> Signup and view all the answers

Which of the following models allows a user thread to be bound to a kernel thread?

<p>Two-level (C)</p> Signup and view all the answers

What is the primary role of a thread library?

<p>To provide an API for creating and managing threads. (B)</p> Signup and view all the answers

Which of the following statements about Pthreads is correct?

<p>Pthreads is a POSIX standard defining an API for thread creation and synchronization. (A)</p> Signup and view all the answers

What is the primary advantage of using thread pools?

<p>They eliminate the overhead of creating new threads for each request. (A)</p> Signup and view all the answers

In the context of implicit threading, what is the main goal of OpenMP?

<p>To offer compiler directives and an API for parallel programming in shared-memory environments. (A)</p> Signup and view all the answers

What is the primary function of Grand Central Dispatch (GCD) in macOS and iOS?

<p>To automatically manage threads using thread pools and dispatch queues. (B)</p> Signup and view all the answers

When dealing with multithreaded programs, what is a key consideration regarding the fork() system call?

<p>The behavior of <code>fork()</code> regarding thread duplication is system-dependent. (B)</p> Signup and view all the answers

In the context of signal handling, what is a 'signal handler'?

<p>A function that is executed when a specific signal is received. (A)</p> Signup and view all the answers

In multithreaded programs, what is a potential strategy for delivering a signal?

<p>Deliver the signal to the thread to which the signal applies. (D)</p> Signup and view all the answers

What is the primary difference between asynchronous and deferred thread cancellation?

<p>Asynchronous cancellation terminates the target thread immediately, while deferred cancellation allows the target thread to periodically check if it should be cancelled. (A)</p> Signup and view all the answers

What is the purpose of thread-local storage (TLS)?

<p>To provide each thread with its own copy of certain data. (A)</p> Signup and view all the answers

What is the role of Scheduler Activations in multithreading?

<p>They enable communication between user-level and kernel-level thread management systems. (D)</p> Signup and view all the answers

In Windows, what is the 'context' of a thread composed of?

<p>The register set, stacks, and private storage area. (B)</p> Signup and view all the answers

In the Windows operating system, what is the purpose of ETHREAD?

<p>It contains a pointer to the process the thread belongs to and a pointer to KTHREAD. (D)</p> Signup and view all the answers

In the Windows operating system, what is contained in the KTHREAD data structure?

<p>Scheduling and synchronization information and a kernel-mode stack (D)</p> Signup and view all the answers

What is the primary function of the clone() system call in Linux with regards to threads?

<p>To allow a child task to share the address space of the parent task. (D)</p> Signup and view all the answers

In Linux, what does the CLONE_VM flag signify when used with the clone() system call?

<p>The same memory space is shared. (A)</p> Signup and view all the answers

Which of the following best describes the concept of data parallelism?

<p>Distributing subsets of the same data across multiple cores, performing the same operation on each. (A)</p> Signup and view all the answers

What is the key benefit of using threads over processes?

<p>Thread creation is light-weight and cheaper than process creation. (C)</p> Signup and view all the answers

In the context of concurrency, what does balance refer to?

<p>Ensuring that each thread has an equal amount of work. (A)</p> Signup and view all the answers

Assume an application is 80% parallel and 20% serial. According to Amdahl's Law, what is the maximum speedup achievable with an infinite number of cores?

<p>5x (A)</p> Signup and view all the answers

Which of the following is NOT a valid thread library?

<p>Solaris threads (B)</p> Signup and view all the answers

Which of the following is a disadvantage of using thread pools?

<p>Applications are bound to the pool size. (C)</p> Signup and view all the answers

Which of the following problems are introduced in multithreaded programming?

<p>All of the above. (D)</p> Signup and view all the answers

For multithreaded programming, what is the goal of concurrency?

<p>Making progress on more than one task. (A)</p> Signup and view all the answers

Which of the following is not a goal of multithreaded programming?

<p>Security. (A)</p> Signup and view all the answers

Which of the following is not a strategy of implicit threading?

<p>Pthread. (C)</p> Signup and view all the answers

Flashcards

What is a Thread?

A fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.

Multithreaded Applications

Modern applications use multiple threads to perform various tasks.

Responsiveness (Threads)

Continued execution if part of a process is blocked; important for user interfaces.

Resource Sharing (Threads)

Threads share resources of the process; it is easier than shared memory or message passing.

Signup and view all the flashcards

Economy (Threads)

Cheaper than process creation; thread switching lowers overhead than context switching.

Signup and view all the flashcards

Scalability (Threads)

A process can take advantage of multiprocessor architectures.

Signup and view all the flashcards

Multicore Programming Challenges

Pressure on programmers caused by multicore or multiprocessor systems.

Signup and view all the flashcards

Data Parallelism

Distributes subsets of the same data across multiple cores, performing the same operation on each.

Signup and view all the flashcards

Task Parallelism

Distributes threads across cores, with each thread performing a unique operation.

Signup and view all the flashcards

User Threads

Management is done by a user-level threads library.

Signup and view all the flashcards

Kernel Threads

Supported directly by the Kernel.

Signup and view all the flashcards

Many-to-One Model

Many user-level threads mapped to a single kernel thread.

Signup and view all the flashcards

One-to-One Model

Each user-level thread maps to a kernel thread.

Signup and view all the flashcards

Many-to-Many Model

Allows many user-level threads to be mapped to many kernel threads.

Signup and view all the flashcards

Two-level Model

Similar to M:M, except it allows a user thread to be bound to a kernel thread.

Signup and view all the flashcards

Thread Library

Provides programmer with API for creating and managing threads.

Signup and view all the flashcards

Pthreads

POSIX standard API for thread creation and synchronization.

Signup and view all the flashcards

Implicit Threading

Growing in popularity; compilers and run-time libraries manage threads.

Signup and view all the flashcards

Thread Pools

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

Signup and view all the flashcards

OpenMP

A set of compiler directives and an API for C, C++, FORTRAN.

Signup and view all the flashcards

Grand Central Dispatch

Apple technology for Mac OS X and iOS operating systems.

Signup and view all the flashcards

Serial Dispatch Queue

Code blocks removed in FIFO order; queue is per process.

Signup and view all the flashcards

Concurrent Dispatch Queue

Blocks removed in FIFO order, but several may be removed at a time.

Signup and view all the flashcards

Threading Issues

Includes the semantics of fork() and exec() system calls, signal handling, and thread cancellation.

Signup and view all the flashcards

Signals

Used in UNIX systems to notify a process that a particular event has occurred.

Signup and view all the flashcards

Thread Cancellation

Terminating a thread before it has finished.

Signup and view all the flashcards

Asynchronous Cancellation

Terminates the target thread immediately.

Signup and view all the flashcards

Deferred Cancellation

Allows the target thread to periodically check if it should be cancelled.

Signup and view all the flashcards

Thread-Local Storage (TLS)

Allows each thread to have its own copy of data.

Signup and view all the flashcards

Scheduler Activations

Models require communication to maintain an appropriate number of kernel threads allocated to the application.

Signup and view all the flashcards

Lightweight Process (LWP)

Intermediate data structure between user and kernel threads.

Signup and view all the flashcards

Upcalls

Communication mechanism from the kernel to the upcall handler in the thread library.

Signup and view all the flashcards

Linux Threads as Tasks

Linux refers to threads as tasks, using clone() system call.

Signup and view all the flashcards

clone() System Call

The system call in Linux for creation of threads.

Signup and view all the flashcards

CLONE_FILES

A set of open files is shared among the cloned tasks

Signup and view all the flashcards

CLONE_FS

File-system information is shared among clone tasks.

Signup and view all the flashcards

CLONE SIGHAND

Task scheduler activations

Signup and view all the flashcards

CLONE_VM

Virtual memory in Linux task scheduling is clone

Signup and view all the flashcards

Study Notes

Threads

  • Threads are a fundamental unit of CPU utilization in multithreaded computer systems.
  • The use of threads simplifies code and increases efficiency.
  • Kernels are generally multithreaded

Objectives of Thread

  • Introduce the concept of a thread
  • Discuss the APIs for Pthreads, Windows, and Java thread libraries.
  • Explore strategies for implicit threading.
  • Examine issues related to multithreaded programming.
  • Cover operating system support for threads in Windows and Linux.

Motivation

  • Most modern applications are multithreaded.
  • Threads run within an application to implement multiple tasks.
  • Process creation is heavy-weight while thread creation is light-weight.

Multithreaded Server Architecture

  • A client sends a request to the server
  • The server creates a new thread to handle the request.
  • The server resumes listening for additional client requests.

Benefits of Threads

  • Responsiveness: Continued execution if part of the process is blocked.
  • Resource Sharing: Threads share resources, easier than shared memory or message passing.
  • Economy: Thread creation is cheaper than process creation.
  • Scalability: Processes can take advantage of multiprocessor architectures.

Multicore Programming

  • Multicore or multiprocessor systems put pressure on programmers to utilize resources in optimal way.
  • Challenges of multicore programming include dividing activities, balancing workloads, splitting data, handling data dependencies, and testing/debugging.
  • Parallelism: A system performs more than one task simultaneously.
  • Concurrency: More than one task makes progress.
  • Concurrency can be achieved via a single processor/core or scheduler.
  • Data parallelism distributes subsets of the same data across multiple cores, applying the same operation on each.
  • Task parallelism distributes threads across cores, each performing a unique operation.
  • CPUs have cores as well as hardware threads.
  • Oracle SPARC T4 has 8 cores and 8 hardware threads per core.

Amdahl’s Law

  • Amdahl's Law identifies performance gains from adding cores to an application with serial and parallel components.
  • Performance is limited by serial sections of code
  • The formula for speedup is: speedup ≤ 1 / (S + (1-S)/N), where S is the serial portion and N is the number of cores.

User Threads vs Kernel Threads

  • User threads are managed by a user-level threads library.
  • Three primary thread libraries are POSIX Pthreads, Windows threads, and Java threads
  • Kernel threads are supported by the Kernel.
  • Examples of systems that use kernel threads are Windows, Solaris, and Linux.

Multithreading Models

  • Many-to-One: Many user-level threads mapped to a single kernel thread.
  • One-to-One: Each user-level thread maps to a kernel thread.
  • Many-to-Many: Allows many user-level threads to be mapped to many kernel threads.
  • Two-Level: Similar to Many-to-Many model, but allows a user thread to be bound to kernel thread

Thread Libraries

  • A thread library provides an API for creating and managing threads.
  • Libraries can be entirely in user space or supported by the OS kernel.

Pthreads

  • Pthreads is a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.
  • It specifies the behavior of the thread library, while the implementation is up to the library developer.
  • Pthreads is common in UNIX operating systems like Solaris, Linux, and Mac OS X.

Implicit Threading

  • The number of threads increase, program correctness becomes more difficult with explicit threads.
  • Creation and management of threads is done by compilers and run-time libraries rather than programmers.
  • Methods: Thread Pools, OpenMP and Grand Central Dispatch.
  • Other options include Microsoft Threading Building Blocks (TBB), java.util.concurrent package.

Thread Pools

  • Thread pools create a number of threads that await work.
  • The advantages of using thread pools are that it is usually slightly faster and limit the number of threads.
  • Task scheduling becomes more flexible and tasks can be scheduled to run periodically.
  • Windows API supports thread pools.

OpenMP

  • OpenMP provides support for parallel programming in shared-memory environments.
  • It identifies parallel regions, which are blocks of code that can run in parallel.
  • Directives are used to create as many threads as there are cores, and code can direct loops to run in parallel.

Grand Central Dispatch

  • Grand Central Dispatch (GCD) is an Apple technology for Mac OS X and iOS operating systems.
  • GCD includes extensions to C, C++, an API, and a run-time library.
  • It allows identification of parallel sections and manages threading details.
  • Blocks, which contain code, are placed in dispatch queues and assigned to available threads from a thread pool when they are removed from the queue.

Types of Grand Central Dispatch

  • Serial – blocks removed in FIFO order, queue is per process, called main queue
  • Programmer can create queues within program
  • Concurrent – removed in FIFO order but several may be removed at the same time
  • 3 System wide queues with priorities low, default, high

Threading Issues

  • Semantics of fork() and exec() system calls.
  • Signal handling is the delivery of signals to threads.
  • Thread cancellation is terminating a thread before it completes, achieved through asynchronous or deferred cancellation.
  • Thread-local storage allows each thread to have its own copy of data and is different from local variables
  • Scheduler Activations.

Semantics of Fork and Exec

  • Fork duplicates only the calling thread or all threads in the program.
  • Some UNIX systems have two version of fork
  • Exec usually works as normal - replace the running process including all threads

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.
  • A signal is generated by an event and delivered to a process.
  • The signal is be handled by default (kernel-defined) or user-defined logic.
  • Every signal has a default handler that the kernel runs when handling the signal
  • A user-defined signal handler can override default
  • For single-threaded processes, the signal is generally delivered to the process.

Where Signals are Delivered

  • Multi-threaded signal delivery options:
  • To the thread to which it applies
  • To every thread in the process
  • To certain threads in the process
  • Assigning a specific thread to receive all signals for the process

Thread Cancellation

  • Terminating of a thread before it has finished
  • Asynchronous cancellation terminates the target thread immediately.
  • Deferred cancellation allows the thread to periodically check if it should be cancelled.

Thread Cancellation requests

  • Involves invoking a thread cancellation, actual cancellation depends on the thread state.
  • If thread has cancellation disabled, cancellation remains pending until the thread enables it.
  • Default type is deferred
  • Cancellation only occurs when the thread reaches cancellation point.

On Linux systems

  • Thread cancellation is handled through signals

Thread Local Storage

  • Thread-local storage (TLS) allows each thread to have its own copy of data.
  • Useful when there is no control over the thread creation process (i.e., when using a thread pool)
  • It is different from local variables, which are visible only during single function invocation. TLS is visible across functions invocations
  • TLS is unique to each thread

Scheduler Activations

  • Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application
  • Typically use an intermediate data structure between user and kernel threads - lightweight process(LWP)
  • Appears to be a virtual processor on which process can schedule user thread to run
  • Each LWP attached to kernel thread
  • Scheduler activations provide upcalls - a communication mechanism from the kernel to the upcall handler in the thread library
  • This communication allows an application to maintain the correct number kernel threads

Operating System Examples

  • Windows Threads and Linux Threads

Windows Threads

  • Windows implements the Windows API – primary API for Win 98, Win NT, Win 2000, Win XP, and Win 7
  • Implements the one-to-one mapping, kernel-level.
  • Each thread contains: a thread ID, a register set representing the state of the processor, separate user and kernel stacks, and a private data storage.
  • Thread-Local Storage (TLS) is used by run-time and dynamic-link libraries (DLLs).
  • The register set, stacks, and private storage area are known as the context of the thread. The primary data structures of a thread include:
  • ETHREAD (executive thread block) includes pointer to process to which thread belongs and to KTHREAD, in kernel space
  • KTHREAD (kernel thread block) scheduling and synchronization info, kernel-mode stack, pointer to TEB, in kernel space
  • TEB (thread environment block) thread id, user-mode stack, thread-local storage, in user space

Linux Threads

  • Linux refers to them as tasks rather than threads.
  • Thread creation is done through clone() system call
  • clone() allows a child task to share the address space of the parent task (process).
  • Used flags control behavior.
  • struct task_struct points to process data structures (shared or unique).

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Threads in Operating Systems
5 questions
Chapter 4: Threads Operating System Concepts
10 questions
Betriebssysteme HIT 3: Multithreading
49 questions
Multithreading: Concepts and Benefits
43 questions
Use Quizgecko on...
Browser
Browser