Thread
Thread
benefits
There are four major categories of benefits to multi-threading:
Responsiveness - One thread may provide rapid response while other threads are blocked or
slowed down doing intensive calculations.
Resource sharing - By default threads share common code, data, and other resources, which
allows multiple tasks to be performed simultaneously in a single address space.
Economy - Creating and managing threads ( and context switches between them ) is much
faster than performing the same tasks for processes.
Scalability, i.e. Utilization of multiprocessor architectures - A single threaded process can
only run on one CPU, no matter how many may be available, whereas the execution of a
multi-threaded application may be split amongst available processors. ( Note that single
threaded processes can still benefit from multi-processor architectures when there are
multiple processes contending for the CPU, i.e. when the load average is above some certain
threshold. )
Types of Thread
In this case, the thread management kernel is not aware of the existence
of threads. The thread library contains code for creating and destroying
threads, for passing message and data between threads, for scheduling
thread execution and for saving and restoring thread contexts. The
application starts with a single thread
Advantages
Thread switching does not require Kernel mode privileges.
User level thread can run on any operating system.
Scheduling can be application specific in the user level thread.
User level threads are fast to create and manage.
Disadvantages
In a typical operating system, most system calls are blocking.
Multithreaded application cannot take advantage of multiprocessing.
The Kernel maintains context information for the process as a whole and
for individuals threads within the process. Scheduling by the Kernel is
done on a thread basis. The Kernel performs thread creation, scheduling
and management in Kernel space. Kernel threads are generally slower to
create and manage than the user threads.
Advantages
Kernel can simultaneously schedule multiple threads from the same
process on multiple processes.
If one thread in a process is blocked, the Kernel can schedule another
thread of the same process.
Kernel routines themselves can be multithreaded.
Disadvantages
Kernel threads are generally slower to create and manage than the user
threads.
Transfer of control from one thread to another within the same process
requires a mode switch to the Kernel.
Multithreading Models
Some operating system provide a combined user level thread and Kernel
level thread facility. Solaris is a good example of this combined approach.
In a combined system, multiple threads within the same application can
run in parallel on multiple processors and a blocking system call need not
block the entire process. Multithreading models are three types
Signal handling
Asynchronous or deferred
Thread-local storage
Scheduler Activations
Thread cancellation
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
Thread 1
Thread 2
Thread 1 returns: 0
Thread 2 returns: 0