Created
November 30, 2011 14:37
-
-
Save skayred/1409278 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <cstdio> | |
| #include <cstdlib> | |
| #include <ctime> | |
| #include <cstring> | |
| #include <sys/types.h> | |
| #include <sys/wait.h> | |
| #include <errno.h> | |
| #include "PiCalculator.h" | |
| #include "zhelpers.hpp" | |
| using namespace std; | |
| pair<int, int> parseArgs(int argc, char* argv[]) { | |
| return pair<int, int>(atoi(argv[1]), atoi(argv[2])); | |
| } | |
| int main(int argc, char* argv[]) { | |
| pair<int, int> args = parseArgs(argc, argv); | |
| zmq::context_t context(1); | |
| zmq::socket_t server(context, ZMQ_REP); | |
| pid_t pid = fork(); | |
| for (int i = 0 ; i < (args.first - 1) ; i++) { | |
| if (pid == 0) { | |
| pid = fork(); | |
| } | |
| } | |
| time_t startTime = time(NULL); | |
| if (pid > 0) { | |
| // Child process | |
| PiCalculator* calculator = new PiCalculator(args.second); | |
| CalculationResult* result = calculator->calculate(pid); | |
| string serialized = result->serialize(); | |
| zmq::socket_t * client = new zmq::socket_t(context, ZMQ_REQ); | |
| client->connect("tcp://localhost:5555"); | |
| if (s_send(*client, serialized)) { | |
| cout << "Sent" << endl; | |
| } else { | |
| cout << "Not sent" << endl; | |
| } | |
| delete client; | |
| exit(1); | |
| } else { | |
| // Parent process | |
| while (true) { | |
| int status; | |
| pid_t done = wait(&status); | |
| if (done == -1) { | |
| if (errno == ECHILD) break; // no more child processes | |
| } else { | |
| if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { | |
| cerr << "pid " << done << " failed" << endl; | |
| exit(1); | |
| } | |
| } | |
| } | |
| cout << "Wait done" << endl; | |
| server.bind("tcp://*:5555"); | |
| //cout << s_recv(server) << endl; | |
| } | |
| time_t endTime = time(NULL); | |
| cout << (endTime - startTime) << " second." << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment