Skip to content

Introduction to Concurrent Programming

In this lesson, we introduce some basic concepts related to concurrent programming.

Concurrent programming allows a program to manage multiple tasks within the same interval of time. Tasks can take turns or, if multiple processors are available, can proceed truly in parallel. This helps make better use of resources and keeps programs responsive while some operations are waiting, for example for data from the network.

It is used in many contexts:

  • Servers and web services: to handle multiple user requests without completing one before starting the next.
  • Graphical interfaces: to keep the interface responsive while performing long operations, such as loading a file.
  • Operating systems: to manage multiple processes and services, alternating CPU use and coordinating access to resources.
  • Network communications: to send and receive data while other tasks continue to execute.
  • Data processing: to divide work into tasks that can proceed concurrently when dependencies allow it.
  • Simulations and video games: to coordinate tasks such as physics updates, input handling, and rendering.
  • Program: a set of instructions that describe the operations to be performed.
  • Process: a running instance of a program, with its own state and resources.
  • Process: a running instance of a program, with its own state and resources.
  • Thread: a flow of execution within a process. Threads in the same process share memory and resources, but each has its own execution state.
  • Processor: a hardware component that executes instructions; a system may have multiple processors or cores.
  • Resource: something a process needs to do its work, such as memory, files, devices, or shared data.

Concurrency can occur at different levels:

  • multiple threads, also called lightweight processes, can run within the same process (for example, in C or Java).
  • Alternatively, multiple processes can run concurrently, each with its own memory space and resources; for example, in Python, processes can be started using the multiprocessing module. To communicate or coordinate, processes can use inter-process communication mechanisms.

Ad ogni modo, indipendentemente dall’implementazione, i concetti esposti in queste lezioni rimangono sempre validi

With a multiprocessor system (multiple processors or cores), two processes can make progress at the same instant: their executions overlap in time (overlapping). With a single processor, multitasking gives processes short turns on the CPU: executions alternate (interleaving), so only one process runs at a given instant, even if rapid switching creates the appearance of simultaneity.

Two processes make progress during the same interval of time on different processors.

gantt title Overlapping: two processes in the same interval dateFormat X axisFormat %s section Processor 1 Process A :a1, 0, 4 section Processor 2 Process B :b1, 0, 4

Diagram created with Mermaid

On a single processor, the two processes take turns: only one process is executed at a time in each interval.

gantt title Interleaving: alternation on a single processor dateFormat X axisFormat %s section Single processor Process A :a1, 0, 1 Process B :b1, 1, 2 Process A :a2, 2, 3 Process B :b2, 3, 4

Diagram created with Mermaid

In these examples, the cars represent processes, while the roads or intersection represent the resources that can be used.

Each car uses a separate road: the resources are not shared, and the processes can proceed independently.

Two cars on parallel roadsTwo cars travel to the right on separate, parallel roads.

Two cars arrive from perpendicular directions and require the same resource, the intersection. If one is crossing it, the other must wait until it is clear. Without rules, an accident could occur.

Two cars approach an intersection from perpendicular directionsOne car arrives from the north and one from the west; both move toward the same intersection.

If each car occupies a section of the intersection and waits for the section occupied by the next car, a circular wait is created. No car can proceed and release its resource: a deadlock occurs.

Simplified representation of cars and an intersectionFour colored cars approach an intersection formed by two roads from different directions.

The answers to the quizzes on this page are saved in local storage in your browser and remain stored between visits

What is a process?

  • A physical component that executes instructions.
  • An executing program with its own state.

    Correct: a process is an active instance of a program.

  • A resource shared by multiple programs.

What is the difference between overlapping and interleaving?

  • In overlapping, activities make progress in the same interval on distinct processors; in interleaving, they take turns on a single processor.

    Correct: overlapping is simultaneous, whereas interleaving alternates execution.

  • In overlapping, processes take turns; in interleaving, they make progress simultaneously.
  • They are two names for the sequential execution of a single process.

Which of these is an example of a resource that processes can share?

  • An instruction that has already completed.
  • A file or a shared section of memory.

    Correct: processes may need to coordinate access to files or memory.

  • The time elapsed since the computer started.

On a single-processor system, how many processes are executed at the same instant?

  • One: processes take turns using the CPU.

    Correct: alternation can create the appearance of simultaneity, but the CPU executes only one process at any instant.

  • Two, as long as they are interleaved.
  • Any number, regardless of the number of processors.

When does a deadlock occur between processes?

  • When a process finishes earlier than expected.
  • When processes wait for resources held by one another and cannot proceed.

    Correct: each process waits for a resource that another process must release.

  • When multiple processes use distinct processors at the same time.

Which situation describes interleaving?

  • Two processes make progress at the same instant on two distinct cores.
  • On a single processor, the CPU alternates the execution of two processes.

    Correct: in interleaving, processes take turns using a single processor.

  • Two processes use separate resources without interacting.

With multiple processors or cores, two processes can make progress at the same instant: their executions [[overlap]] in time (overlapping).

Quiz progress

0 / 0 questions answered (0%)

0 correct answers