Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/mman.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. #include <stdlib.h>
  8. #include <signal.h>
  9. #include <semaphore.h>
  10. #include <string.h>
  11. #include <stdbool.h>
  12.  
  13. #define SHM_PATH "/shmue"
  14. #define PERMISSION (0600)
  15.  
  16. #define SEM_SERVER "/semserver"
  17. #define SEM_CLIENT "/semclient"
  18. #define CLIENT_CONT "/client_cont"
  19.  
  20. #define LINE_SIZE (100)
  21.  
  22. sem_t *server_sem;
  23. sem_t *client_cont_sem;
  24. sem_t *client_sem;
  25.  
  26. int shmfd = -1;
  27.  
  28. typedef struct{
  29.  
  30.     int account_nr;
  31.     int response;
  32.    
  33.     int log_in;
  34. }SHM_msg;
  35.  
  36. SHM_msg *shared_message;
  37.  
  38. void setupSharedMemory(void);
  39. void bail_out(int exit_code, char *error_msg);
  40. void free_resources(void);
  41. void check_login(void);
  42. void printSHM(void);
  43. void createSemaphores(void);
  44. void signal_handler(int signum);
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48.     if(atexit(free_resources) != 0)
  49.     {
  50.         fprintf(stderr,"error at atexit.\n");
  51.     }
  52.    
  53.     signal(SIGINT,signal_handler);
  54.    
  55.     createSemaphores();
  56.     setupSharedMemory();
  57.    
  58.     while(1)
  59.     {
  60.         check_login();
  61.     }
  62.    
  63.     return 1;
  64. }
  65.  
  66. void signal_handler(int signum)
  67. {
  68.     free_resources();
  69.     exit(signum);
  70. }
  71.  
  72. void createSemaphores() {
  73.  
  74.     client_sem = sem_open(SEM_CLIENT,O_CREAT, PERMISSION,1);
  75.     server_sem = sem_open(SEM_SERVER,O_CREAT, PERMISSION,0);
  76.     client_cont_sem = sem_open(CLIENT_CONT,O_CREAT, PERMISSION,0);
  77.    
  78.     if(client_sem == SEM_FAILED || server_sem == SEM_FAILED || client_cont_sem == SEM_FAILED) {
  79.         printf("jdsds");
  80.     }
  81.  
  82. }
  83.  
  84. /*
  85.  
  86. void createSemaphores()
  87. {
  88.     client_sem = sem_open(SEM_CLIENT, O_CREAT, PERMISSION,1);
  89.     server_sem = sem_open(SEM_SERVER, O_CREAT, PERMISSION,0);
  90.     client_cont_sem = sem_open(CLIENT_CONT, O_CREAT, PERMISSION,0);
  91.    
  92.     if(client_sem == SEM_FAILED || server_sem == SEM_FAILED || client_cont_sem == SEM_FAILED)
  93.     {
  94.         bail_out(EXIT_FAILURE, "error at creating semaphores");
  95.     }  
  96. }
  97. */
  98.  
  99. void check_login(void)
  100. {
  101.    
  102.    
  103.     if(shared_message->log_in == 1)
  104.     {
  105.         sem_wait(server_sem);
  106.        
  107.         printf("%d logged in!\n",shared_message->account_nr);
  108.         printSHM();
  109.        
  110.         shared_message->response = 1;
  111.         shared_message->log_in = -1;
  112.        
  113.         sleep(3);
  114.        
  115.         sem_post(client_cont_sem);
  116.     }
  117.    
  118.    
  119. }
  120.  
  121. void setupSharedMemory()
  122. {
  123.     shmfd = shm_open(SHM_PATH, O_CREAT | O_RDWR, PERMISSION);
  124.    
  125.     if(shmfd == -1)
  126.     {
  127.         bail_out(EXIT_FAILURE,"could not create shm obj"); 
  128.     }
  129.    
  130.     if(ftruncate(shmfd, sizeof(SHM_msg)) == -1)
  131.     {
  132.         bail_out(EXIT_FAILURE,"could not truncate memory");
  133.     }
  134.    
  135.     shared_message = mmap(NULL, sizeof(SHM_msg), PROT_READ | PROT_WRITE, MAP_SHARED,shmfd,0);
  136.    
  137.     if(shared_message == MAP_FAILED)
  138.     {
  139.         bail_out(EXIT_FAILURE,"could not map memory");
  140.     }
  141.    
  142.     shared_message->account_nr = -1;
  143.     shared_message->response = -1;
  144.     shared_message->log_in = -1;
  145.    
  146. }
  147.  
  148. void bail_out(int exit_code, char *error_msg)
  149. {
  150.     fprintf(stderr,"Error in program:  %s\n", error_msg);
  151.    
  152.     free_resources();
  153.    
  154.     exit(exit_code);
  155. }
  156.  
  157. void free_resources()
  158. {
  159.  
  160.     printf("Free Resources\n");
  161.     if(shmfd != -1)
  162.     {
  163.         if(close(shmfd) == -1)
  164.         {
  165.             fprintf(stderr,"could not close shm"); 
  166.         }
  167.        
  168.         if(shm_unlink(SHM_PATH) == -1)
  169.         {
  170.             fprintf(stderr,"could not unlink shm");
  171.         }  
  172.        
  173.         if(munmap(shared_message,sizeof(SHM_msg)) == -1)
  174.         {
  175.             fprintf(stderr,"could not unmap shm");
  176.         }
  177.    
  178.        
  179.         printf("Resources freed.\n");
  180.     }
  181.    
  182.     if(sem_close(client_sem) == -1)
  183.     {
  184.         fprintf(stderr,"could not close client_sem\n");
  185.     }
  186.     if(sem_unlink(SEM_CLIENT) == -1)
  187.     {
  188.         fprintf(stderr,"could not unlink SEM_CLIENT\n");
  189.     }
  190.    
  191.    
  192.    
  193.     if(sem_close(server_sem) == -1)
  194.     {
  195.         fprintf(stderr,"could not close client_sem\n");
  196.     }
  197.     if(sem_unlink(SEM_SERVER) == -1)
  198.     {
  199.         fprintf(stderr,"could not unlink SEM_CLIENT\n");
  200.     }
  201.    
  202.    
  203.    
  204.     if(sem_close(client_cont_sem) == -1)
  205.     {
  206.         fprintf(stderr,"could not close client_sem\n");
  207.     }
  208.     if(sem_unlink(CLIENT_CONT) == -1)
  209.     {
  210.         fprintf(stderr,"could not unlink SEM_CLIENT\n");
  211.     }
  212. }
  213.  
  214. void printSHM()
  215. {
  216.     printf("Account nr: %d\n",shared_message->account_nr);
  217.     printf("Response: %d\n",shared_message->response);
  218.     printf("Login: %d\n",shared_message->log_in);
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement