SHARE
TWEET

Untitled

a guest Mar 26th, 2020 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <filesystem>
  3. #include <aio.h>
  4. #include <pthread.h>
  5. #include <fcntl.h>
  6. #include <signal.h>
  7. #include <unistd.h>
  8. #include <string>
  9. using namespace std;
  10. namespace fs = filesystem;
  11.  
  12.  
  13. void* WriterThread(void* par)
  14. {
  15.     int lastSignal;
  16.     int readPipe = *((int*)par);
  17.     int file = open("output.txt",O_WRONLY | O_CREAT, S_IRWXU);
  18.  
  19.     sigevent event;
  20.     event.sigev_notify = SIGEV_SIGNAL;
  21.     event.sigev_signo = 12;
  22.  
  23.     sigset_t sigset1;
  24.     sigemptyset(&sigset1);
  25.     sigaddset(&sigset1, 12);
  26.     sigaddset(&sigset1, 13);
  27.  
  28.     aiocb config;
  29.     config.aio_fildes = file;
  30.     config.aio_offset = 0;
  31.     config.aio_reqprio = 0;
  32.     config.aio_sigevent = event;
  33.  
  34.     while(true)
  35.     {
  36.         char buf[BUFSIZ] = {};
  37.         for (int i=0;i<BUFSIZ;i++)
  38.             buf[i] = 0;
  39.         sigwait(&sigset1, &lastSignal);
  40.         if (lastSignal == 13)
  41.             break;
  42.         read(readPipe,buf,BUFSIZ);
  43.         config.aio_buf = buf;
  44.         config.aio_fildes = file;
  45.         config.aio_nbytes = string(buf).size();
  46.         aio_write(&config);
  47.         sigwait(&sigset1,&lastSignal);
  48.         config.aio_offset += string(buf).size();
  49.         raise(10);
  50.     }  
  51.     close(readPipe);
  52.     close(file);
  53.     return nullptr;
  54. }
  55.  
  56. void* ReaderThread(void *par)
  57. {
  58.     int lastSignal;
  59.     fs::path filesPath(fs::current_path() /= fs::path("files"));
  60.     fs::create_directories(filesPath);
  61.     int pipe1 = *((int*)par);
  62.  
  63.     sigevent event;
  64.     event.sigev_notify = SIGEV_SIGNAL;
  65.     event.sigev_signo = 10;
  66.  
  67.     sigset_t sigset1;
  68.     sigemptyset(&sigset1);
  69.     sigaddset(&sigset1, 10);
  70.  
  71.  
  72.     aiocb config;
  73.     config.aio_offset = 0;
  74.     config.aio_nbytes = BUFSIZ;
  75.     config.aio_reqprio = 0;
  76.     config.aio_sigevent = event;
  77.  
  78.     for (auto it : fs::directory_iterator(filesPath))
  79.     {
  80.         int file = open(it.path().c_str(),O_RDONLY);
  81.         char buf[BUFSIZ] = {};
  82.  
  83.         config.aio_buf = buf;
  84.         config.aio_fildes = file;
  85.  
  86.         aio_read(&config);
  87.         sigwait(&sigset1,&lastSignal);
  88.         write(pipe1, buf, string(buf).size());
  89.         raise(12);
  90.         sigwait(&sigset1,&lastSignal);
  91.         close(file);
  92.     }
  93.     close(pipe1);
  94.     raise(13);
  95.     return nullptr;
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top