Concurrency Flashcards
Process vs Thread
- Process: A process is a self-contained unit of execution that consists of its own memory space, program code, and system resources. Each process runs independently and does not share memory with other processes. Processes are typically isolated from each other.
- Thread: A thread is the smallest unit of execution within a process. Threads within the same process share the same memory space and resources. They can communicate and coordinate with each other more easily than processes.
- Isolation:
- Process: Processes are isolated from each other. If one process crashes, it typically does not affect other processes. Processes provide a higher degree of fault tolerance.
- Thread: Threads within the same process share memory and resources, so issues in one thread can potentially affect the entire process. However, threads are generally more lightweight than processes and have lower overhead.
- Communication and Synchronization:
- Process: Inter-process communication (IPC) mechanisms, such as message passing, pipes, and sockets, are required for processes to communicate with each other. Synchronization between processes is more complex and often requires synchronization primitives like semaphores or mutexes.
- Thread: Threads within the same process can easily communicate and share data since they have access to the same memory space. Synchronization between threads is simpler and can be achieved using synchronization primitives as well, but with less overhead than processes.
- Creation and Termination:
- Process: Creating and terminating processes is typically more resource-intensive and time-consuming compared to threads. Forking a new process involves duplicating the entire process, including its memory space.
- Thread: Creating and terminating threads is faster and requires fewer resources because threads within the same process share resources.
- Parallelism vs. Concurrency:
- Process: Processes are typically used for parallelism, where multiple processes perform tasks simultaneously on multi-core CPUs. They are suitable for CPU-bound tasks.
- Thread: Threads are used for concurrency, where multiple threads within a single process perform tasks concurrently. They are suitable for I/O-bound tasks and tasks that require frequent communication and synchronization.
- Portability:
- Process: Processes are generally more portable across different operating systems because they encapsulate their own resources.
- Thread: Thread implementations and behavior can vary between different operating systems, making them less portable.
In summary, processes and threads have distinct purposes and trade-offs. Processes provide better isolation and fault tolerance but come with higher overhead. Threads are more lightweight and are suitable for concurrent tasks within a process. The choice between processes and threads depends on the specific requirements of a given application and the trade-offs you are willing to make. Many modern applications use a combination of both processes and threads to leverage their respective advantages.
Concurrency vs. Parallelism
Concurrency is about managing multiple tasks simultaneously, while parallelism is about executing multiple tasks simultaneously.
Concurrency can be achieved even on single-core systems by interleaving execution of tasks, while parallelism requires multiple processing units (cores).
Concurrency Models
Shared Memory Concurrency: Threads share the same memory space and communicate through shared variables. This model is prone to race conditions and requires synchronization mechanisms like locks, mutexes, and condition variables.
Message Passing Concurrency: Processes or threads communicate by passing messages to each other. This model is often associated with actor-based concurrency, where each actor is an independent entity with its own state and communicates through message passing.
Deadlock and Livelock
Deadlock occurs when two or more tasks are waiting for each other to release resources, resulting in a deadlock state where none of the tasks can proceed.
Livelock occurs when two or more tasks keep responding to each other’s actions without making progress, effectively preventing any of them from completing their tasks.
Thread Safety
Thread safety refers to the ability of a program to execute multiple threads concurrently without causing data corruption or unexpected behavior.
Thread-safe code ensures that shared data structures are accessed and modified in a way that prevents race conditions and maintains consistency.