|
6 | 6 |
|
7 | 7 | #include <stdio.h>
|
8 | 8 | #include <iostream>
|
| 9 | +#include <sys/sysinfo.h> |
| 10 | +#include <sys/types.h> |
| 11 | +#include <sys/wait.h> |
9 | 12 |
|
10 | 13 | #include "MBString.h"
|
| 14 | +#include "MBVector.h" |
11 | 15 |
|
12 | 16 | using namespace std;
|
13 | 17 |
|
| 18 | +#define PID_INVALID (-1) |
| 19 | + |
| 20 | +typedef struct MainData { |
| 21 | + MBVector<pid_t> children; |
| 22 | + int numJobs; |
| 23 | +} MainData; |
| 24 | + |
| 25 | +static MainData mainData; |
| 26 | + |
| 27 | +static void |
| 28 | +InsertChild(pid_t pid) |
| 29 | +{ |
| 30 | + for (int x = 0; x < mainData.children.size(); x++) { |
| 31 | + if (mainData.children[x] == PID_INVALID) { |
| 32 | + mainData.children[x] = pid; |
| 33 | + return; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + NOT_REACHED(); |
| 38 | +} |
| 39 | + |
| 40 | +static void |
| 41 | +WaitForChild(void) |
| 42 | +{ |
| 43 | + int status; |
| 44 | + int result; |
| 45 | + pid_t pid; |
| 46 | + |
| 47 | + for (int x = 0; x < mainData.children.size(); x++) { |
| 48 | + if (mainData.children[x] == PID_INVALID) { |
| 49 | + return; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + //No free slot |
| 54 | + for (int x = 0; x < mainData.children.size(); x++) { |
| 55 | + pid = mainData.children[x]; |
| 56 | + ASSERT(pid != PID_INVALID); |
| 57 | + |
| 58 | + result = waitpid(pid, &status, WNOHANG); |
| 59 | + if (result == -1) { |
| 60 | + Warning("%s: Error occured while waiting for child.\n"); |
| 61 | + } else if (WIFEXITED(status)) { |
| 62 | + mainData.children[x] = PID_INVALID; |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // All children are still running... |
| 68 | + Warning("%s: Waiting for any child...\n", __FUNCTION__); |
| 69 | + pid = wait(&status); |
| 70 | + if (pid != -1) { |
| 71 | + for (int x = 0; x < mainData.children.size(); x++) { |
| 72 | + if (mainData.children[x] == pid) { |
| 73 | + mainData.children[x] = PID_INVALID; |
| 74 | + return; |
| 75 | + } |
| 76 | + } |
| 77 | + ASSERT(FALSE); // We didn't find our child?? |
| 78 | + } |
| 79 | + |
| 80 | + Warning("%s", "Didn't find a free child... Trying again.\n"); |
| 81 | + WaitForChild(); |
| 82 | +} |
| 83 | + |
14 | 84 | int main()
|
15 | 85 | {
|
16 | 86 | MBString line;
|
| 87 | + pid_t pid; |
| 88 | + |
| 89 | + mainData.numJobs = get_nprocs(); |
| 90 | + mainData.children.resize(mainData.numJobs); |
| 91 | + |
| 92 | + for (int x = 0; x < mainData.numJobs; x++) { |
| 93 | + mainData.children[x] = PID_INVALID; |
| 94 | + } |
17 | 95 |
|
18 | 96 | while (!cin.eof()) {
|
19 | 97 | printf("> ");
|
20 | 98 | MBString_GetLine(cin, line);
|
21 |
| - if (!line.isEmpty()) { |
22 |
| - printf("Executing: %s\n", line.cstr()); |
23 |
| - system(line.cstr()); |
| 99 | + if (!line.isEmpty()) { |
| 100 | + WaitForChild(); |
| 101 | + |
| 102 | + pid = fork(); |
| 103 | + if (!pid) { |
| 104 | + // We are the child |
| 105 | + system(line.cstr()); |
| 106 | + exit(0); |
| 107 | + } else { |
| 108 | + // We are the parent |
| 109 | + Warning("%s: Launched [%d] %s\n", __FUNCTION__, pid, line.cstr());//banackm |
| 110 | + InsertChild(pid); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Wait for any remaining children. |
| 116 | + for (int x = 0; x < mainData.children.size(); x++) { |
| 117 | + pid = mainData.children[x]; |
| 118 | + if (pid != PID_INVALID) { |
| 119 | + printf("%s: Waiting for %d\n", __FUNCTION__, pid);//banackm |
| 120 | + waitpid(pid, NULL, 0); |
24 | 121 | }
|
25 | 122 | }
|
26 | 123 |
|
|
0 commit comments