Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <semaphore.h>
- #include <sys/mman.h>
- #include <sys/stat.h>
- #include <limits.h>
- #include <fcntl.h>
- #include <dlfcn.h>
- #include <semaphore.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <unistd.h>
- #include <sys/types.h>
- typedef struct {
- sem_t request_ready; // начальное значение 0
- sem_t response_ready; // начальное значение 0
- char func_name[20];
- double value;
- double result;
- } shared_data_t;
- typedef double (*function_t) (double);
- int
- main (int argc, char *argv[]) {
- if (argc < 2) {
- abort ();
- }
- void *lib = dlopen (argv[1], RTLD_NOW);
- int shm_id = shm_open ("/aqqq79703qw", O_CREAT | O_RDWR, 0644);
- if (!lib) {
- abort ();
- }
- if (shm_id < 0)
- fprintf (stderr, "shmget\n");
- size_t map_size = sizeof (shared_data_t);
- size_t page_size = sysconf (_SC_PAGESIZE);
- if (map_size % page_size > 0) {
- map_size = (map_size / page_size + 1) * page_size;
- }
- shared_data_t *shd_p = (shared_data_t *) mmap (NULL, map_size,
- PROT_READ | PROT_WRITE,
- MAP_SHARED,
- shm_id, 0);
- ftruncate (shm_id, map_size);
- if (shd_p == MAP_FAILED) {
- // fprintf(stderr, "MAP_FAILED");
- abort ();
- }
- sem_init (&shd_p->request_ready, 1, 0);
- // printf("here\n");
- sem_init (&shd_p->response_ready, 1, 0);
- printf ("/aqqq79703qw\n");
- fflush (stdout);
- //ready
- while (1) {
- // printf("ready to wait\n");
- sem_wait (&shd_p->request_ready);
- if (strlen (shd_p->func_name) == 0) {
- //printf("HERE\n");
- break;
- }
- //printf("shd_p->func_name = %s\n", shd_p->func_name);
- double (*func) (double);
- char *error;
- func = dlsym (lib, shd_p->func_name);
- //printf("are\n");
- /* if ((error = dlerror())!=NULL){
- // printf("gere\n");
- break;
- } */
- double parametr = shd_p->value;
- //printf("are\n");
- double y = (*func) (parametr);
- //printf("are\n");
- shd_p->result = y;
- // printf("loser, y = %f\n", y);
- sem_post (&shd_p->response_ready);
- }
- dlclose (lib);
- sem_destroy (&shd_p->request_ready);
- sem_destroy (&shd_p->response_ready);
- fflush (stdout);
- munmap (shd_p, map_size);
- close (shm_id);
- shm_unlink ("/aqqq79703qw");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement