Differences
- Creation of thread faster than creation of process
- Switching between threads faster than switching between processes
- Variables can be shared between threads of same process hence data sharing in threads is easy as compared to process. Processes don't share a common memory space hence schemes like Pipes and Queues need to be used for data communication
Applications
Threads
- Almost all softwares that we run on PC, like microsoft office, adobe pdf reader, eclipse etc. they create a single process and inside process they create multiple threads - GUI handling, back end processing, event handling etc.
- This is because processes as slower in all aspect as mentioned above and eventually application will be slow performing. Hence in these cases multi-thread creation is a better option as compared to multi-process creation
- There are two types of threads:
- Kernel level thread (Thread model - One to one)
- Advantages:
- Best suited when thread blocking comes into play
- One blocked thread does not block the whole process. Multithreading and multiprocessing hardware is actually used in this case
- Disadvantage:
- Slow as compared to User level threads
- Overhead of thread management
- Example: PThread
- User level thread (Thread model - Many to one)
- Adavantges:
- Best suited for non-blocking thread
- Fast execution as compared to kernel threads
- Fast thread creation, switching, synchronization etc.
- Kernel is not aware of threads created in user level
- Disadvantages:
- Mutlithreading and multiprocessing hardware is not used
- If one thread blocks the process is blocked
- Protecting shared resources
- Mutex(Mutual Exclusion) - Thread resource sharing serialization
- Probable problems -
- Deadlock - Thread1 acquire lockA, and demands lockB. Thread2 acquire lockB and demand lockA. This is the scenario where deadlock occurs.
- Race condition - Unsynchronizaed access to shared resource can cause inconsitency in the shared data
- Priority Inversion - When a LOW priority thread acquires a lockA, and is doing some operation. At teh same time if HIGH priority thread want to acquire the same lockA, then is halts at the point waiting to get lockA. Here, even though the demanding thread is of HIGH priority it has to wait until LOW priority thread release the lockA. Thus the problem of Priority Inversion. In the mean time, if a MEDIUM priority thread want to execute and that doesnt need lockA, then it takes over and LOW thread is halted, which is even a more twist in the story. To avoid this, Priority inheritance and Priority Ceiling are used.
- Thread synchronization primitives
- Join
- Condition Variable
- Barrier
- Spinlocks
- Semaphores
Processes
- Multiple process creation is a better option in scenarios where completely separate memory areas for task as required. Example: The chromium project. its a web browser, where separate processes are created for every tab you create. This is to protect the overall application from bugs and glitches in the rendering engine. This brings to web browsing the benefits that memory protection and access control brought to operating systems.
- If your application will consist of separate, individually-usable components that communicate through well-defined protocols, each of which performs jobs that can individually succeed or fail without complicating the logic of the other components, then it's perfectly reasonable to write an application that utilizes multiple processes.