Suppose there are two programs. One asks the user to enter a name in the character array, and the second prints the name out of its character array. How to transfer the contents of one array to the second without writing and reading a file?

  • 2
    The keyword, as already noted, is IPC. And further on that fantasy is enough: socket, file descriptor, DBus, ... - 0andriy
  • Thank! I will google - biggy

2 answers 2

There are three standard methods:

  1. Message Queues (there are two kinds of queues - POSIX and System V)
  2. Channels (named and unnamed)
  3. Shared memory

Your task is a classic example of the "Manufacturer -> Consumer" task. In order not to bother with ring buffers and other things, I recommend using the named pipe.

man 3 mkfifof mkfifo - make FIFOs (named pipes) Create named pipes (FIFOs) with the given NAMEs. 

The simplest example:

  1. At the command prompt, execute the mkfifo my_pipe command
  2. We write and run the program of the consumer, which opens my_pipe as a regular file and hangs pending reading.
  3. We write and run the provider program, which opens my_pipe on the record and writes the data there.
  4. Channel data exchange begins.

Here the order (!) Of launching these programs is essential. If you run the manufacturer first, the program will crash due to an attempt to write to the channel from which no one reads.

  • Can be an example of the simplest hell - biggy
  • For a named pipe, everything is very simple - I added an example in response. - Sergey
  • And how to make data transfer from program to program, with the receiving program waiting for keyboard input? Without my_pipe! Transmitting for example generates names. - biggy
  • I do not understand: where does the receiving program wait for keyboard input - what is it like? I assumed that the receiving program was waiting for input from the transmitter . Is not it ? - Sergey

In general, for such things they do one of the programs to which they are connected, some kind of API, SAPI - in order for another program to be able to get what it needs.