Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <semaphore.h>
  3. #include <sys/mman.h>
  4. #include <sys/stat.h>
  5. #include <limits.h>
  6. #include <fcntl.h>
  7. #include <dlfcn.h>
  8. #include <semaphore.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/ipc.h>
  14. #include <sys/shm.h>
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. typedef struct {
  18. sem_t request_ready; // начальное значение 0
  19. sem_t response_ready; // начальное значение 0
  20. char func_name[20];
  21. double value;
  22. double result;
  23. } shared_data_t;
  24.  
  25. typedef double (*function_t) (double);
  26.  
  27. int
  28. main (int argc, char *argv[]) {
  29. if (argc < 2) {
  30. abort ();
  31. }
  32. void *lib = dlopen (argv[1], RTLD_NOW);
  33. int shm_id = shm_open ("/aqqq79703qw", O_CREAT | O_RDWR, 0644);
  34. if (!lib) {
  35. abort ();
  36. }
  37. if (shm_id < 0)
  38. fprintf (stderr, "shmget\n");
  39.  
  40. size_t map_size = sizeof (shared_data_t);
  41. size_t page_size = sysconf (_SC_PAGESIZE);
  42. if (map_size % page_size > 0) {
  43. map_size = (map_size / page_size + 1) * page_size;
  44. }
  45. shared_data_t *shd_p = (shared_data_t *) mmap (NULL, map_size,
  46. PROT_READ | PROT_WRITE,
  47. MAP_SHARED,
  48. shm_id, 0);
  49.  
  50. ftruncate (shm_id, map_size);
  51. if (shd_p == MAP_FAILED) {
  52. // fprintf(stderr, "MAP_FAILED");
  53. abort ();
  54. }
  55.  
  56. sem_init (&shd_p->request_ready, 1, 0);
  57. // printf("here\n");
  58.  
  59. sem_init (&shd_p->response_ready, 1, 0);
  60.  
  61. printf ("/aqqq79703qw\n");
  62.  
  63. fflush (stdout);
  64. //ready
  65.  
  66. while (1) {
  67. // printf("ready to wait\n");
  68. sem_wait (&shd_p->request_ready);
  69. if (strlen (shd_p->func_name) == 0) {
  70. //printf("HERE\n");
  71. break;
  72. }
  73. //printf("shd_p->func_name = %s\n", shd_p->func_name);
  74.  
  75. double (*func) (double);
  76. char *error;
  77. func = dlsym (lib, shd_p->func_name);
  78. //printf("are\n");
  79. /* if ((error = dlerror())!=NULL){
  80. // printf("gere\n");
  81. break;
  82. } */
  83. double parametr = shd_p->value;
  84. //printf("are\n");
  85. double y = (*func) (parametr);
  86. //printf("are\n");
  87. shd_p->result = y;
  88. // printf("loser, y = %f\n", y);
  89. sem_post (&shd_p->response_ready);
  90. }
  91. dlclose (lib);
  92. sem_destroy (&shd_p->request_ready);
  93. sem_destroy (&shd_p->response_ready);
  94. fflush (stdout);
  95. munmap (shd_p, map_size);
  96. close (shm_id);
  97. shm_unlink ("/aqqq79703qw");
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement