So while I was learning pipes programming in C, I decided to comment out the opening pipe() code and see what happens, and the result certainly isn't what I expected.
Here's the code
#include <stdio.h>
#include <unistd.h>
#define MSGSIZE 16
char *msg1 = "Hello, World #1";
char *msg2 = "Hello, World #2";
char *msg3 = "Hello, World #3";
int main(void) {
char inbuf[MSGSIZE];
int p[2], j;
/* if (pipe(p) == -1) { */
/* perror("pipe call"); */
/* exit(0); */
/* } */
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);
for (j = 0; j < 3; j++) {
read(p[0], inbuf, MSGSIZE);
printf("%s\n", inbuf);
}
exit(0);
}
And here's the output of the program:
Hello, World #1Hello, World #2Hello, World #3^C
In the Emacs shell the output is even more peculiar
Hello, World #1^@Hello, World #2^@Hello, World #3^@
So I understand 0 and 1 are file descriptors for stdin and stdout respectively, but I just write() to the second element of an int array and read() from the first element, how did I end up with this output?
Thanks in advance to anyone who takes out his/her time to explain this!