Sample Header Ad - 728x90

how to use unix domain socket with bash

0 votes
1 answer
184 views
First of all - I know fifos for IPC, but read about unix domain sockets, which are bidirectional and i wanted to test this, whats possible with command line and bash.
I asked Chat GPT, but the information i got was useless and the examples didnt work.
I wanted to achieve that two processes can write simulteanious to each other or read, without the concept of a fifo.
If i set a file descriptor for reading on a fifo. i can also write with two processes or more to the fifo. but the data get mixed, and the first reader reads all of the mixed content.
process A should receive what process B writes, and process B should receive what process A writes. I got this working, with the following test code: bash file: processA
while true; do
   for ((i=0;i "/proc/$PID/fd/6";
      sleep 1;
   done
   while read -t 0.01 dataB; do echo "Received from process B: $dataB"; done
done
bash File: processB
while true; do
   for ((i=0;i /dev/tty; done
done
terminal 1:
exec 6> >(nc -lU unixSocket | PID=$$ bash processA)
In Prozess A:
After this i can write to the unix socket over "/proc/$PID/fd/6",
read from the unix socket with stdin
and stdout goes terminal. terminal 2:
exec 5In Prozess B:
After this i can write to the unix socket with stdout,
read from the unix socket with "/proc/$PID/fd/5"
write to terminal with /dev/ttx
and stdin is on keyboard. First Question: Why are the file descriptors 5 and 6 not inherited from the subprocesses opened in the process substitution?
I have to get them over exporting the PID -why? Second Question: process A and B can communicate bidirectional, but i need to open two more main processes, which are executing the exec command for file descriptor redirection and starting the process substituion
exec 5But what i finally want is such a communication between the mainprocess and the childprocess, and not more.
Now i got: main 1 opens child 1, main 2 opens child 2, and child 1 can communicate bidirectional with child 2. Thanx for your help in advance
Asked by Schmaehgrunza (370 rep)
Mar 3, 2025, 01:12 AM
Last activity: Mar 5, 2025, 05:50 PM