Advertisement
batuwar09

c++

Mar 10th, 2022
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <thread>
  4. #include <sys/time.h>
  5. #include <sys/wait.h>
  6.  
  7. #include "BoundedBuffer.h"
  8. #include "common.h"
  9. #include "Histogram.h"
  10. #include "HistogramCollection.h"
  11. #include "FIFORequestChannel.h"
  12.  
  13. // ecgno to use for datamsgs
  14. #define EGCNO 1
  15.  
  16. using namespace std;
  17.  
  18.  
  19. void patient_thread_function (/* add necessary arguments */) {
  20.     // functionality of the patient threads
  21.     // get data (read from files)
  22.     // push data (boundedbuffer)
  23.  
  24.     // take a patient p_no;
  25.     // for n request, produce a datamsg(p_no, time, ECGNO) and push to request_buffer
  26.     //      - time dependent on current request
  27.     //      - at 0, time = 0.000; at 1, time = 0.004; at 2, time = 0.008, ...
  28. }
  29.  
  30. void file_thread_function (/* add necessary arguments */) {
  31.     // functionality of the file thread
  32.  
  33.     // file size
  34.     // open output file; allocate memory with fseek; close the file
  35.     // while (offset < file_size), produce filemsg(offset, m)+filename and push to request_buffer
  36.     //      - increment offset; be careful with the final message
  37.  
  38. }
  39.  
  40. void worker_thread_function (/* add necessary arguments */) {
  41.     // functionality of the worker threads
  42.     // pop data (bounded buffer)
  43.     // write data to CSV
  44.  
  45.     // forever loop
  46.     // pop message from the request_buffer
  47.     // view line 120 in server (process_request function) for how to decide current message
  48.     // send request across a FIFO channel, collect response
  49.     //
  50.     // if DATA_msg:
  51.     //      - create pair of p_no from message and response from server
  52.     //      - push that pair to the response_buffer
  53.     // if FILE_msg
  54.     //      - collect file_name from the message
  55.     //      - open the file in update mode
  56.     //      - fseek(SEEK_SET) to offset of the filemsg
  57.     //      - write the buffer from the server
  58. }
  59.  
  60. void histogram_thread_function (/* add necessary arguments */) {
  61.     // functionality of the histogram threads
  62.     // forever loop
  63.     // pop response from the response_buffer
  64.     // call HC::update(resp->p_no, respnse->double)
  65. }
  66.  
  67.  
  68. int main (int argc, char* argv[]) {
  69.     int n = 1000;   // default number of requests per "patient"
  70.     int p = 10;     // number of patients [1,15]
  71.     int w = 100;    // default number of worker threads
  72.     int h = 20;     // default number of histogram threads
  73.     int b = 20;     // default capacity of the request buffer (should be changed)
  74.     int m = MAX_MESSAGE;    // default capacity of the message buffer
  75.     string f = "";  // name of file to be transferred
  76.     bool n_check = false, p_check = false, w_check = false, h_check = false, b_check = false, m_check = false, f_check = false;
  77.    
  78.     // read arguments
  79.     int opt;
  80.     while ((opt = getopt(argc, argv, "n:p:w:h:b:m:f:")) != -1) {
  81.         switch (opt) {
  82.             case 'n':
  83.                 n = atoi(optarg);
  84.                 n_check = true;
  85.                 break;
  86.             case 'p':
  87.                 p = atoi(optarg);
  88.                 p_check = true;
  89.                 break;
  90.             case 'w':
  91.                 w = atoi(optarg);
  92.                 w_check = true;
  93.                 break;
  94.             case 'h':
  95.                 h = atoi(optarg);
  96.                 h_check = true;
  97.                 break;
  98.             case 'b':
  99.                 b = atoi(optarg);
  100.                 b_check = true;
  101.                 break;
  102.             case 'm':
  103.                 m = atoi(optarg);
  104.                 m_check = true;
  105.                 break;
  106.             case 'f':
  107.                 f = optarg;
  108.                 f_check = true;
  109.                 break;
  110.         }
  111.     }
  112.    
  113.     // fork and exec the server
  114.     int pid = fork();
  115.     if (pid == 0) {
  116.         execl("./server", "./server", "-m", (char*) to_string(m).c_str(), nullptr);
  117.     }
  118.     else{
  119.  
  120.     }
  121.    
  122.     // initialize overhead (including the control channel)
  123.     FIFORequestChannel* chan = new FIFORequestChannel("control", FIFORequestChannel::CLIENT_SIDE);
  124.     BoundedBuffer request_buffer(b);
  125.     BoundedBuffer response_buffer(b);
  126.     HistogramCollection hc;
  127.  
  128.     // array of producer threads (if data transfer, p elements; if file transfer, 1 element)
  129.     // array of FIFOs (w elements)
  130.     // array of worker threads (w elements)
  131.     // array of histogram threads (if data transfer, h elements; if file, 0 elements)
  132.  
  133.     // making histograms and adding to collection
  134.     for (int i = 0; i < p; i++) {
  135.         Histogram* h = new Histogram(10, -2.0, 2.0);
  136.         hc.add(h);
  137.     }
  138.    
  139.     // record start time
  140.     struct timeval start, end;
  141.     gettimeofday(&start, 0);
  142.  
  143.     /* create all threads here */
  144.  
  145.     // if (data transfer):
  146.     //      - create p patient_threads
  147.     //      - create h histogram_threads (store hist array)
  148.     //
  149.     // if (file transfer):
  150.     //      - create 1 file_thread
  151.     //      - create w worker_threads (store worker array)  [producer for histogram thread]
  152.     //          -> create w channels (store FIFO array)
  153.  
  154.  
  155.     /* join all threads here */
  156.     // iterate thread arrays, call join
  157.     //      - order will be patient, worker, histogram
  158.  
  159.     // record end time
  160.     gettimeofday(&end, 0);
  161.  
  162.     // print the results
  163.     if (f == "") {
  164.         hc.print();
  165.     }
  166.     int secs = ((1e6*end.tv_sec - 1e6*start.tv_sec) + (end.tv_usec - start.tv_usec)) / ((int) 1e6);
  167.     int usecs = (int) ((1e6*end.tv_sec - 1e6*start.tv_sec) + (end.tv_usec - start.tv_usec)) % ((int) 1e6);
  168.     cout << "Took " << secs << " seconds and " << usecs << " micro seconds" << endl;
  169.  
  170.     // quit and close all channels in FIFO array
  171.  
  172.     // quit and close control channel
  173.     MESSAGE_TYPE q = QUIT_MSG;
  174.     chan->cwrite ((char *) &q, sizeof (MESSAGE_TYPE));
  175.     cout << "All Done!" << endl;
  176.     delete chan;
  177.  
  178.     // wait for server to exit
  179.     wait(nullptr);
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement