I recently started learning about IPC and I have some issues. I wrote a program that creates two processes which communicate through pipe like this:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
int pfds[2];
char buf[30];
pipe(pfds);
if (!fork())
{
printf(" CHILD: writing to the pipe\n");
write(pfds[1], "test", 5);
printf(" CHILD: exiting\n");
exit(0);
}
else
{
printf("PARENT: reading from pipe\n");
read(pfds[0], buf, 5);
printf("PARENT: read \"%s\"\n", buf);
wait(NULL);
}
return 0;
}
Sorry for not handling potential errors, I wrote it like this for simplicity.
This works great but my question is: Is there any possibility to have two programs – server/client(two separate executables – not parent process/child process relationship) that communicate through pipe? Just like you can through FIFOs?
Thank you!
A regular pipe can only connect two related processes. It is created by a process and will vanish when the last process closes it.
To communicate between two separate processes you must use named pipes (FIFO).