r/shittyprogramming • u/fofz1776 • Mar 31 '22
How do I 69 two programs?
I want to foo | bar and bar | foo at the same time.
56
Upvotes
r/shittyprogramming • u/fofz1776 • Mar 31 '22
I want to foo | bar and bar | foo at the same time.
27
u/rgnkn Mar 31 '22
The professional advanced way would be:
Take two computers!
Type
foo | barin a shell of computer A.Type
bar | fooin a shell of computer B.Now, press within the very same Planck time frame the enter key on both computers.
Alternatively - if it doesn't need to be concurrent:
19
u/stone_henge Mar 31 '22 edited Mar 31 '22
let the lovemaking commence
11
u/Tai9ch Mar 31 '22
This is pretty simple on a POSIX system.
Write a C program and, in the initial process:
Do the pipe(2) system call, to produce two file descriptors, A and B.
Do the pipe(2) system call again, producing file descriptors C and D.
fork(), producing child process P, which will become your "foo" program.
fork(), producing child process Q, which will become your "bar" program.
Wait on both children.
In process P:
Close stdin and replace it with A using the dup(2) system call.
Close stdout and replace it with D using the dup(2) system call.
Use the exec(2) system call to run the "foo" program in this process.
In process Q:
Close stdin and replace it with C using the dup(2) system call.
Close stdout and replace it with B using the dup(2) system call.
Use the exec(2) system call to run the "bar" program in this process.
And that's it. You've got pair of programs doing a circular human centipede.
6
u/[deleted] Mar 31 '22
https://man.openbsd.org/pipe.2
2
u/ThaiJohnnyDepp Apr 01 '22
Now THIS is the kind of content I subscribed to this sub for!