I have two processes given by their pids: P1 and P2. Is there are a simple way of chcecking whether these processes are communicating via sockets or other inter-process communication mechanism? I need to know this because I have two seemingly unrelated apps that might be communicating under the hood and I want to know if this is really the case.
You can use
lsof -p P1 and
lsof -p P2 to see the file descriptors open by the two processes. Then you can look at the list of sockets and pipes they each have open, and see if any of them have the same ID.
imac:barmar $ sleep 100 | sleep 100 &  51885 imac:barmar $ jobs -l + 51884 Running sleep 100 51885 | sleep 100 & imac:barmar $ lsof -p 51884 | grep -i pipe sleep 51884 barmar 1 PIPE 0x491a6929f9ea1ca9 16384 ->0x491a6929f9e9fae9 imac:barmar $ lsof -p 51885 | grep -i pipe sleep 51885 barmar 0 PIPE 0x491a6929f9e9fae9 16384 ->0x491a6929f9ea1ca9
Notice that the destination ID of the pipe in the first process is the same as the source ID of the pipe in the second process. That indicates that they're the two ends of the same pipe.