Tobiahao

S01_PAMIEC_DZIELONA_08

Dec 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.60 KB | None | 0 0
  1. /*
  2.    Napisz program, który stworzy dwa spokrewnione procesy, które prze ś l ą mi ę dzy
  3.    sobą 10 komunikatów przez kolejkę komunikatów, a nast ę pnie stworzy dwa kolejne
  4.    procesy, które prze ś l ą te same komunikaty za pomocą pami ę ci dzielonej. W obu
  5.    przypadkach dokonaj pomiaru czasu przesy ł ania komunikatów i wy ś wietl otrzy-
  6.    mane wyniki. Do pomiaru czasu u ż yj funkcji time() (man 2 time).
  7.    */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/ipc.h>
  12. #include <sys/types.h>
  13. #include <sys/msg.h>
  14. #include <sys/shm.h>
  15. #include <sys/sem.h>
  16. #include <unistd.h>
  17. #include <wait.h>
  18. #include <time.h>
  19. #include <string.h>
  20.  
  21. const char *messages[] = {
  22.     "Pierwszy komunikat",
  23.     "Drugi komunikat",
  24.     "Trzeci komunikat",
  25.     "Czwarty komunikat",
  26.     "Piaty komunikat",
  27.     "Szosty komunikat",
  28.     "Siodmy komunikat",
  29.     "Osmy komunikat",
  30.     "Dziewiaty komunikat",
  31.     "Dziesiaty komunikat"
  32. };
  33.  
  34. #define n_messages (sizeof(messages) / sizeof(const char *))
  35. #define BUFFER_SIZE 64
  36.  
  37. void error_exit(const char *msg)
  38. {
  39.     perror(msg);
  40.     exit(EXIT_FAILURE);
  41. }
  42.  
  43. struct msgbuf {
  44.     long mtype;
  45.     char mtext[BUFFER_SIZE];
  46. };
  47.  
  48. void msg_send_message(int msgid, int i)
  49. {
  50.     struct msgbuf send_buffer;
  51.  
  52.     send_buffer.mtype = 1;
  53.     memset(send_buffer.mtext, '\0', BUFFER_SIZE-1);
  54.     strncpy(send_buffer.mtext, messages[i], BUFFER_SIZE-1);
  55.  
  56.     if(msgsnd(msgid, &send_buffer, strlen(send_buffer.mtext), 0) == -1)
  57.         error_exit("msgsnd");
  58. }
  59.  
  60. void msg_recv_message(int msgid)
  61. {
  62.     struct msgbuf recv_buffer;
  63.  
  64.     memset(recv_buffer.mtext, '\0', BUFFER_SIZE-1);
  65.     if(msgrcv(msgid, &recv_buffer, sizeof(recv_buffer.mtext), 1, 0) == -1)
  66.         error_exit("msgrcv");
  67.  
  68.     printf("%s\n", recv_buffer.mtext);
  69. }
  70.  
  71. void sem_up_and_wait(int semid)
  72. {
  73.     struct sembuf up_operation = {0, 1, 0},
  74.               wait_operation = {0, 0, 0};
  75.  
  76.     if(semop(semid, &up_operation, 1) == -1)
  77.         error_exit("semop_up");
  78.     if(semop(semid, &wait_operation, 1) == -1)
  79.         error_exit("semop_wait");
  80. }
  81.  
  82. void sem_down(int semid)
  83. {
  84.     struct sembuf down_operation = {0, -1, 0};
  85.     if(semop(semid, &down_operation, 1) == -1)
  86.         error_exit("semop_down");
  87. }
  88.  
  89. void shm_save_message(int shmid, int i)
  90. {
  91.     char *shared_memory;
  92.  
  93.     if((shared_memory = (char *)shmat(shmid, NULL, 0)) == (void *)-1)
  94.         error_exit("shmat_save");
  95.  
  96.     memset(shared_memory, '\0', BUFFER_SIZE);
  97.     strncpy(shared_memory, messages[i], strlen(messages[i]));
  98.  
  99.     if(shmdt(shared_memory) == -1)
  100.         error_exit("shmdt_save");
  101. }
  102.  
  103. void shm_load_message(int shmid)
  104. {
  105.     char *shared_memory;
  106.  
  107.     if((shared_memory = (char *)shmat(shmid, NULL, 0)) == (void *)-1)
  108.         error_exit("shmat_load");
  109.  
  110.     printf("%s\n", shared_memory);
  111.  
  112.     if(shmdt(shared_memory) == -1)
  113.         error_exit("shmdt_load");
  114. }
  115.  
  116. int main(void)
  117. {
  118.     pid_t pid, pid2;
  119.     int msgid;
  120.     int shmid;
  121.     int semid;
  122.     time_t start_msg, stop_msg;
  123.     time_t start_shm, stop_shm;
  124.  
  125.     ///////////////////////////
  126.  
  127.     start_msg = clock();
  128.     // Kolejka komunikatow
  129.     printf("KOLEJKA KOMUMINKATOW:\n");
  130.     if((msgid = msgget(IPC_PRIVATE, 0775 | IPC_CREAT)) == -1)
  131.         error_exit("msgget");
  132.  
  133.     pid = fork();
  134.     if(pid == -1)
  135.         error_exit("fork_msg");
  136.     else if(pid == 0){
  137.         for(int i = 0; i < 10; i++){
  138.             msg_send_message(msgid, i);
  139.             usleep(100000);
  140.         }
  141.         return EXIT_SUCCESS;
  142.     }
  143.     else{
  144.         for(int i = 0; i < 10; i++)
  145.             msg_recv_message(msgid);
  146.  
  147.         if(msgctl(msgid, IPC_RMID, NULL) == -1)
  148.             error_exit("msgctl");
  149.  
  150.     }
  151.     stop_msg = clock();
  152.  
  153.     wait(NULL);
  154.     usleep(500000);
  155.     printf("\n");
  156.  
  157.     start_shm = clock();
  158.     // Pamiec dzielona
  159.     printf("PAMIEC DZIELONA:\n");
  160.     if((shmid = shmget(IPC_PRIVATE, SHMLBA, 0775 | IPC_CREAT)) == -1)
  161.         error_exit("shmget");
  162.  
  163.     if((semid = semget(IPC_PRIVATE, 1, 0775 | IPC_CREAT)) == -1)
  164.         error_exit("semget");
  165.  
  166.     if(semctl(semid, 0, SETVAL, 1) == -1)
  167.         error_exit("semctl_setval");
  168.  
  169.     pid2 = fork();
  170.     if(pid2 == -1)
  171.         error_exit("fork_shm");
  172.     else if(pid2 == 0){
  173.         sem_up_and_wait(semid);
  174.         for(int i = 0; i < 10; i++){
  175.             shm_save_message(shmid, i);
  176.             usleep(100000);
  177.             sem_up_and_wait(semid);
  178.         }
  179.         return EXIT_SUCCESS;
  180.     }
  181.     else{
  182.         sem_down(semid);
  183.         for(int i = 0; i < 10; i++){   
  184.             sem_down(semid);
  185.             shm_load_message(shmid);
  186.         }
  187.         sem_down(semid);
  188.         wait(NULL);
  189.  
  190.         if(semctl(semid, 0, IPC_RMID, NULL) == -1)
  191.             error_exit("semctl_rmid");
  192.  
  193.         if(shmctl(shmid, IPC_RMID, NULL) == -1)
  194.             error_exit("shmctl");
  195.     }
  196.     stop_shm = clock();
  197.    
  198.     wait(NULL);
  199.     printf("\n");
  200.  
  201.     printf("Czas przesylania przez kolejke komunikatow: %lf\n",
  202.             (double)(stop_msg-start_msg)/CLOCKS_PER_SEC);
  203.     printf("Czas przesylania przez pamiec dzielona: %lf\n",
  204.             (double)(stop_shm-start_shm)/CLOCKS_PER_SEC);
  205.     return EXIT_SUCCESS;
  206. }
Add Comment
Please, Sign In to add comment