Create a simple message passing program in C++ for a UNIX environment utilizing
ID: 665584 • Letter: C
Question
Create a simple message passing program in C++ for a UNIX environment utilizing a circular buffer.
http://wiki.osdev.org/Message_Passing_Tutorial
You may run N processes in one host.
Each process has two designated ports - one for sending and the other for receiving.
Each program for each process will be run with an argument telling which process (Nth process)
You may assume that each process has a table of process with 2 ports as following:
Process i
IP address
Port# to send
Port# to receive
0
127.0.0.1
9000
9001
1
127.0.0.1
9100
9101
2
127.0.0.1
9200
9201
and so on
When N processes (or threads) in a ring are up and ready, Process 0 sends a message to be received by Process 1, and so on, to back to Process 0 and done. Whenever a process sends or receives a message, it prints its process # (N), "the message", and "sent" or "received".
Process i
IP address
Port# to send
Port# to receive
0
127.0.0.1
9000
9001
1
127.0.0.1
9100
9101
2
127.0.0.1
9200
9201
and so on
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <functional>
#include "parser.h"
#include "variables.h"
#include "reedoo.h"
#include "io.h"
#include "cond.h"
using namespace std;
void parse(vector<string> tokens) {
int errcount = 0;
int linenum = 1;
int i = 0;
bool cond = false;
int cond_result = 2;
while (i < tokens.size()) {
TOP:if (tokens[i] + " " + tokens[i+1] == "print sc") {
cout << SYNTAX_ERROR << "'print' supplied without anything to print [line " << linenum << "]" << endl;
errcount++;
i+=2;
break;
}
if (tokens[i] + " " + tokens[i+1].substr(0,6) + " " + tokens[i+2] == "print string sc" or
tokens[i] + " " + tokens[i+1].substr(0,3) + " " + tokens[i+2] == "print num sc" or
tokens[i] + " " + tokens[i+1].substr(0,4) + " " + tokens[i+2] == "print expr sc" or
tokens[i] + " " + tokens[i+1].substr(0,8) + " " + tokens[i+2] == "print variable sc") {
if (tokens[i+1].substr(0,8) == "variable") {
doPRINT(goGETVAR(tokens[i+1]));
} else {
doPRINT(tokens[i+1]);
}
i+=3;
} else if (tokens[i].substr(0,8) + " " + tokens[i+1] + " " + tokens[i+2].substr(0,3) + " " + tokens[i+3] == "variable eq num sc" or
tokens[i].substr(0,8) + " " + tokens[i+1] + " " + tokens[i+2].substr(0,6) + " " + tokens[i+3] == "variable eq string sc" or
tokens[i].substr(0,8) + " " + tokens[i+1] + " " + tokens[i+2].substr(0,4) + " " + tokens[i+3] == "variable eq expr sc" or
tokens[i].substr(0,8) + " " + tokens[i+1] + " " + tokens[i+2].substr(0,8) + " " + tokens[i+3] == "variable eq variable sc") {
doASSIGN(tokens[i],tokens[i+2]);
i+=4;
} else if (tokens[i] + " " + tokens[i+1].substr(0,4) + " " + tokens[i+2] == "if cond opencb") {
//cout << eval_cond(tokens[i+1].substr(5)) << endl;
cond_result = eval_cond(tokens[i+1].substr(5));
if (eval_cond(tokens[i+1].substr(5))) {
// Run true block
//cout << "TOKENS: " << tokens[i+1].substr(5) << eval_cond(tokens[i+1].substr(5)) << endl;
//i+=3;
} else {
//cout << "TOKENS: " << tokens[i+1].substr(5) << eval_cond(tokens[i+1].substr(5)) << endl;
while (tokens[i] != "closecb") {
i++;
}
i++;
}
i+=3;
} else if (tokens[i] == "closecb") {
if (tokens[i+1] == "else") {
i+=1;
if (cond_result == 0) {
}
} else {
i+=2;
}
} else {
break;
}
if (i >= tokens.size()-2) {
break;
}
if (errcount > 0) {
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.