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
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
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.
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.