Interprocess Communication via file
Asked Answered
V

2

6

When I echo into files at some arbitrary locations in Linux, i.e. echo > /tmp/file, some running processes respond. Is this IPC via file pipe?

Does this mean a running process always open the file to be read? But so, how can the file be written, since the file stream is locked by by its own process?

Vitric answered 16/6, 2012 at 9:54 Comment(2)
some running processes respond — which ones, and how? running process always open the file — which file? file stream is locked — who says so?Murcia
I mean running processes in general. And if I open a file, it is possible to be locked. For example, an opened file is not possible for its content to be changed, since some other processes are using it. Normal computer users get this message all the time if they attempt to delete the file or modify it.Vitric
C
8

If you want use a file to communicate with another process, you should have a look at man fifo.

I'll report here just the first lines:

NAME
       fifo - first-in first-out special file, named pipe

DESCRIPTION
       A FIFO special file (a named pipe) is similar to a pipe, except that it
       is accessed as part of the file system.  It can be opened  by  multiple
       processes  for  reading or writing.  When processes are exchanging data
       via the FIFO, the kernel passes all data internally without writing  it
       to the file system.  Thus, the FIFO special file has no contents on the
       file system; the file system entry merely serves as a  reference  point
       so that processes can access the pipe using a name in the file system.

I think this is what you need.

Just think to it as a buffer. It must be opened both for reading and for writing by different process. The process who's reading will be blocked until the writing process doesn't write on it. When the writing process finish to write, close the file and that is the green light for the reading process to start empty the buffer. It's a FIFO, so the first line written will be the first line read. Then the writing process can open it again and they start again.

You can create a FIFO with mkfifo. Have a look to man mkfifo.

Coahuila answered 16/6, 2012 at 10:1 Comment(4)
which means, the file is not really a file, but it is just a pipe (owned by some processes) visible in the file system? On lower level, does this mean the file is just a reference to a block of memory, and when this block of memory has some data arrives, it will be read by the process owning this block of memory. The named pipe is just merely for human interaction, isn't it?Vitric
Added a short explanation. But I didn't get the part about the human interaction... aren't we speaking of software??? :PCoahuila
Well, we got named pipe, and we can manually write to the file i.e. type echo foo > /tmp/file, so I consider it include human interaction.Vitric
Of course you can. Once you have it, you can write there in any way you can imagine.Coahuila
C
0

Here is a python example of using unix socket for interprocess communication. using file.sock

SERVER Code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import sys
import os

server_address = './process.sock'

# Make sure the socket does not already exist
try:
    os.unlink(server_address)
except OSError:
    if os.path.exists(server_address):
        raise

# Create a UDS socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

# Bind the socket to the port
print(sys.stderr, 'starting up on %s' % server_address)
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print('waiting for a connection')
    connection, client_address = sock.accept()
    try:
        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(4096);
            print('received :');
            print(data);
            if data:
                print('sending data back to the client')
                connection.sendall(data.upper())
            else:
                print('no more data from', client_address)
                break

    finally:
        connection.close()

CLIENT Code

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import json

server_address = './process.sock'


# Create a UDS socket client
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.connect(server_address);


data = ["i","d"];

client.send(bytes(json.dumps(data), 'utf-8'));
from_server = client.recv(4096)
client.close()

print(from_server);
Carbohydrate answered 1/7, 2022 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.