static const char *kCS110StudentIDsFile = "studentsunets.txt";
int main(int argc, char *argv[]) {
unordered_set<string> cs110Students;
readStudentFile(cs110Students, argv[1] != NULL ? argv[1] : kCS110StudentIDsFile);
map<int, int> processCountMap;
compileCS110ProcessCountMap(cs110Students, processCountMap);
publishLeastLoadedMachineInfo(processCountMap);
return 0;
}
static const char *kCS110StudentIDsFile = "studentsunets.txt";
int main(int argc, char *argv[]) {
unordered_set<string> cs110Students;
readStudentFile(cs110Students, argv[1] != NULL ? argv[1] : kCS110StudentIDsFile);
map<int, int> processCountMap;
compileCS110ProcessCountMap(cs110Students, processCountMap);
publishLeastLoadedMachineInfo(processCountMap);
return 0;
}
static const int kMinMythMachine = 51;
static const int kMaxMythMachine = 66;
static void compileCS110ProcessCountMap(const unordered_set<string>& sunetIDs,
map<int, int>& processCountMap) {
for (int num = kMinMythMachine; num <= kMaxMythMachine; num++) {
int numProcesses = getNumProcesses(num, sunetIDs);
if (numProcesses >= 0) {
processCountMap[num] = numProcesses;
cout << "myth" << num << " has this many CS110-student processes: " << numProcesses << endl;
}
}
}
int getNumProcesses(int num, const unordered_set<std::string>& sunetIDs);
poohbear@myth55:$ date
Sat May 9 14:51:49 PDT 2020
poohbear@myth55:$ time ./myth-buster-sequential
myth51 has this many CS110-student processes: 177
myth52 has this many CS110-student processes: 88
myth53 has this many CS110-student processes: 116
myth54 has this many CS110-student processes: 153
myth55 has this many CS110-student processes: 64
myth56 has this many CS110-student processes: 99
myth57 has this many CS110-student processes: 145
myth58 has this many CS110-student processes: 114
myth59 has this many CS110-student processes: 89
myth60 has this many CS110-student processes: 78
myth61 has this many CS110-student processes: 106
myth62 has this many CS110-student processes: 119
myth63 has this many CS110-student processes: 122
myth64 has this many CS110-student processes: 102
myth65 has this many CS110-student processes: 107
myth66 has this many CS110-student processes: 117
Machine least loaded by CS110 students: myth55
Number of CS110 processes on least loaded machine: 64
real 0m6.933s
user 0m0.128s
sys 0m0.008s
poohbear@myth55:$
poohbear@myth55:$ date
Sat May 9 14:55:12 PDT 2020
poohbear@myth55:$ time ./myth-buster-sequential
myth51 has this many CS110-student processes: 177
myth52 has this many CS110-student processes: 88
myth53 has this many CS110-student processes: 117
myth54 has this many CS110-student processes: 156
myth55 has this many CS110-student processes: 65
myth56 has this many CS110-student processes: 98
myth57 has this many CS110-student processes: 145
myth58 has this many CS110-student processes: 114
myth59 has this many CS110-student processes: 89
myth60 has this many CS110-student processes: 78
myth61 has this many CS110-student processes: 106
myth62 has this many CS110-student processes: 119
myth63 has this many CS110-student processes: 126
myth64 has this many CS110-student processes: 102
myth65 has this many CS110-student processes: 107
myth66 has this many CS110-student processes: 117
Machine least loaded by CS110 students: myth55
Number of CS110 processes on least loaded machine: 65
real 0m7.127s
user 0m0.124s
sys 0m0.008s
poohbear@myth55:$
static void countCS110Processes(int num, const unordered_set<string>& sunetIDs,
map<int, int>& processCountMap, mutex& processCountMapLock,
semaphore& permits) {
permits.signal(on_thread_exit); // immediately schedule signal, ensures call no matter how we exit
int count = getNumProcesses(num, sunetIDs);
if (count >= 0) {
lock_guard<mutex> lg(processCountMapLock);
processCountMap[num] = count;
cout << "myth" << num << " has this many CS110-student processes: " << count << endl;
}
}
static void compileCS110ProcessCountMap(const unordered_set<string> sunetIDs,
map<int, int>& processCountMap) {
vector<thread> threads;
mutex processCountMapLock;
semaphore permits(8); // limit the number of threads to the number of CPUs
for (int num = kMinMythMachine; num <= kMaxMythMachine; num++) {
permits.wait();
threads.push_back(thread(countCS110Processes, num, ref(sunetIDs),
ref(processCountMap), ref(processCountMapLock), ref(permits)));
}
for (thread& t: threads) t.join();
}