Operating System
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.
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
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
Threading Issues
Semantic of fork() and exec ()
Thread cancellation
Signal handling
Thread pools
Thread specific data
Scheduler activation
system calls
Thread Cancellation
Terminating a thread before it has finished
Data Base searching / stop button
Two general approaches:
Asynchronous cancellation terminates the target thread
immediately
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
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
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
Linux threads
Java threads