Multi-processing and Multithreading
Definition
Multi-processing is the execution of multiple processes simultaneously, where each process is an independent program instance with its own memory and resources.
Multithreading is the execution of multiple threads within a single process, where threads share the same memory and resources of that process.
In simple words:
Process
- = a separate running program
Thread
- = a smaller execution unit inside a process
Example:
- A text editor and a music player running separately are separate processes.
- In a web browser, one thread may handle user input while another thread loads a web page; these are threads within the same process.
Main Content
1. Multi-processing
Meaning and structure
- Multi-processing means dividing work among multiple processes.
- Each process has its own private memory, stack, and address space.
- Processes do not directly share memory unless special mechanisms are used, such as shared memory, pipes, sockets, or message passing.
- Example: A server may create separate processes to handle different client requests.
Characteristics and behavior
- Processes are strongly isolated from one another.
- If one process crashes, other processes usually continue running.
- Switching between processes is generally more expensive than switching between threads because the operating system must save and restore more state.
- Multi-processing is useful when tasks are independent and when reliability is important.
- Example: A data analysis tool may run multiple independent processes to process different files at the same time.
2. Multithreading
Meaning and structure
- Multithreading means a program creates several threads that run independently but within the same process.
- Threads share code, data, and heap memory, but each thread has its own program counter, registers, and stack.
- This shared memory makes communication between threads fast and efficient.
- Example: In a browser, one thread may render graphics while another thread downloads resources.
Characteristics and behavior
- Threads are lightweight compared to processes.
- Creating a thread usually takes less time and less memory than creating a process.
- Threads are useful for improving responsiveness, especially in interactive applications.
- However, because threads share memory, they can interfere with one another if synchronization is not used properly.
- Example: If two threads update the same bank account balance at the same time without coordination, the result may be incorrect.
3. Comparison Between Multi-processing and Multithreading
Memory usage
- Multi-processing uses separate memory spaces, so memory consumption is usually higher.
- Multithreading shares the same memory space, so it is more memory-efficient.
Communication and coordination
- Processes communicate more slowly because they need IPC (Inter-Process Communication) methods.
- Threads communicate faster because they can access shared data directly.
- However, shared memory creates risks like race conditions and deadlocks.
Fault isolation and safety
- A failure in one process usually does not affect others.
- A failure in one thread can crash the whole process because all threads belong to the same program.
- Therefore, multi-processing is often preferred for robust isolation, while multithreading is preferred for speed and lightweight task management.
Process A Process B
+------------------+ +------------------+
| Code, Data, Heap | | Code, Data, Heap |
| Thread 1 | | Thread 1 |
| Thread 2 | | Thread 2 |
+------------------+ +------------------+
Inside one process:
+------------------+
| Shared Memory |
| Thread A |
| Thread B |
| Thread C |
+------------------+
Working / Process
1. Task identification and division
- The program first determines which work can be done in parallel.
- Independent tasks are split into separate processes or threads depending on the design.
- Example: In a file-processing application, each file can be assigned to a separate process, or multiple parts of one file can be handled by different threads.
2. Creation and scheduling
- The operating system creates processes or threads and assigns them CPU time.
- The scheduler decides when each process or thread runs.
- On a multicore CPU, multiple processes or threads may run truly in parallel on different cores.
- On a single-core CPU, they may appear to run at the same time due to fast switching.
3. Execution, communication, and synchronization
- Processes may communicate using pipes, message queues, sockets, or shared memory.
- Threads communicate directly through shared variables, but they need synchronization tools like mutexes, semaphores, monitors, or locks.
- Synchronization prevents problems such as:
- Race condition: two threads updating the same data simultaneously
- Deadlock: two or more threads waiting forever for each other
- Starvation: a thread not getting access to resources for a long time
Advantages / Applications
Improved performance and speed
- Parallel execution can reduce total processing time.
- Large jobs can be divided into smaller tasks and completed faster.
- Example: Scientific simulations, video rendering, and image processing often use multiprocessing or multithreading.
Better responsiveness
- Applications remain responsive while background tasks run.
- Example: A word processor can keep accepting keyboard input while spell-checking runs in another thread.
Efficient resource utilization and scalability
- Modern systems can use multiple CPU cores effectively.
- Servers can handle many users at once.
- Example: Web servers often use multiple processes or threads to serve many client requests simultaneously.
- Examples of common applications:
- Operating systems
- Web browsers
- Database systems
- Game engines
- Real-time monitoring systems
- Network servers
- Data analytics tools
Summary
- Multi-processing uses separate processes with separate memory spaces.
- Multithreading uses multiple threads inside one process with shared memory.
- Multi-processing is safer and more isolated, while multithreading is lighter and faster for shared-task execution.
- Important terms to remember: process, thread, shared memory, IPC, synchronization, race condition, deadlock