djk77

recivepravi

Apr 1st, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/msg.h>
  7.  
  8. #define PERMS 0644
  9. struct my_msgbuf {
  10. long mtype;
  11. char mtext[200];
  12. };
  13.  
  14. int main(void) {
  15. struct my_msgbuf buf;
  16. int msqid;
  17. int toend;
  18. key_t key;
  19.  
  20. if ((key = ftok("msgq.txt", 'B')) == -1) {
  21. perror("ftok");
  22. exit(1);
  23. }
  24.  
  25. if ((msqid = msgget(key, PERMS)) == -1) { /* connect to the queue */
  26. perror("msgget");
  27. exit(1);
  28. }
  29. printf("message queue: ready to receive messages.\n");
  30.  
  31. for(;;) { /* normally receiving never ends but just to make conclusion
  32. /* this program ends wuth string of end */
  33. if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) {
  34. perror("msgrcv");
  35. exit(1);
  36. }
  37. printf("recvd: \"%s\"\n", buf.mtext);
  38. toend = strcmp(buf.mtext,"end");
  39. if (toend == 0)
  40. break;
  41. }
  42. printf("message queue: done receiving messages.\n");
  43. system("rm msgq.txt");
  44. return 0;
  45. }
Add Comment
Please, Sign In to add comment