Internet Ring Buffer example of Bi-directional Rendezvous
static char internet[8];
static semaphore emptyBuffers[8];
static semaphore fullBuffers[0];
static void writer() {
for( int i = 0; i < 400; i++) {
char ch = randomChar(); // calculate the next char to be written to the internet
// need a slot on the shared internet to write this char
emptyBuffers.wait();
internet[ i % 8 ] = ch; // the exact spot where the next char goes
fullBuffers.signal();
}
}
static void reader() {
for( int i = 0; i < 400; i++) {
// reader needs to wait for there to be a character to read
fullBuffers.wait();
char ch = internet[ i % 8 ]; // from shared internet to a local var
emptyBuffers.signal();
processChar(ch);
}
}
int main( ) {
thread w(writer);
thread r(reader);
w.join();
r.join();
return 0;
}
Myth-Buster example to show polling a bunch of servers to find out how many processes are running on each server. Goal is to surface the least used server.
Sequential Version
short countProcesses(unsigned short num, const set<string> &studentids);
void compileProcessCountMap(map< short, int> &m, const set<string> &studentids) {
for( unsigned short num = 1; num <= highestServerNumber; num++) {
short count = countProcesses(num, studentids);
if(count >= 0) {
m[num] = count; // add the new key/value to the map
}
}
}
int main() {
set<string> studentids;
readStudentids(studentids, “/class/studentids.txt”);
map<short, int> processCountMap; // map of server ids to the number of processes
// pass in the map to update and the student’s ids from the class
compileProcessCountMap( processCountMap, studentids);
return 0;
}
Concurrent Version of Myth-Buster
static set<string> studentids;
static map<short, int> counts;
static mutex m;
static void compileMap() {
thread threads[highestServerNumber];
for( size_t i = 0; i < highestServerNumber; i++) {
threads[i] = thread(
);
}
for(thread &t : threads) {
t.join();
}
}
int main() {
readStudentids(“/class/ studentids.txt”);
compileMap();
return 0;
}
Sorry, the comment form is closed at this time.