Multi Processing & Multi Threading
A multi processing/multi tasking OS can have more than one process executing at any given time. This simultaneous execution may be possible either by
- Concurrent, meaning that multiple processes in a run state can be swapped in and out by the OS
- Parallel, meaning that multiple processes are actually running at the same time on multiple processors.
Just like the multitasking OS, a single process can have multiple threads that are executing in concurrent or parallel, which is called as the multi threading.
Thread
A thread is a single sequential flow of control within a program. Each process by default contains one thread. A thread is sometimes called as lightweight process, since it has its own thread id, stack, stack pointer, signal mask, program counter, registers etc.
All threads within a given process share resource handles, memory segments (heap and data segments) and code. The following diagram shows the process vs thread
- Threads have less overhead than process, for example threads share the heap, all data and code segments.
- Processes don't need to be swapped for creating a thread.
Advantages of Multi Threading
- Since the codes are running in concurrent or parallel, the speed of executing and the performance increases.
- Increases application responsiveness.
- Avoids inter process communications
POSIX Threads
The POSIX stands for Portable Operating System Interface. It is a family of standard specified for maintaining the compatibility between Operating System. It defines the application programming interface (API) along with command line shells and utility interfaces.
In the implementation of threads also different OS had it's own library and style, making the system incompatible. So POSIX defined the standards for the threads which are generally called as the POSIX Threads or the pthread.
Life cycle of thread
The thread life cycle consists of 4 stages. They are
- ready - When a thread is ready to run, i.e. waiting for a processor then it is said to be in ready state.
- running - When the thread is scheduled and is running/executing.
- blocked - When the thread is waiting for a shared resource.
- terminated - When the thread system resources are fully released and the thread becomes obsolete.
No comments:
Post a Comment