How can two parallel threads or processes interact?

In general, how do interprocess and cross-thread communications work?

Clarification: I understand that this issue cannot be exhaustively addressed in one brief paragraph. I am looking for answers that will be a good starting point for beginners - a high-level concept on how it works.

+4
source share
2 answers

Broadly speaking, there are two main mechanisms.

The simplest is shared memory. Both processes have access to some memory that can be used for reading or writing, so that the entries in them are visible in the readings of the other.

Another mechanism is the channels, which act as a pipe between two processes. In this case, one process puts some data into the channel, and another process pulls it. This mechanism destroys when it is consumed, data is lost from the pipes, so the process of getting it does something better with it.

Although the first case sounds simpler, in practice it is done with danger. If both processes try to write at the same time, who knows what will happen. To avoid this, the third type of IPC mechanism is used, locks, which are used to transfer a signal from one process to another when it normally does something for the general state.

From a theoretical point of view, they are all equivalent. Most operating systems provide all of these mechanisms.

But concurrent processes should not bind. in the Shared Nothing model, one main task prepares a series of work tasks. Work orders perform calculations without additional input. when all workers are completed, the main task can lead to a result. This is attractive because IPC comes with performance (synchronization) and uses nothing to do with synchronization.

+6
source

interprocesses: between processes - for a fee, System 5 IPC. in particular, it will cover pipes, semaphores, shared memory, sockets, signals. Typically, these services are provided by the OS kernel.

inter-thread: communication between threads of the same process. you must use stream synchronization primitives that the OS could provide, as well as be implemented in a user-space stream library.

+2
source

All Articles