Advertisement
djk77

recive

Apr 1st, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/msg.h>
  9.  
  10. #define PERMS 0644
  11. struct my_msgbuf {
  12. long mtype;
  13. char mtext[200];
  14. };
  15.  
  16. int main(void) {
  17. struct my_msgbuf buf;
  18. int msqid;
  19. int len;
  20. key_t key;
  21. system("touch msgq.txt");
  22.  
  23. if ((key = ftok("msgq.txt", 'B')) == -1) {
  24. perror("ftok");
  25. exit(1);
  26. }
  27.  
  28. if ((msqid = msgget(key, PERMS | IPC_CREAT)) == -1) {
  29. perror("msgget");
  30. exit(1);
  31. }
  32. printf("message queue: ready to send messages.\n");
  33. printf("Enter lines of text, ^D to quit:\n");
  34. buf.mtype = 1; /* we don't really care in this case */
  35.  
  36. while(fgets(buf.mtext, sizeof buf.mtext, stdin) != NULL) {
  37. len = strlen(buf.mtext);
  38. /* remove newline at end, if it exists */
  39. if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0';
  40. if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */
  41. perror("msgsnd");
  42. }
  43. strcpy(buf.mtext, "end");
  44. len = strlen(buf.mtext);
  45. if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */
  46. perror("msgsnd");
  47.  
  48. if (msgctl(msqid, IPC_RMID, NULL) == -1) {
  49. perror("msgctl");
  50. exit(1);
  51. }
  52. printf("message queue: done sending messages.\n");
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement