Vous êtes sur la page 1sur 7

OPERATING SYSTEM

Threads
A Thread is a single sequence stream within in a process. Because
threads have some of the properties of processes, they are sometimes called
lightweight processes. In a process, threads allow multiple executions of
streams. In many respect, threads are popular way to improve application
through parallelism. The CPU switches rapidly back and forth among the
threads giving illusion that the threads are running in parallel. Like a
traditional process i.e., process with one thread, a thread can be in any of
several states (Running, Blocked, Ready or Terminated). Each thread has
its own stack. Since thread will generally call different procedures and thus
a different execution history. This is why thread needs its own stack. An
operating system that has thread facility, the basic unit of CPU utilization is
a thread. A thread has or consists of a program counter (PC), a register set,
and a stack space. Threads are not independent of one other like processes
as a result threads shares with other threads their code section, data
section, OS resources also known as task, such as open files and signals.

Single and Multithread processes


Single threaded means there is only one thread within the process
and it is doing all the work of the process.
Multithreading process have multiple threads within a single process,
each having their own program counter, stack, and set of registers but
sharing common code, data and certain structures such as open files

Benefits of Multithreading
Single application may perform several similar task: web server
Responsiveness
program may continue to run even if part of it is blocked.
Resource sharing
Share the memory/ resource of the process . Multiple
threads within the same address space.
Economy
Allocate memory / resource is costly . Solaris: 20 times
slower to create a process than a thread. Context switching
is 5 times slower.
Utilization of MP architectures

Thread Libraries
Provide an API for creating & managing threads

User space
No Kernel support
Invoking a function results in a local function calling user
space and not a system
Kernel Space
Library supported directly by the kernel
Code & data structure are present in the kernel
API
system call

User-Level Threads
User-level threads implement in user-level libraries, rather than via
systems calls, so thread switching does not need to call operating system
and to cause interrupt to the kernel. In fact, the kernel knows nothing about
user-level threads and manages them as if they were single-threaded
processes.
Advantages:
The most obvious advantage of this technique is that a user-level threads
package can be implemented on an Operating System that does not support
threads. Some other advantages are
User-level threads does not require modification to operating
systems.
Simple Representation:
Each thread is represented simply by a PC, registers, stack and a
small control block, all stored in the user process address space.
Simple Management:
This simply means that creating a thread, switching between
threads and synchronization between threads can all be done without
intervention of the kernel.
Fast and Efficient:
Thread switching is not much more expensive than a procedure
call.

Disadvantages:
There is a lack of coordination between threads and operating system
kernel. Therefore, process as whole gets one time slice irrespect of

whether process has one thread or 1000 threads within. It is up to


each thread to relinquish control to other threads.
User-level threads requires non-blocking systems call i.e., a
multithreaded kernel. Otherwise, entire process will blocked in the
kernel, even if there are runnable threads left in the processes. For
example, if one thread causes a page fault, the process blocks.

Kernel-Level Threads
In this method, the kernel knows about and manages the threads. No
runtime system is needed in this case. Instead of thread table in each
process, the kernel has a thread table that keeps track of all threads in the
system. In addition, the kernel also maintains the traditional process table
to keep track of processes. Operating Systems kernel provides system call to
create and manage threads.
Advantages:
Because kernel has full knowledge of all threads, Scheduler may
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 and inefficient. For instance,
threads operations are hundreds of times slower than that of userlevel threads.
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. As a result there is significant
overhead and increased in kernel complexity.

Multithreading Models
Ultimately there must exist a relationship between user threads and kernel
threads
Many to one
Many user level threads mapped to single kernel thread
Thread management done in user space efficient

The whole process will block if a thread makes a blocking system call
Only one thread can access the kernel at a time unable to run on
multiprocessors

One to one

Each user level threads maps to kernel threads


More concurrency
Parallel multiprocessors
Creating user thread == creating a kernel process
Many to many
Allows many user level threads to be mapped to many kernel threads
(Smaller or equal)
Allows the operating system to create a sufficient number of kernel
threads
Solaris prior to version 9
Windows NT/2000 with the Thread Fiber package

Threading Issues
Semantic of fork() and exec ()
Thread cancellation
Signal handling
Thread pools
Thread specific data
Scheduler activation

system calls

Semantic of fork () and exec ()


Semantic of fork() and exec() change in a multithread program
Does fork() duplicate only the calling thread or all threads?
Two version of fork
Duplicates all threads
Duplicate only the calling thread
Exec() will replace all the process including all threads

Thread Cancellation
Terminating a thread before it has finished
Data Base searching / stop button
Two general approaches:
Asynchronous cancellation terminates the target thread
immediately

Deferred cancellation allows the target thread to periodically


check if it should be cancelled
Problem when resources have been allocated to a thread

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
Signal is generated by particular event
Signal is delivered to a process
Signal is handled
Synchronous signal
Illegal memory access/ division by 0

Delivered to the same process that performed the operation


that cause signal
Asynchronous signal
Generated by an external event ( Control-C)
Every signal may be handled by
Default signal handler
User defined signal handler

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
Synchronous signal delivered to the thread
Asynchronous signal ? (not so clear)
Control-C should be delivered to all threads
Thread should specify which signal it will accept / which it will block
Signal needs to be handled only once to the first thread that do not
block it
Kill(aid_+aid, int signal) Pthread_kill(pthread_+ tid,int signal)

Thread Pools

Create number of threads in a pool where they await work


Web server
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 to be bound to


the size of the pool

Thread specific data

Threads belonging to a process share the data of the process


Allows each thread to have its own copy of data
Useful when you do not have control over the thread creation
process(when using a thread pool)

Scheduler activation
Communication issue between the kernel and the user space
Bothe 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

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 XP 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
The primary data structures of a thread include:
ETHREAD(executive thread block)
KTHREAD(kernel thread block)
TEB(thread environment block)

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 are managed by the JVM


Java threads may be created by:
Extending thread class
Implementing the runnable interface

Vous aimerez peut-être aussi