Sie sind auf Seite 1von 34

CA 7402 – OPERATING SYSTEMS CONCEPTS

Lecture #5 : T h r e a d s (Light weight processes)


Readings

Abraham Silberschatz, Peter B. Galvin and Greg Gagne,


“Operating System Concepts”, Ninth Edition, John Wiley
and Sons Inc, 2012.

Andrew S. Tanenbaum, “Modern Operating Systems”, Third


Edition, Addison Wesley, 2009.

CA 7204 2
Operating System Concepts
Threads
• A thread is a basic unit of processing

• Has the following components:

• Thread ID

• Program counter

• Register set

• Stack

• Shares some resources with other threads in same process

• Code section

• Data section

• OS Resources (e.g. open files, signals)

• Scheduled by the operating system

CA 7204 3
Operating System Concepts
Threads
• A heavyweight process is a process that has a single thread of control
• Can only perform a single task at a time

• A multi-threaded process is a process that has multiple threads of


control
• Can perform more than one task at a time
• Render images

• Fetch data

• Update display

• Check spelling

CA 7204 4
Operating System Concepts
Single and Multithreaded Processes

CA 7204 5
Operating System Concepts
Threads vs Processes

• A thread has no data segment or • A process has code, heap, stack,


heap other segments

• A thread cannot live on its own. It • A process has at-least one thread.
needs to be attached to a process • Threads within a process share
• There can be more than one the same I/O, code, files.
thread in a process. • If a process dies, all threads die.
• Each thread has its own stack

• If a thread dies, its stack is


reclaimed

CA 7204 6
Operating System Concepts
Benefits - Multithreaded Programming

• 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

CA 7204 7
Operating System Concepts
Multithreaded Server Architecture

Most operating-system kernels are now multithreaded. Several threads operate in


the kernel, and each thread performs a specific task, such as managing devices,
managing memory, or interrupt handling.

CA 7204 8
Operating System Concepts
Multicore Programming

• Multicore or multiprocessor systems putting pressure on programmers, challenges


include:

• Dividing activities - How can an application be divided into separate, concurrent tasks?

• Balance - How can those tasks be divided in such a way that each does an equal amount
of work?

• Data splitting - Can the data for those task be divided for processing on separate CPU
cores?

• Data dependency - Are there data dependencies between different task?

• Testing and debugging - What is the best way to debug a multithreaded program with
many different execution paths?

• 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


CA 7204 9
Operating System Concepts
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

• Consider Oracle SPARC T4 with 8 cores, and 8 hardware threads per core

CA 7204 10
Operating System Concepts
Concurrency vs. Parallelism

• Concurrent execution on single-core system:

• Parallelism on a multi-core system:

CA 7204 11
Operating System Concepts
Who manages threads?

• Two strategies

• User threads

• Thread management done by user level thread library. Kernel knows nothing
about the threads.

• Kernel threads

• Threads directly supported by the kernel.

• Known as light weight processes.

CA 7204 12
Operating System Concepts
User level threads

• Advantages:

• Fast (really lightweight) (no system call to manage


threads. The thread library does everything).

• Can be implemented on an OS that does not support


threading.

• Switching is fast. No, switch from user to protected


mode.

• Disadvantages:

• Scheduling can be an issue. (Consider, one thread that is


blocked on an IO and another runnable.)

• Lack of coordination between kernel and threads. (A


process with 1000 threads competes for a timeslice with
a process having just 1 thread.)

• Requires non-blocking system calls. (If one thread


invokes a system call, all threads need to wait)

CA 7204 13
Operating System Concepts
Kernel level threads

• Advantages:

• Scheduler can decide to give more time to a process having


large number of threads than process having small number of
threads.

• Kernel-level threads are especially good for applications that


frequently block.

• Disadvantages:

• The kernel-level threads are slow (they involve kernel


invocations.)

• Overheads in the kernel. (Since kernel must manage and


schedule threads as well as processes. It require a full thread
control block (TCB) for each thread to maintain information
about threads.)

CA 7204 14
Operating System Concepts
Thread Models

• Many-to-One

• One-to-One How do user and kernel


threads map into each other?
• Many-to-Many

CA 7204 15
Operating System Concepts
Many-to-one model

• Many user level threads map to a single kernel thread

• Pros:

• Fast. No system calls to manage threads.

• No mode change for switching threads

• Cons:

• No parallel execution of threads. All threads block when one


has a system call.

• Not suited for multi-processor systems.

CA 7204 16
Operating System Concepts
One-to-one mode

• Each user thread associated with one kernel thread.

• Pros.

• Better suited for multiprocessor environments.

• When one thread blocks, the other threads can


continue to execute.

• Cons.

• Expensive. Kernel is involved

CA 7204 17
Operating System Concepts
Many-to-Many model

• Many user threads mapped to many kernel threads

• Supported by some UNIX and windows versions

• Pros: flexible

• OS creates kernel threads as required

• Process creates user threads as needed

• Cons: Complex

• Double management

CA 7204 18
Operating System Concepts
Typical usage of threads

CA 7204 19
Operating System Concepts
Thread Libraries

• A thread library provides the programmer with an API for creating and managing threads.

• There are two primary ways of implementing a thread library.

• The first approach is to provide a library entirely in user space with no kernel support. All

code and data structures for the library exist in user space. This means that invoking a

function in the library results in a local function call in user space and not a system call.

• The second approach is to implement a kernel-level library supported directly by the

operating system. In this case, code and data structures for the library exist in kernel space.

Invoking a function in the API for the library typically results in a system call to the kernel.

• Three main thread libraries are in use today: POSIX Pthreads, Windows, and Java.

CA 7204 20
Operating System Concepts
Thread Libraries

• Pthreads, the threads extension of the POSIX standard, may be provided as either a user-
level or a kernel-level library.

• The Windows thread library is a kernel-level library available on Windows systems.

• The Java thread API allows threads to be created and managed directly in Java programs.

• Java threads are typically implemented using the Windows API

• UNIX and Linux systems often use Pthreads.

• For POSIX and Windows threading, any data declared globally —that is, declared outside
of any function—are shared among all threads belonging to the same process.

• Because Java has no notion of global data, access to shared data must be explicitly
arranged between threads.

• Data declared local to a function are typically stored on the stack. Since each thread has
its own stack, each thread has its own copy of local data.

CA 7204 21
Operating System Concepts
Thread Libraries

• Two general strategies for creating multiple threads

• Asynchronous threading and Synchronous threading

• With Asynchronous threading, once the parent creates a child thread, the
parent resumes its execution, so that the parent and child execute concurrently.
Each thread runs independently of every other thread, and the parent thread need
not know when its child terminates. Because the threads are independent, there is
typically little data sharing between threads.

• Synchronous threading occurs when the parent thread creates one or more
children and then must wait for all of its children to terminate before it resumes —
the so-called fork-join strategy. Here, the threads created by the parent perform
work concurrently, but the parent cannot continue until this work has been
completed.
CA 7204 22
Operating System Concepts
Thread Libraries

PThreads
• A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
• API specifies behavior of the thread library, implementation is up to development of the
library
• Common in UNIX operating systems (Solaris, Linux, Mac OS X)
Windows Threads
• Implements the one-to-one mapping
• Each thread contains
• A thread id
• Register set
• Separate user and kernel stacks
• Private data storage area
• The register set, stacks, and private storage area are known as the context of the
threads

CA 7204 23
Operating System Concepts
Thread Libraries

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)

Java Threads

• Java threads may be created by:

• Extending Thread class

• Implementing the Runnable interface

• Java threads are managed by the JVM.

CA 7204 24
Operating System Concepts
Threading issues

• There are a variety of issues to consider with multithreaded programming

• Semantics of fork() and exec() system calls

• Thread cancellation
• Asynchronous or deferred

• Signal handling
• Synchronous and asynchronous

• Thread pooling

• Thread-specific data
• Create facility needed for data private to thread

CA 7204 25
Operating System Concepts
Threading issues - Semantics of fork() and exec() system calls

• Recall that when fork() is called, a separate, duplicate process is created

• How should fork() behave in a multithreaded program?


• Should all threads be duplicated?

• Should only the thread that made the call to fork() be duplicated?

• In some systems, different versions of fork() exist depending on the


desired behavior

• The exec() system call continues to behave as expected


• Replaces the entire process that called it, including all threads

• If planning to call exec() after fork(), then there is no need to duplicate all
of the threads in the calling process

CA 7204 26
Operating System Concepts
Threading issues - Thread cancellation

• Thread cancellation is the act of terminating a thread before it has completed


• Example - clicking the stop button on your web browser will stop the thread that is
rendering the web page

• The thread to be cancelled is called the target thread

• Threads can be cancelled in a couple of ways

• Asynchronous cancellation terminates the target thread immediately


• Thread may be in the middle of writing data ... not so good

• Deferred cancellation allows the target thread to periodically check if it should be


cancelled
• Allows thread to terminate itself in an orderly fashion

CA 7204 27
Operating System Concepts
Threading issues - Signal handling

• Signals are used in UNIX systems to notify a process that a particular event
has occurred
• CTRL-C is an example of an asynchronous signal that might be sent to a process
• An asynchronous signal is one that is generated from outside the process that receives
it

• Divide by 0 is an example of a synchronous signal that might be sent to a process


• A synchronous signal is delivered to the same process that caused the signal to occur

• All signals follow the same basic pattern:


• A signal is generated by particular event

• The signal is delivered to a process

• The signal is handled by a signal handler (all signals are handled exactly once)

CA 7204 28
Operating System Concepts
Threading issues - Signal handling

• Signal handling is straightforward in a single-threaded process


• The one (and only) thread in the process receives and handles the signal

• In a multithreaded program, where should signals be delivered?


• Options:

• Deliver the signal to the thread to which the signal applies


• Deliver the signal to every thread in the process

• Deliver the signal to certain threads in the process

• Assign a specific thread to receive all signals for the process

CA 7204 29
Operating System Concepts
Threading issues - Signal handling

• Option 1 - Deliver the signal to the thread to which the signal applies

• Most likely option when handling synchronous signals (e.g. only the thread that
attempts to divide by zero needs to know of the error)

• Option 2 - Deliver the signal to every thread in the process

• Likely to be used in the event that the process is being terminated (e.g. a
CTRLC is sent to terminate the process, all threads need to receive this signal
and terminate)

CA 7204 30
Operating System Concepts
Threading issues - Thread pools

• In applications where threads are repeatedly being created/destroyed thread pools might
provide a performance benefit
• Example: A server that spawns a new thread each time a client connects to the system and discards
that thread when the client disconnects

• A thread pool is a group of threads that have been pre-created and are available to do work
as needed Create a number of threads in a pool where they await work.
• Threads may be created when the process starts

• A thread may be kept in a queue until it is needed

• After a thread finishes, it is placed back into a queue until it is needed again

• Avoids the extra time needed to spawn new threads when they’re needed

CA 7204 31
Operating System Concepts
Threading issues - Thread pools

Advantages of thread pools:

• Usually slightly faster to service a request with an existing thread than create a
new thread

• Allows the number of threads in the application(s) to be bound to the size of the
pool

• The only threads available are those in the thread pool

• If the thread pool is empty, then the process must wait for a thread to re-enter
the pool before it can assign work to a thread

• Without a bound on the number of threads in a process, it is possible for a


process to create so many threads that all of the system resources are
exhausted

CA 7204 32
Operating System Concepts
Thread pools

CA 7204 33
Operating System Concepts
Threading issues

Thread specific data

• Allows each thread to have its own copy of data

• Useful when you do not have control over the thread creation process (i.e., when
using a thread pool)

Scheduler activations

• Many: Many models require communication to maintain the appropriate number of


kernel threads allocated to the application

• Scheduler activations provide upcalls - a communication mechanism from the


kernel to the thread library

• This communication allows an application to maintain the correct number kernel


threads

CA 7204 34
Operating System Concepts

Das könnte Ihnen auch gefallen