last day (15 days later) » 

12:37 PM
@terdon here
 
hi
i updated my question
 
So post the full text here.
Ah...
Reading...
 
@terdon both the files need to interact with each other through the named pipe
 
mkfifo myfifo
$ ./A < myfifo | ./B > myfifo
$ # now i want to read myfifo
are A and B scripts?
or executables?
 
compiled C++ executables,
 
12:39 PM
Can you copy-paste the full text of lsof?
 
@Fabby how? i'll try redirecting it to a text file
 
Are you familiar with lsof?` (List open files)
 
$ lsof | wc -l
100338
it has these many lines
i'll probably upload to pastebin
 
and all GUI programs are closed?
Firefox all by iitself takes a bunch.
 
39564 without all GUI (Except terminal)
 
12:43 PM
@GaurangTandon That'll have to do.
oh, of you did not quit the terminal, what's the output of jobs as well?
 
@Fabby how do i get that?
 
Please note that I'm an amateur and Terdon is a pro. ;-) :-)
@GaurangTandon jobs[enter]
 
@Fabby no output
 
(should be nothing at all)
@GaurangTandon Good!
@GaurangTandon So where's the output of the lsof command?
 
doesn't upload... :/
too big
 
12:53 PM
@GaurangTandon post the output of lsof | grep pipe then.
@GaurangTandon Is this time-sensitive?
 
Reading...
 
Hang on, @Fabby why do you want the whole lsof and not just lsof myfifo?
 
because that didn't produce any output earlier...
 
Also, @GaurangTandon when you ran ./A < myfifo | ./B > myfifo didn't that hang?
Basically, the pipe there makes no sense, there's no output to pipe, you probably meant A < myfifo & (sending that to the background) and then B >myfifo.
 
1:02 PM
Also: did you try /A < myfifo | ./B > myfifo twice?
@GaurangTandon Ping me when you're ready to interact. Going to eat something now...
 
sorry i was interrupted for a while
am back
@terdon no it completed instantly
@terdon i want the output of A to go to B
so what other way there is if not pipe |?
@Fabby nope
 
@GaurangTandon then the fifo is closed. Had you already written something to myfifo before running that command?
 
Hmm
 
the cpmmand sequence i've given in my question is the exact sequence
 
1:11 PM
@GaurangTandon and lsof myfifo didn't produce any output earlier, right?
 
@Fabby yeah, just shows a red cross
 
AFK 5min.
@GaurangTandon Yup, that's just that the last command set a non-zero exit code on zsh.
 
@GaurangTandon can you show us the C code? Is it something simple or are those complex programs?
 
yeah sure @terdon
// A.cpp

#include <fstream>
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;
#define PIPE "pipe.txt"
#define MAX_BUF 150

int main()
{
    ofstream file("inter2.txt");
    cout << 1 << endl;
    file << 1;
    int in;
    cin >> in;
    cout << 2 * in;
    file << 2 * in;
    file.close();
    return 0;
}
 
@GaurangTandon No, hang on, I think I understand.
Why are you redirecting B's output to myfifo?
 
1:17 PM
// B.cpp

#include <fstream>
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;

int main()
{
    ofstream file("inter.txt");
    int in = -1;
    if (file.is_open())
    {
        cin >> in;
        cout << (in + 1);
    }
    else
        cout << -1;
    file << in;
    file.close();
    return 0;
}
@terdon so that A can receive it
 
@GaurangTandon Precisely. So then what is the pipe doing?
 
@terdon directing A's output to B
 
There's never any output passed to the pipe, you redirect the output to the fifo
 
what?
 
@GaurangTandon You run two commands there: A < myfifo and B > myfifo, right?
So the output of B is being sent to myfifo.
Therefore, it will never send any output to the pipe, it is already being redirected to myfifo.
What you want is:
./A < myfifo &
./B > myfifo
 
1:21 PM
I'm not sure if I get it. Acc to me, `./A < fifo2 | ./B > fifo2` does this:

1. A takes input from fifo2
2. B takes input from A's output (via`|`)
3. B gives output to fifo2 (which A receives)
4. B takes input from A's output (via |)
 
No.
 
which step is wrong here?
 
Well, yes, but what output does A have? Where is A's output going?
You are sending all of A's output to fifo2, so what are you expecting will be sent to the pipe?
 
A's output in the file is given to cout. I expect that to be passed via | to B's cin.
 
@GaurangTandon but you're redirecting it to the fifo...
 
1:24 PM
@GaurangTandon No, A's output is not sent to cout, it is sent to the fifo2 file
 
@terdon how? isn't it ./A < myfifo and not ./A > myfifo? (the latter should send it to myfifo imo, i'm doing the former)
or maybe i've understood the angle brackets wrong
 
no, no, you're right. I'm confused now. Hang on.
 
@GaurangTandon Do you want A to talk to B and B talk to A in a pipe bidirectionally?
 
Ah, hang on, where is B reading from stdin?
 
Let's take a step back: What are you trying to accomplish? @GaurangTandon
 
1:27 PM
@Fabby yes...
 
That seems to be reading from a file.
 
@terdon it's reading from cin. please see the line cin >> in;
 
Then what is ` ofstream file("inter.txt");`?
My C++ knowledge is very close to 0
 
I think he wants:
A | fifo | B
 
@terdon it's an output file stream. that's what the ofstream stands for. so B cannot read anything from inter.txt
 
1:30 PM
But the main issue here is how named pipes work. When you run A < myfifo, that means "start reading from myfifo and wait until something is written to it". Then, B >myfifo writes to it. So A reads it, and exits. Which is why the command returns immediately.
@Fabby You can't pipe to a file.
Here, @GaurangTandon, I'm testing with these two little programs. Are they a good approximation of what your C++ code is trying to do?
// A.c
#include <stdio.h>

int main() {
   char buff[255];
   int i;
   scanf("%s %d", buff, &i);
   printf("I read : %s\n",buff);

}

and
 
@Fabby ok i'll check it
 
@GaurangTandon Don't!
 
@terdon oh i didn't know that...
 
Sorry, this one. Not writing to the file directly.
 
Answer Terdon first!
(He's a giant, I'm a midget)
 
1:33 PM
@terdon looking into it
 
Yeah, a giant who can't write C. OK, @GaurangTandon, I mean this for B:
//B.c
#include <stdio.h>

main() {
   printf("This is a test\n");
}
So, A just reads from standard input and B just prints to standard output. Is that a good enough approximation of what your C++ is doing?
If I now run this:
./A < myfifo | ./B > myfifo
 
hmm.. ok i'll tell what is supposed to happen:

A has to read B's output.
A has to output to B, and so B reads A's output
 
So an infinite loop?
 
no
there is a set position after which no one outputs anything
in my current code
A outputs 1
B reads 1 and outputs 2
A reads 2 and outputs 4
then the programs end
simple communication
 
I think you need to post the source to your question too and we'll have to ping someone in Canada.
(Nathan)
@GaurangTandon ---^
 
1:39 PM
@Fabby source to my question as in?
 
Source code of your C++ programs.
Neither Terdon nor me know C++
So we have to ask someone else.
 
i posted the source codes above for both files
you want them in C?
 
It's a simple POC that Nathan (who's still asleep) will sort in 3 seconds flat)
@GaurangTandon No, I want you to edit your question and post your current source code in there and we'll ping someone later today.
 
ok cool
thanks for the help
 
That's what I meant with "Is this time sensitive?"
But it's just a test, right?
 
1:42 PM
@Fabby i thought you meant the number of lines in lsof is time consant
@Fabby as in?
 
@GaurangTandon You're just doing some testing...
There is not a factory full of people waiting for a response to your question is what I meant...
;-) 0:-)
 
In any case, the issue is how named pipes work, I think. @GaurangTandon does this do what you expect?
./A < myfifo > myfifo & ./B > myfifo
 
@Fabby oh ofc
@terdon checking
stuck
$ ./A < fifo3 > fifo3 & ./B > fifo3
[1] 19715
[1]  + 19715 done       ./A < fifo3 > fifo3
like so
 
Hang on, what exactly are you expecting to see?
 
expecting them to end
on their own
 
1:46 PM
@GaurangTandon They did.
 
i had to hit Ctrl-C and then i did cat fifo3 and still no output (terminal stuck)
 
@GaurangTandon Ah, that will never work. Fifos don't keep output. The output is already gone: it has been eaten by A which reads from the fifo.
 
@terdon oh well
why didn't you tell before? :( i was hoping to cat fifo all the time
 
But that doesn't mean it didn't work. You're just using the wrong method to see if it worked.
@GaurangTandon I hadn't realized what you were expecting. You never said :)
 
How to close a named pipe (thereby enabling it for cat)? was my question lol :P
nvm
i have already verified communication by checking the inter.txt file
 
1:48 PM
:D
 
1. run cat myfifo
@GaurangTandon Writing to a pipe enables it for cat. Try this:
2. in another terminal, run echo foo > myfifo
 
i was just hoping i could directly use the fifo's output instead of keeping a separate inter.txt file
@terdon trying
@terdon done it worked
 
@GaurangTandon You can, it is actually working as you want it to work, you just have no way of seeing it because all the output is going through the fifo.
If you add a line that prints something to another file, you will see that the output has been passed correctly .
 
@terdon hmm that's what inter.txt does alread
alright
can you post that as an answer, or may i self-answer?
 
Go ahead and self answer. I think the main issue is just a confusion about how named pipes work.
 
1:51 PM
@terdon You should answer so you get some rep out of this.
I'll upvote both Q&A
0:-)
 
I have more rep than I know what to do with :)
 
After I've eaten!!!
 
@terdon xD
ok
i'll self answer tomo
cya both
and thanks for all the help :D
 
Ciao!
 
you're welcome
 

last day (15 days later) »