Q1Operating System
Question
4 marks
Explain the differences between Process and Thread.
Answer
Processes and threads differ in resource allocation, overhead, communication mechanisms, and the level of isolation they provide.
A process is an independent program in execution with its own address space, while a thread is a unit of execution within a process that shares the process's resources. Key differences:
- Resource Sharing: Threads share the code, data, heap, and open files of their parent process. Processes have separate address spaces and do not share memory by default.
- Creation Overhead: Thread creation is much faster and requires fewer resources than process creation (fork()). Threads are called 'lightweight processes' for this reason.
- Communication: Inter-thread communication is easy since threads share memory. Inter-process communication (IPC) requires pipes, sockets, shared memory, or message queues — more complex and slower.
- Context Switching: Thread context switching is faster (only stack and registers switch); process context switching requires switching the entire memory map and other resources.
- Isolation/Protection: Processes are isolated — a crash in one process does not affect others. A crash in one thread can crash the entire process.
- Synchronization: Threads require careful synchronization (locks, semaphores) to avoid race conditions on shared data. Processes are naturally isolated.
- Use Cases: Use multiple threads when tasks share data and need fast communication. Use multiple processes for tasks needing strong isolation (e.g., web server processes separate requests).
Examples: A web browser uses threads for rendering (UI thread) and networking. A web server like Apache uses separate processes per request for isolation. Modern applications commonly use multi-threading (Java threads, pthreads in C).