/
main.cpp
266 lines (235 loc) · 5.71 KB
/
main.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <algorithm>
#include <cctype>
#include <iostream>
#include <map>
#include <ostream>
#include <string>
#include <tuple>
#include <type_traits>
#include <vector>
struct process_info
{
pid_t id;
timeval start_timeval;
};
static_assert(std::is_pod<process_info>::value,
"Type 'process_info' need to be a POD type to send it via a pipe.");
struct bg_task
{
int task_id;
process_info watcher_proc;
process_info exec_proc;
};
static_assert(std::is_pod<bg_task>::value,
"Type 'bg_task' need to be a POD type to send it via a pipe.");
std::ostream& operator<<(std::ostream& out, const bg_task& t)
{
out << "[" << t.task_id << "] " << t.exec_proc.id;
return out;
}
int wait_cmd(process_info process)
{
int status;
if (-1 == waitpid(process.id, &status, 0))
{
std::cerr << "Error on wait(): " << strerror(errno) << "\n";
}
rusage ts;
timeval end;
timeval wallclock;
gettimeofday(&end, nullptr);
timersub(&end, &process.start_timeval, &wallclock);
if (-1 != getrusage(RUSAGE_CHILDREN, &ts))
{
char tmbuf[64];
timeval tv;
timeradd(&ts.ru_utime, &ts.ru_stime, &tv);
std::cout << '\n';
strftime(tmbuf, 64, "%s", localtime(&tv.tv_sec));
printf("CPU time: %s.%06ld seconds\n", tmbuf, tv.tv_usec);
strftime(tmbuf, sizeof tmbuf, "%s", localtime(&wallclock.tv_sec));
printf("Wall-clock time: %s.%06ld seconds\n", tmbuf, wallclock.tv_usec);
printf("Voluntary context switches: %ld\n", ts.ru_nvcsw);
printf("Involuntary context switches: %ld\n", ts.ru_nivcsw);
printf("Page faults serviced without I/O activity: %ld\n", ts.ru_minflt);
printf("Page faults that required I/O activity: %ld\n", ts.ru_majflt);
std::cout << '\n';
}
if (WIFEXITED(status))
{
return WEXITSTATUS(status);
}
return -1;
}
process_info launch_cmd(std::vector<std::string> args)
{
pid_t child_pid = fork();
timeval t;
gettimeofday(&t, nullptr);
switch (child_pid)
{
case -1: // Error
std::cerr << "Error on fork(): " << strerror(errno) << "\n";
return process_info{child_pid, t};
case 0: // Child process
{
const char *file = args[0].c_str();
char * * argv = new char *[args.size()+1];
std::vector<std::string>::size_type i;
for (i = 0; i < args.size(); ++i) {
argv[i] = const_cast<char*>(args[i].c_str());
}
argv[i] = nullptr;
if (-1 == execvp(file, argv))
{
std::cerr << "Error on exec(): " << strerror(errno) << "\n";
for (auto v : args)
{
std::cout << "'" << v << "'\n";
}
std::exit(EXIT_FAILURE);
}
return process_info{-1, t};
} break;
default: // Parent process
{
return process_info{child_pid, t};
}
}
}
bg_task launch_background_cmd(int task_id, std::vector<std::string> args)
{
timeval t;
gettimeofday(&t, nullptr);
int pipefd[2];
if (0 == pipe(pipefd))
{
pid_t watcher_pid = fork();
switch (watcher_pid)
{
case -1: // Error
{
std::cerr << "Error on fork(): " << strerror(errno) << "\n";
return bg_task{-2, process_info{-1, t}, process_info{}};
} break;
case 0: // Child process
{
close(pipefd[0]); // Close read end
auto info = launch_cmd(args);
write(pipefd[1], &info, sizeof(process_info));
wait_cmd(info);
std::exit(EXIT_SUCCESS);
} break;
default: // Parent process
{
close(pipefd[1]); // Close write end
process_info info;
read(pipefd[0], &info, sizeof(process_info));
return bg_task{task_id, process_info{watcher_pid, t}, info};
} break;
}
}
else
{
std::cerr << "Error on pipe(): " << strerror(errno) << "\n";
return bg_task{-1, process_info{-1, t}, process_info{}};
}
}
void cd(std::string dir)
{
if (chdir(dir.c_str()) == -1)
{
std::cout << strerror(errno) << "\n";
}
}
/* Check if processes need to be removed from bg_tasks */
void check_processes(std::map<int, bg_task> & map)
{
for (auto pair : map)
{
int status;
int ret = waitpid(pair.second.watcher_proc.id, &status, WNOHANG);
if (ret == -1 || (WEXITSTATUS(status) == 0 && ret != 0))
{
map.erase(pair.second.exec_proc.id);
}
}
}
int main(int /* argc */, char* /* argv */[])
{
auto bg_tasks = std::map<int, bg_task>();
timeval start;
gettimeofday(&start, nullptr);
int next_task_id = 0;
bool quit = false;
while (not quit)
{
std::string line;
std::cout << get_current_dir_name() << "> ";
std::cout.flush();
if (not std::getline(std::cin, line) || line == "exit")
{
std::cout << std::endl;
quit = true;
}
else
{
auto is_whitespace = [](char c){
return std::isspace(c) != 0;
};
auto begin = std::find_if_not(line.begin(), line.end(), is_whitespace);
auto end = line.end();
auto result = begin;
std::vector<std::string> args;
while (begin != end)
{
result = std::find(begin, end, ' ');
args.emplace_back(begin, result);
begin = std::find_if_not(result, end, is_whitespace);
}
if (not args.empty())
{
if (args.back() == "&")
{
args.pop_back();
auto task = launch_background_cmd(next_task_id++, args);
std::cout << task << std::endl;
bg_tasks[task.exec_proc.id] = task;
}
else if (args[0] == "cd")
{
cd(args[1]);
}
else if (args[0] == "aptaches")
{
check_processes(bg_tasks);
// for (std::map<int, bg_task>::const_iterator it=bg_tasks.begin(); it!=bg_tasks.end(); ++it) {
for (auto pair : bg_tasks)
{
std::cout << pair.second << std::endl;
}
}
else
{
wait_cmd(launch_cmd(args));
}
}
}
}
/* Kill everything in bg_tasks */
check_processes(bg_tasks);
for (auto pair : bg_tasks)
{
kill(pair.second.exec_proc.id, SIGTERM);
}
}