Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <thread>
- #include <sys/time.h>
- #include <sys/wait.h>
- #include "BoundedBuffer.h"
- #include "common.h"
- #include "Histogram.h"
- #include "HistogramCollection.h"
- #include "FIFORequestChannel.h"
- // ecgno to use for datamsgs
- #define EGCNO 1
- using namespace std;
- void patient_thread_function (/* add necessary arguments */) {
- // functionality of the patient threads
- // get data (read from files)
- // push data (boundedbuffer)
- // take a patient p_no;
- // for n request, produce a datamsg(p_no, time, ECGNO) and push to request_buffer
- // - time dependent on current request
- // - at 0, time = 0.000; at 1, time = 0.004; at 2, time = 0.008, ...
- }
- void file_thread_function (/* add necessary arguments */) {
- // functionality of the file thread
- // file size
- // open output file; allocate memory with fseek; close the file
- // while (offset < file_size), produce filemsg(offset, m)+filename and push to request_buffer
- // - increment offset; be careful with the final message
- }
- void worker_thread_function (/* add necessary arguments */) {
- // functionality of the worker threads
- // pop data (bounded buffer)
- // write data to CSV
- // forever loop
- // pop message from the request_buffer
- // view line 120 in server (process_request function) for how to decide current message
- // send request across a FIFO channel, collect response
- //
- // if DATA_msg:
- // - create pair of p_no from message and response from server
- // - push that pair to the response_buffer
- // if FILE_msg
- // - collect file_name from the message
- // - open the file in update mode
- // - fseek(SEEK_SET) to offset of the filemsg
- // - write the buffer from the server
- }
- void histogram_thread_function (/* add necessary arguments */) {
- // functionality of the histogram threads
- // forever loop
- // pop response from the response_buffer
- // call HC::update(resp->p_no, respnse->double)
- }
- int main (int argc, char* argv[]) {
- int n = 1000; // default number of requests per "patient"
- int p = 10; // number of patients [1,15]
- int w = 100; // default number of worker threads
- int h = 20; // default number of histogram threads
- int b = 20; // default capacity of the request buffer (should be changed)
- int m = MAX_MESSAGE; // default capacity of the message buffer
- string f = ""; // name of file to be transferred
- bool n_check = false, p_check = false, w_check = false, h_check = false, b_check = false, m_check = false, f_check = false;
- // read arguments
- int opt;
- while ((opt = getopt(argc, argv, "n:p:w:h:b:m:f:")) != -1) {
- switch (opt) {
- case 'n':
- n = atoi(optarg);
- n_check = true;
- break;
- case 'p':
- p = atoi(optarg);
- p_check = true;
- break;
- case 'w':
- w = atoi(optarg);
- w_check = true;
- break;
- case 'h':
- h = atoi(optarg);
- h_check = true;
- break;
- case 'b':
- b = atoi(optarg);
- b_check = true;
- break;
- case 'm':
- m = atoi(optarg);
- m_check = true;
- break;
- case 'f':
- f = optarg;
- f_check = true;
- break;
- }
- }
- // fork and exec the server
- int pid = fork();
- if (pid == 0) {
- execl("./server", "./server", "-m", (char*) to_string(m).c_str(), nullptr);
- }
- else{
- }
- // initialize overhead (including the control channel)
- FIFORequestChannel* chan = new FIFORequestChannel("control", FIFORequestChannel::CLIENT_SIDE);
- BoundedBuffer request_buffer(b);
- BoundedBuffer response_buffer(b);
- HistogramCollection hc;
- // array of producer threads (if data transfer, p elements; if file transfer, 1 element)
- // array of FIFOs (w elements)
- // array of worker threads (w elements)
- // array of histogram threads (if data transfer, h elements; if file, 0 elements)
- // making histograms and adding to collection
- for (int i = 0; i < p; i++) {
- Histogram* h = new Histogram(10, -2.0, 2.0);
- hc.add(h);
- }
- // record start time
- struct timeval start, end;
- gettimeofday(&start, 0);
- /* create all threads here */
- // if (data transfer):
- // - create p patient_threads
- // - create h histogram_threads (store hist array)
- //
- // if (file transfer):
- // - create 1 file_thread
- // - create w worker_threads (store worker array) [producer for histogram thread]
- // -> create w channels (store FIFO array)
- /* join all threads here */
- // iterate thread arrays, call join
- // - order will be patient, worker, histogram
- // record end time
- gettimeofday(&end, 0);
- // print the results
- if (f == "") {
- hc.print();
- }
- int secs = ((1e6*end.tv_sec - 1e6*start.tv_sec) + (end.tv_usec - start.tv_usec)) / ((int) 1e6);
- int usecs = (int) ((1e6*end.tv_sec - 1e6*start.tv_sec) + (end.tv_usec - start.tv_usec)) % ((int) 1e6);
- cout << "Took " << secs << " seconds and " << usecs << " micro seconds" << endl;
- // quit and close all channels in FIFO array
- // quit and close control channel
- MESSAGE_TYPE q = QUIT_MSG;
- chan->cwrite ((char *) &q, sizeof (MESSAGE_TYPE));
- cout << "All Done!" << endl;
- delete chan;
- // wait for server to exit
- wait(nullptr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement