-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProcess.cpp
46 lines (41 loc) · 1.07 KB
/
Process.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include "Process.hpp"
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 ((m_pid = fork()) < 0)
{
perror("Process fork");
throw std::string("Process fork");
} else if ( m_pid == 0 ) {
/* child process */
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;
}
};
Process::~Process()
{
if (verbose)
std::cerr << "Process " << m_name << ": Entering ~Process()" << std::endl;
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;
};