CS GRIND

CSC Notes Chapter 4 Threads

Chapter Objective

  • To introduce the notion of a thread
  • To discuss the APIs for the Pthreads, Win32, and Java thread libraries
  • To examine issues related to multithreaded programming

Overview

A thread is a basic unit of CPU utilization; it comprises:

It shares with other threads belonging to the same process its

Uses

Benefits

Multicore Programming

Multithread programming provides a mechanism for more efficient use of multiple cores and improved concurrency.

Five areas present challenges in programming for multicore systems:

Multithreading Models

User threads are supported above the kernel and are managed without kernel support, whereas kernel threads are supported and managed directly by the operating system.

Many-to-One Model

Maps user-level threads to one kernel thread.

Thread management is done by the thread library in user space, so it is efficient; but the entire process will block if a thread makes a blocking system call. Also, because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors.

One-to-One Model

The only drawback to this model is that creating a user thread requires creating the corresponding kernel thread. Because the overhead of creating kernel threads can burden the performance of an application, most implementations of this model restrict the number of threads supported by the system.

Many-to-Many Model

developers can create as many user threads as necessary, and the corresponding kernel threads can run in parallel on a multiprocessor. Also, when a thread performs a blocking system call, the kernel can schedule another thread for execution.

Thread Libraries

The JVM and the Host Operating System

A Java thread may be in one of six possible states in the JVM:

The fork() and exec() System Calls

Cancellation

Thread cancellation is the task of terminating a thread before it has completed.

Cancellation may occur in 2 different scenarios:

Signal Handling

A signal is used in UNIX systems to notify a process that a particular event has occurred.

Thread Pools

The mechanism that handle every new request by a new thread has at least two downsides:

One solution: Thread Pool:

Pros:

he number of threads in the pool can be set heuristically based on factorssuch as the number of CPUs in the system, the amount of physical memory, and the expected number of concurrent client requests. More sophisticated thread-pool architectures can dynamically adjust the number of threads in the pool according to usage patterns.

Thread-Specific Data

Scheduler Activations

Concerns the communications between kernel and the thread library.

Many systems use a data structure named light weight process, to handle this job.

scheduler activation mechanism: