SHARE
TWEET

Untitled

a guest Mar 24th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <sys/signalfd.h>
  3. #include <sys/mman.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <semaphore.h>
  11. #include <dlfcn.h>
  12.  
  13. typedef struct {
  14.     sem_t request_ready;
  15.     sem_t response_ready;
  16.     char func_name[20];
  17.     double value;
  18.     double result;
  19. } shared_data_t;
  20.  
  21. int main(int argc, char** argv) {
  22.     const char shm_name[] = "gachimuchi1488";
  23.     int shm_id = shm_open(shm_name, O_RDWR | O_CREAT, 0666);
  24.    
  25.     size_t page_size = sysconf(_SC_PAGESIZE);
  26.     size_t map_size = sizeof(shared_data_t);
  27.     if (map_size % page_size > 0) {
  28.         map_size = (map_size / page_size + 1) * page_size;
  29.     }
  30.    
  31.     ftruncate(shm_id, map_size);
  32.     shared_data_t* shared_data = mmap(NULL, map_size,
  33.                                       PROT_READ | PROT_WRITE,
  34.                                       MAP_SHARED, shm_id, 0);
  35.    
  36.     sem_init(&shared_data->request_ready, 1, 0);
  37.     sem_init(&shared_data->response_ready, 1, 0);
  38.    
  39.     printf("%s", shm_name);
  40.     fflush(stdout);
  41.    
  42.     void* shared_lib = dlopen(argv[1], RTLD_LAZY);
  43.  
  44.     while (1) {
  45.         sem_wait(&shared_data->request_ready);
  46.         if (strlen(shared_data->func_name) == 0) break;
  47.         double (*func)(double) = dlsym(shared_lib, shared_data->func_name);
  48.         shared_data->result = func(shared_data->value);
  49.         sem_post(&shared_data->response_ready);
  50.     }
  51.    
  52.     sem_destroy(&shared_data->request_ready);
  53.     sem_destroy(&shared_data->response_ready);
  54.     munmap(shared_data, map_size);
  55.     shm_unlink(shm_name);
  56. }
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