Advertisement
Kacper_Michalak

Listing_3

Nov 11th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <sys/types.h>
  5. #include <string.h>
  6.  
  7. struct msgbuf {
  8. long type;
  9. char mtext[50];
  10. };
  11.  
  12. void send_message(int mqid, const char *message)
  13. {
  14. struct msgbuf buffer;
  15. buffer.type = 1; // Ustawia typ komunikatu na 1
  16. strncpy(buffer.mtext, message, sizeof(buffer.mtext));
  17.  
  18. if (msgsnd(mqid, &buffer, sizeof(buffer.mtext), 0) < 0)
  19. perror("msgsnd");
  20. else
  21. printf("Wysłano komunikat: %s\n", buffer.mtext);
  22. }
  23.  
  24. int main(void)
  25. {
  26. int key = ftok("/tmp", 8);
  27. if (key < 0)
  28. perror("ftok");
  29.  
  30. int id = msgget(key, 0600 | IPC_CREAT | IPC_EXCL);
  31. if (id < 0)
  32. perror("msgget");
  33.  
  34. send_message(id, "Hello, message!");
  35.  
  36. if (msgctl(id, IPC_RMID, 0) < 0)
  37. perror("msgctl");
  38.  
  39. return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement