5 ‐ Concurrency - CloudScope/DevOpsWithCloudScope GitHub Wiki

Threads

Threads in Linux are fundamental units of execution within a process. They share the same memory space and resources, which allows for efficient communication and resource sharing compared to separate processes.

A thread is a lightweight process that can run concurrently with other threads in the same process. Threads share the same memory space and file descriptors but have their own execution stack, registers, and program counter.

Threads provide a way to perform multiple tasks simultaneously within a single process, which can improve performance and responsiveness.

Difference between Threads and Process.

  1. Definition

    • Process: A process is an independent program in execution, which includes its own memory space, code, data, and system resources. Each process operates in its own isolated environment.

    • Thread: A thread, or a lightweight process, is a smaller unit of execution within a process. Threads within the same process share the same memory space and resources but have their own execution context (e.g., registers, stack).

  2. Memory and Resources

  • Process:

    • Memory: Each process has its own separate memory space, which includes code, data, and heap.

    • Resources: Processes do not share resources like file descriptors or open files with other processes unless explicitly set up (e.g., inter-process communication).

  • Thread:

    • Memory: Threads share the same memory space as other threads in the same process, including the process’s heap and data segment.

    • Resources: Threads share resources such as file descriptors, signals, and other process-wide resources, but each thread has its own stack and registers.

  1. Creation and Overhead
  • Process:

    • Creation: Creating a new process involves duplicating the parent process’s memory space (via fork in Unix/Linux) and can be relatively costly in terms of time and system resources.

    • Overhead: Processes have a higher overhead due to the need for separate memory space and the cost of context switching between processes.

  • Thread:

    • Creation: Creating a new thread within a process is relatively lightweight compared to creating a new process, as threads share the same memory space and resources.

    • Overhead: Threads have lower overhead for context switching compared to processes because they share the same memory space.

Concurrency

Concurrency is about dealing with many tasks at once, but not necessarily executing them simultaneously. It involves managing multiple tasks that may run concurrently but not in parallel. It allows multiple tasks to overlap in execution, giving the appearance of parallelism even on single-core processors. Concurrency is achieved through various techniques such as multitasking, multithreading, and multiprocessing.

  • Multitasking involves the execution of multiple tasks by rapidly switching between them. Each task gets a time slot, and the OS switches between them so quickly that it seems as if they are running simultaneously.

  • Multithreading takes advantage of modern processors with multiple cores. It allows different threads of a process to run on separate cores, enabling true parallelism within a single process.

  • Multiprocessing goes a step further by distributing multiple processes across multiple physical processors or cores, achieving parallel execution at a higher level.

Why Allow Concurrent Execution?

  • The need for concurrent execution arises from the desire to utilize computer resources efficiently.

  • Resource Utilization:

    • Concurrency ensures that the CPU, memory, and other resources are used optimally. Without concurrency, a CPU might remain idle while waiting for I/O operations to complete, leading to inefficient resource utilization.
  • Responsiveness:

    • Concurrent systems are more responsive. Users can interact with multiple applications simultaneously, and the OS can switch between them quickly, providing a smoother user experience.
  • Throughput:

    • Concurrency increases the overall throughput of the system. Multiple tasks can progress simultaneously, allowing more work to be done in a given time frame.
  • Real-Time Processing:

    • Certain applications, such as multimedia playback and gaming, require real-time processing. Concurrency ensures that these applications can run without interruptions, delivering a seamless experience.

Parallelism

  • Parallelism is about executing multiple tasks simultaneously, which is possible on multi-core processors.

Parallelism complements Concurrency.