-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.cpp
More file actions
142 lines (119 loc) · 2.87 KB
/
Process.cpp
File metadata and controls
142 lines (119 loc) · 2.87 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "Process.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
Process::Process(const std::vector<char*>& args, bool verbose) :
verbose(verbose),
m_name(args[0]),
m_pid((pid_t)NULL),
m_writepipe {-1,-1},
m_readpipe {-1,-1},
m_pwrite((FILE*)NULL),
m_pread((FILE*)NULL)
{
if(pipe(m_writepipe) < 0)
{
perror("pipe write");
throw std::string("pipe write");
}
if(pipe(m_readpipe) < 0)
{
perror("pipe read");
throw std::string("pipe read");
}
if ((m_pid = fork()) < 0)
{
perror("process fork");
throw std::string("process pork");
}
else if (m_pid == 0) //child process
{
if(close(PARENT_WRITE) < 0)
{
perror("child process close parent write");
throw std::string("child process close parent write");
}
if(close(PARENT_READ) < 0)
{
perror("child process close parent read");
throw std::string("child process close parent read");
}
if((dup2(CHILD_WRITE, 1)) < 0)
{
perror("child process dup write");
throw std::string("child process dup write");
}
if((dup2(CHILD_READ, 0)) < 0)
{
perror("child process dup read");
throw std::string("child process dup read");
}
if(close(CHILD_READ) < 0)
{
perror("child process close child read");
throw std::string("child process close child read");
}
if(close(CHILD_WRITE) < 0)
{
perror("child process close child write");
throw std::string("child process close child write");
}
execvp(args[0], const_cast<char**>(&args[0]));
perror("Process execvp");
throw std::string("Process execvp");
}
else //parent process
{
if (verbose)
std::cerr << "Process " << m_name << ": forked PID " << m_pid << std::endl;
if(close(CHILD_READ) < 0)
{
perror("parent process close child read");
throw std::string("parent process close child read");
}
if(close(CHILD_WRITE) < 0)
{
perror("parent process close child write");
throw std::string("parent process close child write");
}
m_pread = fdopen(PARENT_READ, "r");
m_pwrite = fdopen(PARENT_WRITE, "w");
}
}
Process::~Process()
{
if (verbose)
std::cerr << "Process " << m_name << ": Entering ~Process()" << std::endl;
fclose(m_pwrite);
fclose(m_pread);
kill(m_pid, SIGTERM);
int status;
pid_t pid = waitpid(m_pid, &status, 0);
if (pid < 0)
perror("~Process waitpid");
if (verbose)
std::cerr << "Process " << m_name << ": Leaving ~Process()" << std::endl;
}
void Process::write(const std::string& line)
{
if(fputs(line.c_str(), m_pwrite) < 0);
{
perror("process fputs fail");
throw std::string("process fputs fail");
}
if(fflush(m_pwrite) < 0)
{
perror("process fflush fail");
throw std::string("process fflush fail");
}
}
std::string Process::read()
{
std::string line;
char* strng = NULL;
size_t num_bytes;
getline(&strng, &num_bytes, m_pread);
line = strng;
return line;
}