-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeline.cc
More file actions
67 lines (54 loc) · 1.47 KB
/
Pipeline.cc
File metadata and controls
67 lines (54 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/** @file Pipeline.cc
* Implementation of class Pipeline.
*/
#include <iostream>
#include <fcntl.h> // for: O_RDONLY, O_CREAT, O_WRONLY, O_APPEND
#include <signal.h> // for: signal, SIG*
#include "asserts.h"
#include "unix_error.h"
#include "Pipeline.h"
using namespace std;
Pipeline::Pipeline()
: background(false)
{
}
void Pipeline::addCommand(Command *cp)
{
require(cp != 0);
commands.push_back(cp);
}
Pipeline::~Pipeline()
{
for (vector<Command*>::iterator i = commands.begin() ; i != commands.end() ; ++i)
delete *i;
}
bool Pipeline::isEmpty() const
{
return commands.empty();
}
// Execute the commands in this pipeline in parallel
void Pipeline::execute()
{
//cerr << "Pipeline::execute\n";//DEBUG
// Because we want the shell to wait on the rightmost process only
// we must created the various child processes from the right to the left.
// Also see: pipe(2), fork(2), dup2(2), dup(2), close(2), open(2), signal(2).
// Maybe also usefull for debugging: getpid(2), getppid(2).
size_t j = commands.size(); // for count-down
// TODO
for (vector<Command*>::reverse_iterator i = commands.rbegin() ; i != commands.rend() ; ++i, --j)
{
Command *cp = *i;
if (j == commands.size()) {//DEBUG
cerr << "Pipeline::RIGHTMOST PROCESS\n";//DEBUG
}//DEBUG
cp->execute();
//TODO
if (j == 1) {//DEBUG
cerr << "Pipeline::LEFTMOST PROCESS\n";//DEBUG
} else {//DEBUG
cerr << "Pipeline::CONNECT TO PROCESS\n";//DEBUG
}//DEBUG
}
}
// vim:ai:aw:ts=4:sw=4: