[go: up one dir, main page]

0% found this document useful (0 votes)
78 views35 pages

Threads, Signals: Abhijit A M

The document discusses threads and signals. It defines threads as separate control flows within a program that execute concurrently. Threads allow dividing an application into multiple parts that can run simultaneously. Signals allow notifying a process of events. Signal handling involves specifying handlers for different signals. Threads and signals present challenges for programming multicore systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views35 pages

Threads, Signals: Abhijit A M

The document discusses threads and signals. It defines threads as separate control flows within a program that execute concurrently. Threads allow dividing an application into multiple parts that can run simultaneously. Signals allow notifying a process of events. Signal handling involves specifying handlers for different signals. Threads and signals present challenges for programming multicore systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Threads, Signals

Abhijit A M
abhijit.comp@coep.ac.in
Threads

thread — a fundamental unit of CPU utilization

A separate control flow within a program

set of instructions that execute “concurrently” with other parts of the code

Note the difference: Concurrency: progress at the same time, Parallel:
execution at the same time

Threads run within application

An application can be divided into multiple parts

Each part may be written to execute as a threads

Let’s see an example
Threads

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, due
to the very nature of threads

Can simplify code, increase efficiency

Kernels are generally multithreaded
Single vs Mulththreaded process
A mulththreaded server
Benefits of threads

Responsiveness

Resource Sharing

Economy

Scalability
Single vs Multicore systems

Single core : Concurrency possible

Multicore : parallel execution possible


Multicore programming

Multicore systems putting pressure on
programmers, challenges include:

Dividing activities

Balance

Data splitting

Data dependency

Testing and debugging

User vs Kernel Threads

User Threads: Thread 
Kernel Threads: Supported
management done by by the Kernel
user-level threads library 
Examples

Three primary thread 
Windows XP/2000
libraries: 
Solaris

POSIX Pthreads 
Linux

Win32 threads 
Tru64 UNIX

Java threads 
Mac OS X
User threads vs Kernel Threads

User threads 
Kernel Threads

User level library provides a 
Kernel implements concept
“typedef” called threads of threads

The scheduling of threads 
Still, there may be a user
needs to be implemented in the
level library, that maps kernel
user level library
concept of threads to “user

Need some type of timer concept” since applications
handling functionality at user link with user level libraries
level execution of CPU

OS needs to provide system calls

Kernel does scheduling!
for this

Kernel does not know that there
are threads!
Mulththreading models

How to map user threads to kernel threads?

Many-to-One

One-to-One

Many-to-Many

What if there are no kernel threads?

Then only “one” process. Hence many-one mapping possible,
to be done by user level thread library

Is One-One possible?
Many-One Model

Many user-level
threads mapped to
single kernel thread

Examples:

Solaris Green
Threads

GNU Portable
Threads
One-One Model


Each user-level thread maps to kernel thread

Examples

Windows NT/XP/2000

Linux

Solaris 9 and later
Many-Many Model

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

Allows the operating
system to create a
sufficient number of
kernel threads

Solaris prior to version 9

Windows NT/2000 with the
ThreadFiber package
Two Level Model

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

Examples

IRIX

HP-UX

Tru64 UNIX

Solaris 8 and earlier
Thread Libraries
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
pthreads

May be provided either as user-level or kernel-level

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)

Demo of pthreads code

Demonstration on Linux – see the code,


compile and execute it.
Other libraries

Windows threading API

CreateThread(...)

WaitForSingleObject(...)

CloseHandle(...)

Java Threads

The Threads class

The Runnable Interface
Issues with threads

Semantics of fork() and exec() system calls

Does fork() duplicate only the calling thread or all threads?

Thread cancellation of target thread

Terminating a thread before it has finished

Two general approaches:

Asynchronous cancellation terminates the target thread immediately.

Deferred cancellation allows the target thread to periodically check if
it should be cancelled.
Issues with threads

Signals are used in UNIX systems to notify a
process that a particular event has occurred.

Signal handling

Synchronous and asynchronous

A signal handler is used to process signals

Signal is generated by particular event

Signal is delivered to a process

Signal is handled
Issues with threads

More about signals

Different signals are typically identified as different numbers

Operating systems provide system calls like kill() and signal()
to enable processes to deliver and receive signals

Signal() - is used by a process to specify a “signal handler” –
a code that should run on receiving a signal

Kill() is used by a process to send another process a signal

There are restrictions on which process can send which
signal to other processes
Demo

Let’s see a demo of signals with respect to processes

Let’s see signal.h

/usr/include/signal.h

/usr/include/asm-generic/signal.h

/usr/include/linux/signal.h

/usr/include/sys/signal.h

/usr/include/x86_64-linux-gnu/asm/signal.h

/usr/include/x86_64-linux-gnu/sys/signal.h

man 7 signal

Important signals: SIGKILL, SIGUSR1, SIGSEGV, SIGALRM, SIGCLD, SIGINT,
SIGPIPE, ...
Signal handling by OS
Process 12323 { Process P1 {
kill (12323, 19) ;
signal(19, abcd);
}
} OS: sys_kill {
OS: sys_signal { Note down in PCB of process 12323 that
signal number 19 is pending for you.
Note down that process 12323 }
wants to handle signal number When process 12323 is scheduled, at that
19 with function abcd time the OS will check for pending signals,
and invoke the appropriate signal handler
} for a pending signal.
Issues with threads

Signal handling 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
Issues with threads

Thread pools

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

Advantages:

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
Issues with threads

Thread-specific data, Thread Local Storage (TLS)

Not local, but global kind of data for all functions of a thread, more like
“static” data

Create Facility needed for data private to thread

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)

gcc compiler provides the storage class keyword thread for declaring
TLS data
static __thread int threadID;
Issues with threads

Scheduler Activations

Both M:M and Two-level 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
Issues with threads

Scheduler Activations: LWP
approach

An intermediate data structure LWP

appears to be a virtual processor on
which the application can schedule a
user thread to run.

Each LWP attached to a kernel thread

Typically one LWP per blocking call,
e.g. 5 file I/Os in one process, then 5
LWPs needed
Issues with threads

Scheduler Activations: LWP
approach

Kernel needs to inform
application about events like: a
thread is about to block, or wait
is over

This will help application
relinquish the LWP or request a
new LWP
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)

struct task_struct points to process data
structures (shared or unique)
Linux threads

fork() and clone() system calls

Doesn’t distinguish between
process and thread

Uses term task rather than thread

clone() takes options to determine
sharing on process create

struct task_struct points to
process data structures (shared or
unique)
Issues in implementing threads
project

How to implement a 
Identifying the
user land library for support required from
threads? OS and hardware

How to handle 1-1,

Identifying the
many-one, many- libraries that will help
many in implementation
implementations?
Issues in implementing threads
project

Understand the clone() system call completely

Try out various possible ways of calling it

Passing different options

Passing a user-land buffer as stack

How to save and restore context?

C: setjmp, longjmp

Setcontext, getcontext(), makecontext(), swapcontext() functions

Sigaction is more powerful than signal

Learn SIGALRM handling for timer and scheduler, timer_create() & timer_stop()
system calls

Customized data structure to store threads, and manage thread-lists for
scheduling

You might also like