Debian Codegraph @

yo/concrete/fork2/pipe/driver/driver.cc

raw
1 /*
2 driver.cc
3
4 */
5
6 #include "../pipe.h"
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <unistd.h>
10 #include <iostream>
11 #include <string>
12
13 using namespace std;
14 using namespace FBB;
15
16 int main(int argc, char **argv)
17 {
18 Pipe p; // construct a pipe
19
20 cout << "Read file descriptor: " << p.readFd() << '\n';
21 cout << "Write file descriptor: " << p.writeFd() << '\n';
22
23 int pid = fork();
24
25 if (pid == -1)
26 return 1;
27
28 if (!pid) //child
29 {
30 p.readVia(STDIN_FILENO); // read what goes into the pipe from cin
31
32 string s;
33
34 getline(cin, s);
35
36 cout << "Got: " << s << '\n';
37
38 getline(cin, s);
39
40 cout << "Got: " << s << '\n';
41
42 return 0;
43 }
44
45 int stdfd[] = {STDOUT_FILENO, STDERR_FILENO};
46
47 p.writeVia(stdfd); // write to the pipe via cout
48
49 cout << "first line" << '\n';
50 cerr << "second line" << '\n';
51
52 waitpid(pid, 0, 0);
53 }