Spring 2021
Instructors Roz Cyrus and 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@myth55:$ date
Wed 05 May 2021 12:23:54 PM PDT
poohbear@myth55:$ time ./myth-buster-sequential
myth51 has this many CS110-student processes: 195
myth52 has this many CS110-student processes: 110
myth53 has this many CS110-student processes: 209
myth54 has this many CS110-student processes: 168
myth55 has this many CS110-student processes: 161
myth56 has this many CS110-student processes: 123
myth57 has this many CS110-student processes: 134
myth58 has this many CS110-student processes: 90
myth59 has this many CS110-student processes: 112
myth60 has this many CS110-student processes: 92
myth61 has this many CS110-student processes: 295
myth62 has this many CS110-student processes: 140
myth63 has this many CS110-student processes: 334
myth64 has this many CS110-student processes: 101
myth65 has this many CS110-student processes: 138
myth66 has this many CS110-student processes: 1413
Machine least loaded by CS110 students: myth58
Number of CS110 processes on least loaded machine: 90
real 0m4.694s
user 0m0.312s
sys 0m0.139s
poohbear@myth55:$
poohbear@myth55:$ date
Wed Oct 21 17:34:19 PDT 2020
poohbear@myth55:$ time ./myth-buster-sequential
myth51 has this many CS110-student processes: 200
myth52 has this many CS110-student processes: 110
myth53 has this many CS110-student processes: 215
myth54 has this many CS110-student processes: 165
myth55 has this many CS110-student processes: 164
myth56 has this many CS110-student processes: 116
myth57 has this many CS110-student processes: 134
myth58 has this many CS110-student processes: 81
myth59 has this many CS110-student processes: 107
myth60 has this many CS110-student processes: 86
myth61 has this many CS110-student processes: 296
myth62 has this many CS110-student processes: 140
myth63 has this many CS110-student processes: 334
myth64 has this many CS110-student processes: 101
myth65 has this many CS110-student processes: 138
myth66 has this many CS110-student processes: 1415
Machine least loaded by CS110 students: myth58
Number of CS110 processes on least loaded machine: 81
real 0m3.959s
user 0m0.305s
sys 0m0.187s
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();
}