Process vs Thread
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.