Reputation: 18552
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?
Upvotes: 6
Views: 5463
Reputation: 69
Here is a python example of using unix socket for interprocess communication. using file.sock
#!/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()
#!/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);
Upvotes: 0
Reputation: 11890
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
.
Upvotes: 8