> For the complete documentation index, see [llms.txt](https://davidjosearaujo.gitbook.io/notes-miect/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://davidjosearaujo.gitbook.io/notes-miect/sistemas-de-operacao/interprocess-communication/message-passing.md).

# Message-passing

## Introduction

Processes can communicate exchanging messages.

* A general communication mechanism, not requiring explicit shared memory, that includes both communication and synchronizatio&#x6E;**.**
* Valid for uniprocessor and multiprocessor systems.

Two operations are required:

* **send** and **receive**.

A communication link is required.

* That can be categorized in different ways:
  * Direct or indirect communication.
  * Synchronous or asynchronous communication.
  * Type of buffering.

## Direct and indirect communication

**Symmetric direct communication.**

A process that wants to communicate must explicitly name the receiver or sender.

* *send(P, msg)*– send message *msg* to process *P*
* *receive(P, msg)* – receive message *msg* from process *P*

A communication link in this scheme has the following properties:

* it is established automatically between a pair of communicating processes.
* it is associated with exactly two processes.
* between a pair of communicating processes there exist exactly one link.

**Asymetric direct communication.**

* Only the sender must explicitly name the receiver.
  * *send(P, msg)*– send message *msg* to process *P*
  * *receive(id, msg)* – receive message *msg* from process *P*

**Indirect communication**

The messages are sent to and received from mailboxes, or port&#x73;**.**

* *send(M, msg)*– send message *msg* to mailbox *M*
* *receive(M, msg)* – receive message *msg* from mailbox *M*

A communication link in this scheme has the following properties:

* it is only established if the pair of communicating processes has a shared mailbox.
* it may be associated with more than two processes.
* between a pair of processes there may exist more than one link (a mailbox per each).

The problem of two or more processes trying to receive a message from the same mailbox.

* Is it allowed?
* If allowed, which one will succeed?

## Synchronization

From a synchronization point of view, there are different design options for implementing send and receive.

* **Blocking send** – the sending process blocks until the message is received by the receiving process or by the mailbox.
* **Nonblocking send** – the sending process sends the message and resumes operation.
* **Blocking receive** – the receiver blocks until a message is available.
* **Nonblocking receive** – the receiver retrieves either a valid message or the indication that no one exits.

Different combinations of send and receive are possible.

## Buffering

There are different design options for implementing the link supporting the communication.

* **Zero capacity** – there is no queue.
  * the sender must block until the recipient receives the message.
* **Bounded capacity** – the queue has finite length.
  * if the queue is full, the sender must block until space is available.
* **Unbounded capacity** – the queue has (potentially) infinite length.

## Bounded-buffer problem

<figure><img src="https://933056030-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmDWmJ4LyRblScRPGY3di%2Fuploads%2FL49IiMP5ElAOCsOKjCOn%2Fa.png?alt=media&amp;token=6f3cd27c-96f3-4651-88f2-c11456c56a08" alt=""><figcaption></figcaption></figure>

There is no need to deal with mutual exclusion and synchronization explicitly.

* the send and receive primitives take care of it.
