Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/ipc.h>
  5. #include <sys/msg.h>
  6.  
  7. struct mymsg {
  8. long int mtype;
  9. char mtext[15];
  10. }buff;
  11.  
  12. int main() {
  13.  
  14. key_t key = ftok( ".", 'z');
  15. int id = msgget(key, IPC_CREAT | 0600);
  16. int r;
  17. buff.mtype = 2;
  18. strcpy(buff.mtext, "bylo");
  19. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  20.  
  21. buff.mtype = 3;
  22. strcpy(buff.mtext, "smaszno");
  23. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  24.  
  25. buff.mtype = 7;
  26. strcpy(buff.mtext, "a");
  27. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  28.  
  29. buff.mtype = 5;
  30. strcpy(buff.mtext, "jaszmije");
  31. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  32.  
  33. buff.mtype = 6;
  34. strcpy(buff.mtext, "smukwijne");
  35. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  36.  
  37. buff.mtype = 3;
  38. strcpy(buff.mtext, "swidrokretnie");
  39. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  40.  
  41. buff.mtype = 1;
  42. strcpy(buff.mtext, "na");
  43. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  44.  
  45. buff.mtype = 4;
  46. strcpy(buff.mtext, "zegwniku");
  47. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  48.  
  49. buff.mtype = 2;
  50. strcpy(buff.mtext, "wezaly");
  51. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  52.  
  53. buff.mtype = 5;
  54. strcpy(buff.mtext, "peliczaple");
  55. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  56.  
  57. buff.mtype = 8;
  58. strcpy(buff.mtext, "staly");
  59. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  60.  
  61. buff.mtype = 2;
  62. strcpy(buff.mtext, "smutochlijne");
  63. msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
  64.  
  65. buff.mtext[5]=0;
  66. // Każdy wypisywany komunikat będzie się składał z max. 5 bajtów
  67. // Jeżeli jakiś komunikat został już odczytany, to jest on USUWANY z kolejki.
  68. msgrcv(id, (struct msgbuf*)&buff, 5, 3, IPC_NOWAIT | MSG_NOERROR);
  69. puts(buff.mtext);
  70. /* zegwn
  71. Jeżeli msgtyp ma wartość większą od 0 (w tym przypadku ma wartość 4),
  72. to odczytuje pierwszy komunikat wskazanego typu, czyli typu 4.
  73. */
  74. for(;;) {
  75. r = msgrcv(id, (struct msgbuf*)&buff, 5, -4, IPC_NOWAIT | MSG_NOERROR);
  76. if(r < 0)
  77. break;
  78. puts(buff.mtext);
  79. /*
  80. na
  81. bylo
  82. wezal
  83. smuto
  84. smasz
  85. swidr
  86. Jeżeli msgtyp ma wartość mniejszą od 0, to z kolejki zostaną odczytane
  87. komunikaty o numerach typów nie większych niż wartość bezwględna msgtyp.
  88. W tym przypadku mamy -4, a więc czytane są wszystkie komunikaty o numerach
  89. mniejszych lub równych 4, chyba ża jakieś zostały już odczytane.
  90. */
  91. }
  92. msgrcv(id, (struct msgbuf*)&buff, 5, 0, IPC_NOWAIT | MSG_NOERROR);
  93. puts(buff.mtext);
  94. /*
  95. a
  96.  
  97. Jeżeli msgtyp ma wartość równą 0, to odczytywany jest pierwszy dostępny komunikat
  98. w kolejce. Może to być najdawniej wysłany, ale jeśli został usunięty, to czytany jest
  99. następny w kolejce komunikat.
  100. */
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement