Tobiahao

S01_LAB03_03b

Nov 14th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. /*
  2. Napisz dwa programy (procesy niespokerwnione), które b ę d ą
  3. komunikowa ć si ę przy pomocy kolejki komunikatów. Do wygenerowania
  4. klucza u ż yj funkcji ftok().
  5. */
  6.  
  7. // Program drugi - odbierajacy wiadomosc
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12. #include <sys/ipc.h>
  13. #include <sys/msg.h>
  14. #include <string.h>
  15.  
  16. #define FTOK_KEY 712
  17. #define BUFFER_SIZE 128
  18.  
  19. struct msgbuf {
  20.     long mtype;
  21.     char mtext[BUFFER_SIZE];
  22. };
  23.  
  24. int main(void)
  25. {
  26.     int id;
  27.     key_t key;
  28.     struct msgbuf recv_buffer;
  29.  
  30.     if((key = ftok("/tmp", FTOK_KEY)) == -1){
  31.         perror("ftok");
  32.         return EXIT_FAILURE;
  33.     }
  34.  
  35.     if((id = msgget(key, 0600 | IPC_CREAT)) == -1){
  36.         perror("msgget");
  37.         return EXIT_FAILURE;
  38.     }
  39.  
  40.     recv_buffer.mtype = 1;
  41.     memset(recv_buffer.mtext, '\0', BUFFER_SIZE);
  42.  
  43.     if(msgrcv(id, &recv_buffer, sizeof(recv_buffer.mtext), 1, 0) == -1){
  44.         perror("msgrcv");
  45.         return EXIT_FAILURE;
  46.     }
  47.  
  48.     printf("Wiadomosc: %s\n", recv_buffer.mtext);
  49.  
  50.     if(msgctl(id, IPC_RMID, 0) == -1){
  51.         perror("msgctl");
  52.         return EXIT_FAILURE;
  53.     }
  54.  
  55.     return EXIT_SUCCESS;
  56. }
Add Comment
Please, Sign In to add comment