Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int fids[2][2];
- if (0 != pipe(fids[0])) {
- throw sys_error(errno, strerror(errno));
- }
- if (0 != pipe(fids[1])) {
- int myerrno = errno;
- close(fids[0][0]);
- close(fids[0][1]);
- throw sys_error(myerrno, strerror(myerrno));
- }
- pid_t pid = fork();
- switch(pid)
- {
- case 0:
- // Close read ends
- close(fids[0][0]);
- close(fids[1][0]);
- // Dup write ends to stdout
- if (-1 == dup2(fids[0][1], STDOUT_FILENO)) {
- _exit(1);
- }
- cerr << "about to test stdout" << endl; // this prints
- cout << "Testing stdout!" << endl; // this doesn't. EPIPE here
- cerr << "Testing stderr!" << endl; // obv doesn't
- exec(...);
- _exit(1);
- break;
- case -1:
- int myno = errno;
- // close everything
- close(fids[0][0]);
- close(fids[0][1]);
- close(fids[1][0]);
- close(fids[1][1]);
- // error
- throw sys_error(myno, strerror(myno));
- break;
- }
- // this should be parent only
- // close write ends
- close(fids[0][1]);
- close(fids[1][1]);
- wait(...)
Advertisement
Add Comment
Please, Sign In to add comment