Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. #include <dlfcn.h>
  2. #include <semaphore.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/mman.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10.  
  11. //#define DEBUG
  12.  
  13. #ifdef DEBUG
  14. #define DEBUG_PRINT(x) printf x;
  15. #else
  16. #define DEBUG_PRINT(x) ;
  17. #endif
  18.  
  19. typedef struct {
  20.     sem_t request_ready;
  21.     sem_t response_ready;
  22.     char func_name[20];
  23.     double value;
  24.     double result;
  25. } shared_data_t;
  26.  
  27. typedef double (*dd_func_t)(double);
  28.  
  29. const char SHM_NAME[] = "/maximmum_shm";
  30. enum { SHM_PERMS = 0644 };
  31.  
  32. void prepare_shared_data(shared_data_t* data) {
  33.     memset(data, 0, sizeof(shared_data_t));
  34.  
  35.     sem_init(&data->request_ready, 1, 0);
  36.     sem_init(&data->response_ready, 1, 0);
  37. }
  38.  
  39. int main(int argc, char** argv)
  40. {
  41.     if (argc != 2) {
  42.         fprintf(stderr, "Usage: %s <library name>", argv[0]);
  43.         return 1;
  44.     }
  45.  
  46.     void* lib = dlopen(argv[1], RTLD_NOW);
  47.     if (!lib) {
  48.         fprintf(stderr, "Error on dlopen: %s\n", dlerror());
  49.         return 1;
  50.     }
  51.  
  52.     const size_t page_size = sysconf(_SC_PAGESIZE);
  53.     int shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, SHM_PERMS);
  54.     const size_t needed_size = sizeof(shared_data_t);
  55.     const size_t shm_size = (needed_size % page_size == 0 ?
  56.                             needed_size :
  57.                             (needed_size / page_size + 1) * page_size);
  58.     ftruncate(shm_fd, shm_size);
  59.  
  60.     shared_data_t* data =
  61.         mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  62.     prepare_shared_data(data);
  63.  
  64.     sem_t* request_ready_sem = &data->request_ready;
  65.     sem_t* response_ready_sem = &data->response_ready;
  66.  
  67.     printf("%s\n", SHM_NAME);
  68.     fflush(stdout);
  69.  
  70.     do {
  71.         DEBUG_PRINT(("1: sem_wait...\n"));
  72.         sem_wait(request_ready_sem);
  73.         DEBUG_PRINT(("1: sem waited\n"));
  74.  
  75.         if (data->func_name[0] == '\0') {
  76.             DEBUG_PRINT(("1: func_name is empty, exiting...\n"));
  77.             break;
  78.         }
  79.  
  80.         void* sym = dlsym(lib, data->func_name);
  81.         if (!sym) {
  82.             fprintf(stderr, "Error on dlsym: %s\n", dlerror());
  83.             break;
  84.         }
  85.  
  86.         dd_func_t func = sym;
  87.  
  88.         data->result = func(data->value);
  89.  
  90.         sem_post(response_ready_sem);
  91.         DEBUG_PRINT(("1: sem posted\n"));
  92.     } while (data->func_name[0] != '\0');
  93.  
  94.     sem_destroy(response_ready_sem);
  95.     sem_destroy(request_ready_sem);
  96.     munmap((void*)data, shm_size);
  97.     close(shm_fd);
  98.     shm_unlink(SHM_NAME);
  99.     dlclose(lib);
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement