Reputation: 1
I understand how IPC works and how when you make a pipe before fork() the child/children can inherit the pipe for communication. However, I cant wrap my head around how I would do this for two unrelated processes? (i.e processes that are not the parent or children of each other?).
Im asking because Im trying to get one of my .c files to communicate with another .c file via pipes, but I dont know how that works explicitly in xv6. Any help/ideas would be greatly appreciated!!
Upvotes: 0
Views: 1264
Reputation: 479
What pipe does is to connect the standard output of one program to the standard input of another program.
Consider the cat
and wc
program in xv6. cat
displays the content of a file or files. Let's say we have a text file test.txt
with Hello World
in it. cat
can display its content like this:
$ cat test.txt
Hello World
wc
calculates the number of lines, words, and characters in a file, files, OR stdin
. So it can be used either like this(read from a file):
$ wc test.txt
1 2 12 test.txt
or this(read from stdin
):
$ wc
Hello World
<Press Ctrl-D>
1 2 12
We can use pipe to connect these two commands:
$ cat test.txt | wc
1 2 12
So here cat
reads the file and prints its content into stdout
, but it doesn't get displayed onto the screen. Instead it gets feeded into the stdin
of wc
and wc
counts the lines, words, and characters in it. Notice that this communication is unidirectional. It only goes from left to right, no backwards.
You can refer to the source code of these two commands to write your program. Basically all you have to do is to make sure the previous command outputs into stdout
and the subsequent command reads from stdin
.
Upvotes: 0
Reputation: 29
Pipes are not able to communicate between 2 processes unless those two processes share a common ancestor, regardless of the Linux distro you use. If you are trying to allow 2 unrelated files to communicate, one of the exec() commands must be used in the child or parent process; this page may have what you're looking for.
Upvotes: 0
Reputation: 552
There are several ways to accomplish this, for something simple, try checking out popen(). One program can spawn an unrelated one and then can then read and write to each other.
https://man7.org/linux/man-pages/man3/popen.3.html
For other solutions you should look into FIFO files or even sockets as a means of IPC.
Upvotes: 0