Autumn 2021
Jerry Cain
PDF
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@myth63:$ date
Tue 26 Oct 2021 07:48:23 PM PDT
poohbear@myth63:$ time ./myth-buster-sequential
myth51 has this many CS110-student processes: 84
myth52 has this many CS110-student processes: 365
myth53 has this many CS110-student processes: 85
myth54 has this many CS110-student processes: 134
myth55 has this many CS110-student processes: 94
myth56 has this many CS110-student processes: 134
myth57 has this many CS110-student processes: 105
myth58 has this many CS110-student processes: 115
myth59 has this many CS110-student processes: 194
myth60 has this many CS110-student processes: 60
myth61 has this many CS110-student processes: 202
myth62 has this many CS110-student processes: 73
myth63 has this many CS110-student processes: 89
myth64 has this many CS110-student processes: 87
myth65 has this many CS110-student processes: 84
myth66 has this many CS110-student processes: 66
Machine least loaded by CS110 students: myth60
Number of CS110 processes on least loaded machine: 60
real 0m4.509s
user 0m0.357s
sys 0m0.142s
poohbear@myth63:$
poohbear@myth63:$ date
Tue 26 Oct 2021 09:50:27 PM PDT
poohbear@myth63:$ time ./myth-buster-sequential
myth51 has this many CS110-student processes: 83
myth52 has this many CS110-student processes: 366
myth53 has this many CS110-student processes: 82
myth54 has this many CS110-student processes: 135
myth55 has this many CS110-student processes: 93
myth56 has this many CS110-student processes: 134
myth57 has this many CS110-student processes: 109
myth58 has this many CS110-student processes: 118
myth59 has this many CS110-student processes: 197
myth60 has this many CS110-student processes: 60
myth61 has this many CS110-student processes: 209
myth62 has this many CS110-student processes: 73
myth63 has this many CS110-student processes: 89
myth64 has this many CS110-student processes: 88
myth65 has this many CS110-student processes: 84
myth66 has this many CS110-student processes: 66
Machine least loaded by CS110 students: myth60
Number of CS110 processes on least loaded machine: 60
real 0m4.294s
user 0m0.322s
sys 0m0.207s
poohbear@myth63:$
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();
}
poohbear@myth63:$ date
Tue 26 Oct 2021 10:02:51 PM PDT
poohbear@myth63:$ time ./myth-buster-concurrent
myth55 has this many CS110-student processes: 86
myth57 has this many CS110-student processes: 112
myth51 has this many CS110-student processes: 81
myth53 has this many CS110-student processes: 90
myth54 has this many CS110-student processes: 161
myth58 has this many CS110-student processes: 118
myth52 has this many CS110-student processes: 358
myth56 has this many CS110-student processes: 134
myth63 has this many CS110-student processes: 110
myth59 has this many CS110-student processes: 191
myth61 has this many CS110-student processes: 203
myth60 has this many CS110-student processes: 60
myth64 has this many CS110-student processes: 85
myth62 has this many CS110-student processes: 73
myth66 has this many CS110-student processes: 70
myth65 has this many CS110-student processes: 86
Machine least loaded by CS110 students: myth60
Number of CS110 processes on least loaded machine: 60
real 0m0.567s
user 0m0.168s
sys 0m0.093s
poohbear@myth63:$
poohbear@myth63:$ date
Tue 26 Oct 2021 10:03:55 PM PDT
poohbear@myth63:$ time ./myth-buster-concurrent
myth57 has this many CS110-student processes: 114
myth51 has this many CS110-student processes: 69
myth52 has this many CS110-student processes: 353
myth56 has this many CS110-student processes: 134
myth55 has this many CS110-student processes: 83
myth58 has this many CS110-student processes: 117
myth53 has this many CS110-student processes: 91
myth54 has this many CS110-student processes: 162
myth63 has this many CS110-student processes: 120
myth60 has this many CS110-student processes: 60
myth66 has this many CS110-student processes: 72
myth65 has this many CS110-student processes: 84
myth62 has this many CS110-student processes: 73
myth64 has this many CS110-student processes: 79
myth59 has this many CS110-student processes: 191
myth61 has this many CS110-student processes: 205
Machine least loaded by CS110 students: myth60
Number of CS110 processes on least loaded machine: 60
real 0m0.572s
user 0m0.303s
sys 0m0.118s
poohbear@myth63:$