What is Inter Process Communication (IPC)?

Asked

Viewed 1,602 times

31

In a discussion with experienced programmers here on the site regarding competition control, the terms arose traffic lights, mutex, shared memory, monitor, and still others, which I’m beginning to understand now.

All my doubts started when I asked about asynchronous processes, where I realized that I was not understanding very well about the term asynchronous and I ended up learning about process fork.

In the middle of all this, I started researching many things about operating system processes. And that’s when I learned another term: CPI - which they say means inter process Communication.

But what is IPC in more detail?

How it relates to the terms I quoted above, which I said I was discussing?

  • 2

    Perhaps it’s the case that you consolidate a little more what you know before you ask the questions... Because I have the impression that the answers will have a lot of overlap. By the way, I think it fits the tag [tag:multithreading] in all these questions.

  • @bfavaretto thinks of a guy who this time really doesn’t know anything about it? It’s me. Most of the time I ask on the website, it’s because I already know the answer, but this time it was different. I apologize if I ended up formulating something in a bad way, but is that I saw nothing on the site talking directly about the subject.

  • @bfavaretto I was even wanting to ask what was System V, because I had never heard of it before.

  • 3

    It’s not that the questions are poorly formulated, it’s that you’re casting a lot of related questions. Maybe you could merge some of them. But I’m also not an expert in the field, I may not be able to. My tip is just to hold the rhythm a little ;)

  • @bfavaretto if that’s the case, we can close the questions. I don’t care about that. I usually really like to separate each term into a question. But maybe we can at least mark it as duplicate, to have more references.

  • @bfavaretto I’m really "riding". I’m running back and forth like crazy to know what’s behind that question I asked about msg_ in php. I don’t want to try to implement anything without knowing right if what I’m thinking about functions is right.

  • 2

    IPC would be the [Communication between processes] (https://pt.wikipedia.org/wiki/Comunica%C3%A7%C3%A3o_entre_processos)

  • 2

    Out of curiosity, what are you trying to implement? As I said, I don’t understand much of the subject, but I would think twice before dealing with multiple threads or processes with PHP. Doesn’t seem like the proper language for it.

  • @bfavaretto agree with you. PHP actually has several limitations when controlling processes (when using Apache primarily). The very idea was to understand how those work message queues php natives I showed in the other question, and how this relates to the terms : System V, IPC, Shared memory, semaphores and the like...

  • I found this link. I’ll check it out later.

  • How to use IPC wrong: https://answall.com/a/45330/64969

Show 6 more comments

2 answers

29


Multiprocessor operating systems are operating systems that are capable of executing more than one process at a time. Examples are UNIX systems, Windows version 95 onwards, and macos Classic (Macos X is also multiprocessed, but is included under the aforementioned UNIX rubric).

A very important feature of multiprocessed systems is that they implement so-called virtual address spaces, where each running process sees itself as the only running process in memory. This makes the development of programs much easier, because the programmer doesn’t have to worry about accidentally reading, overwriting or even executing data or code from another process unrelated to you, but that happens to be running at the same time.

So the OS is concerned with building this fiction that each process is the only one running on the machine, and the processes simply run as if they were the only ones in the world. This works great for simple programs, but eventually one process will need to communicate with another - for example, a graphical program will want to communicate with X/Wayland in a UNIX environment. How to do in this case?

This is where the mechanisms called Interprocess Communication (IPC) come in. Because processes cannot talk to each other by accessing each other’s memory, they need some kind of mechanism - and there are several - to talk to each other when necessary. And since it is the OS that implements this separation between processes, then it is the OS’s responsibility to implement these mechanisms as well.

The simplest mechanism of IPC is simply the use of files. This was used in the time of the old C compilers, where the Preprocessor would take the source code and write a file with this preprocessed code, and then the compiler would take this code and write a program Assembly corresponding, the assembler took the program Assembly and issued the object code, and finally the link-editor merged the object codes and generated the executable.

A refinement of this is the concept of pipe, which is a file that only exists while the programs while the programs on each side of the pipe run and is only visible to them; another larger one is UNIX domain sockets (UNIX Domain Sockets), used by Wayland to perform IPC; the use of TCP/IP sockets, COM, DCOM, CORBA, memory mapping, Win32 messages, are numerous others.

One of the mechanisms of interprocess communication is really to establish an area of memory that the two (or more) communicating processes can access at the same time, which is simple and efficient, but can cause problems if more than one process tries to write the same region at the same time. In this case, you need to establish some kind of protocol to define who can write where and when. This protocol is the semaphores, mutexes and allies.

In short, the concept of CPI is a concept that arises naturally from the concept of process isolation. The methods of realising this concept are numerous; each has its own characteristics, advantages and disadvantages. Which is most appropriate for your specific case depends on you.

22

CPI: inter process Communication. The easiest part of the question has already been answered, but it covers much more underneath.

I’m going to take the liberty here and tell you that process is all that occupies the processor to simplify the understanding, okay?

CPI levels vary according to the technology used and the technique for communicating. Let’s discuss a MIMD/processor computational system with process scheduling, from the level I consider most decoupled to what I consider more coupled.

Since we are talking about multiple processes talking to each other, these multiple processes do not necessarily need to be on the same machine. Let’s start with the case that they’re on the same machine.

Communication interfaces

Summary:
Plexity? Uniplex, half-duplex or full duplex
Type of data? Generalized
Assets/liabilities? Assets
Duration: Arbitrary/Lifetime

They are interfaces provided by the operating system itself. They can be Uniplex, half duplex and full duplex. The most typical example of an interface communication Uniplex is the pipeline, more briefly known as pipe, or even by | for the intimate. How this works?

Take a basic shell script example:

echo "furiosas ideias verdes dormem furiosamente" | sed -e 's/(^| )[aeiou][^ ]* /  /g' |
    sed 's/  */ /g' | tr -d aeiou

Here, each process works sufficiently separate from each other. There is no memory sharing, nor does one process know of each other’s existence. Underneath, the bash is printing the effect phrase, then feeding the first sed (which removes words beginning with vowels, leaving a space in place), which then feeds the second sed (which removes excess space) which only then launches into the tr delete vowels from the input. The output of this script is:

frss vrds drmm frsmnt

Step by step from example

echo "furiosas ideias verdes dormem furiosamente"

Entree:

Exit:

furiosas ideias verdes dormem furiosamente

sed 's/[aeiou][^ ]* / /g'

Entree:

furiosas ideias verdes dormem furiosamente

Exit:

furiosas verdes dormem furiosamente

sed 's/ */ /g'

Entree:

furiosas verdes dormem furiosamente

Exit:

furiosas verdes dormem furiosamente

tr -d aeiou

Entree:

furiosas verdes dormem furiosamente

Exit:

frss vrds drmm frsmnt

Other interprocess communications by interfaces

The pipe. That one pipe is also known as pipe anonymous, for he was not named. There is also the pipe named. On Unix system, you create a pipe appointed through command mkfifo, then you put a process to write on pipe and another to read from it, as if it were a traditional file system file.

Another model is also the socket. A program opens a socket and goes into listening mode, then another program connects and the two go on speaking through socket.

MPI

Summary:
Plexity? Full duplex
Type of data? Generalized
Assets/liabilities? Assets
Duration: Arbitrary/Lifetime

MPI is a kind of communication interface, but here there is a greater cohesion, there is a more intense coupling. It can also be said that this is multi computer, but it works very well with only one computer.

Imagine that you need to know how a molecule if structure given the pH and temperature of human blood (remnants of the time where he worked at CENAPAD-UFC). This molecule is too big, with almost independent parts of each other, so you can separately compute the behavior of its structures, arrive at a position of electro-stabilityand then update the information obtained by the other processing to refine its positioning.

This exchange of values is commonly done by sending messages in a processing type BSP.

BSP in fifteen seconds: large independent processing pieces that are synchronized and exchange information with each other from time to time.
Example of BSP in this answer, through the processes in the ordering networks.

Shared memory

Summary:
Plexity? Full duplex
Type of data? Generalized
Assets/liabilities? Liabilities
Duration: Arbitrary

Here two or more processes share part of the computer’s memory. They write in the common area, they read from the common area, but also have their private area. MPI can be mounted on top of that.

It is usually at this level that the programmer begins to worry about semaphores/mutex/monitors themselves. Before, he just knows that these things exist.

This sharing of data allows greater agility in execution, but it also requires more control from the programmer. Unlike communication by interface, this here does not allow launching an event for the other process to take action: here, communication is totally passive.

I don’t need or should talk to the other process: look, data! The other process just takes the data.

Shared variables

Summary:
Plexity? Full duplex
Type of data? Generalized
Assets/liabilities? Liabilities
Duration: Lifetime

Okay, some would argue that this is a kind of Shared Memory, others would argue that I’m making up that name. And, in fact, I can’t remember what the right name is, but that name comes in handy.

Here, not only is there a shared memory space between several processes, but in fact all memory is shared. Sharing is so large that even by the same names the variables meet. This is the IPC model in threads.

Signal sending

Summary:
Plexity? Uniplex
Data type? Signal/byte
Assets/liabilities? Assets
Duration: Moment

Who has never received a SIGSEGV when programmed in C?

SIGSEGV is also known for Segmentation fault, or segmentation failure. This is a unix signal sent by operating system saying that you accessed an area of memory out of expected (ended up violating the program memory segment).

It has other very characteristic signs that we use:

  • pause signal (CTRL+Z on terminal; SIGSTOP)
  • interrupt signal (CTRL+C on terminal; SIGINT)
  • sign of death (already given kill -9 <PID> in some process? SIGKILL)

Some signals can be intercepted, others not. SIGKILL and SIGSTOP, if I’m not mistaken, they’re not interceptable. I don’t remember if it is possible to send signals from any program to any program, but I am sure that any ancestral program can send a signal to any of its descendants. Wtrmute shows in an excellent way in this answer how to use messaging within the process tree, from ancestor to descendant and vice versa.

Has a specific question on signs

Via file

Summary:
Plexity? Half-duplex
Type of data? Generalized
Assets/liabilities? Liabilities
Duration: Arbitrary

Write to a file, wait for another process to read. Pig and straight to the subject. If the file is resident of RAM (RAM-FS), it will be faster.

It could also work well in case the OS takes too long to flush and allows "reading" of what is cached files by other processes, it might not be as bad of performance.

The advantage of this method is that communication information is persisted in the case of abrupt termination of the reading process.

Via database

Summary:
Plexity? Half-duplex
Type of data? Generalized
Assets/liabilities? Liabilities
Duration: Arbitrary

Similar to IPC per file, but DBMS can optimize for the table to stay in RAM while writing/reading more efficiently than reading in the file cache before flush.

Latch file

Summary:
Plexity? Uniplex
Type of dice? Boolean
Assets/liabilities? Liabilities
Duration: Arbitrary

I remembered that kind of communication when I wrote that answer.

In short: when you are in a multitasking environment and need to ensure the integrity of some file on the system, having no fixed server to control this situation, a latch file.

The following excerpt exemplifies how this strategy works in the environment git:


In case, as there is no server waiting to hear a message, a process git needs to decide for yourself by observing the environment (OS included as part of the environment) whether it can make critical changes or not. So he makes the following call to the operating system:

So, please, create for me the file .git/index.lock? Returns me success if it is only if I was the one who managed to create this file, or if it already exists return me failure

For which, there are two answers that the OS can provide:

It’s here, little one git, you created the file

Or, in case the file already exists (or just created by another process in parallel):

Hey, pet! This file already exists!

If the file previously exists (and therefore the error occurred), the git aborts gracefully with that message you’ve been received.

If the file was created (and therefore successful), the git is happy in its processing and, when you have finished the required activity, will remove the lock file.


Environment variable

Summary:
Plexity? Uniplex
Type of data? Text
Assets/liabilities? Liabilities
Duration: Creation of the child process

In shell-script, in several corners we need to do things like this:

export PATH=/bin:/usr/bin:$HOME/bin
./meu_script.sh

That’s one way to get on the show meu_scrpit.sh the variable $PATH. Note: if the export is omitted, the value of the changed variable will not be passed to the child process.

These variables are only passed from the parent process to the child process, and if I’m not mistaken the values are set at the time of doing the fork.

Code of return

Summary:
Plexity? Uniplex
Data type? Byte
Assets/liabilities? Liabilities
Duration: Termination of child process

When a child process ends, he can notify the father if everything was normal or if there was a failure during the execution. The default is to return 0 when there is no error (successful execution) and with a code other than 0 when there is error, and each value would correspond to a different error.

This allows a calling program to make execution flow decisions conditioned on the success or failure of the child program. The error code also allows you to inform the calling program what the problem was (or the person running the program in hand).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.