Created
May 10, 2022 08:46
-
-
Save gabonator/98c14907fe50607b672c0151bb729796 to your computer and use it in GitHub Desktop.
Pipe demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcc -x c++ -o test_read - << EOF | |
#include <stdio.h> | |
#include <unistd.h> | |
int main() | |
{ | |
fprintf(stdout, "[[Read begin]]\n"); | |
for (int j=0; j<20; j++) | |
{ | |
fprintf(stdout, "[[Read Block %d]]\n", j); | |
usleep(500*1000); | |
// Read 1 MB of data, show first 20 characters | |
for (int r=0; r<1024; r++) | |
{ | |
for (int i=0; i<1024; i++) | |
{ | |
int c = fgetc(stdin); | |
if (r==0 && i<20) printf("%c", c); | |
} | |
} | |
} | |
fprintf(stdout, "[[Read Done]]\n"); | |
return 0; | |
} | |
EOF | |
gcc -x c++ -o test_write - << EOF | |
#include <stdio.h> | |
int main() | |
{ | |
fprintf(stderr, "Write begin\n"); | |
for (int b=0; b<200; b++) | |
{ | |
fprintf(stderr, "Write Block %d, ", b); | |
// Write 1MB of data | |
for (int i=0; i<1024; i++) | |
{ | |
printf("%08d-%08d:", b, i); | |
for (int k=0; k<1024-20; k++) | |
printf(" "); | |
printf("\r\n"); | |
} | |
} | |
fprintf(stderr, "Write done\n"); | |
return 0; | |
} | |
EOF | |
./test_write | ./test_read | |
exit 0 | |
Expected output ("Write done" never reached): | |
Write begin | |
Write Block 0, [[Read begin]] | |
[[Read Block 0]] | |
Write Block 1, 00000000-00000000: [[Read Block 1]] | |
Write Block 2, 00000001-00000000: [[Read Block 2]] | |
Write Block 3, 00000002-00000000: [[Read Block 3]] | |
Write Block 4, 00000003-00000000: [[Read Block 4]] | |
Write Block 5, 00000004-00000000: [[Read Block 5]] | |
Write Block 6, 00000005-00000000: [[Read Block 6]] | |
Write Block 7, 00000006-00000000: [[Read Block 7]] | |
Write Block 8, 00000007-00000000: [[Read Block 8]] | |
Write Block 9, 00000008-00000000: [[Read Block 9]] | |
Write Block 10, 00000009-00000000: [[Read Block 10]] | |
Write Block 11, 00000010-00000000: [[Read Block 11]] | |
Write Block 12, 00000011-00000000: [[Read Block 12]] | |
Write Block 13, 00000012-00000000: [[Read Block 13]] | |
Write Block 14, 00000013-00000000: [[Read Block 14]] | |
Write Block 15, 00000014-00000000: [[Read Block 15]] | |
Write Block 16, 00000015-00000000: [[Read Block 16]] | |
Write Block 17, 00000016-00000000: [[Read Block 17]] | |
Write Block 18, 00000017-00000000: [[Read Block 18]] | |
Write Block 19, 00000018-00000000: [[Read Block 19]] | |
Write Block 20, 00000019-00000000: [[Read Done]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment