Tobiahao

S01_LAB03_04a

Nov 14th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. /*
  2. Zmodyfikuj programy z zadania trzeciego tak, aby zbada ć jak si ę b ę dzie
  3. zachowywa ł a funkcja msgrcv(), w zale ż no ś ci od tego, czy otrzyma flag ę
  4. IPC_NOWAIT, czy nie.
  5. */
  6.  
  7. // Program pierwszy - wysylajacy 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. #include <errno.h>
  16.  
  17. #define MESSAGE_TO_SEND "Wiadomosc do wyslania"
  18. #define FTOK_KEY 712
  19. #define BUFFER_SIZE 128
  20.  
  21. struct msgbuf {
  22.     long mtype;
  23.     char mtext[BUFFER_SIZE];
  24. };
  25.  
  26. int main(void)
  27. {
  28.     int id;
  29.     key_t key;
  30.     struct msgbuf send_buffer;
  31.  
  32.     if((key = ftok("/tmp", FTOK_KEY)) == -1){
  33.         perror("ftok");
  34.         return EXIT_FAILURE;
  35.     }
  36.  
  37.     if((id = msgget(key, 0600 | IPC_CREAT)) == -1){
  38.         perror("msgget");
  39.         return EXIT_FAILURE;
  40.     }
  41.  
  42.     send_buffer.mtype = 1;
  43.     memset(send_buffer.mtext, '\0', BUFFER_SIZE);
  44.     strncpy(send_buffer.mtext, MESSAGE_TO_SEND, BUFFER_SIZE);
  45.    
  46.     for(int i = 0; i < 1000; i++){
  47.         if(msgsnd(id, &send_buffer, sizeof(send_buffer.mtext), IPC_NOWAIT) == -1){
  48.             if(errno == EAGAIN)
  49.                 printf("Blad dzieki ustawionej fladze IPC_NOWAIT: %s\n", strerror(errno));
  50.             else
  51.                 perror("msgsnd");
  52.             return EXIT_FAILURE;
  53.         }
  54.     }
  55.  
  56.     return EXIT_SUCCESS;
  57. }
Add Comment
Please, Sign In to add comment