Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <mqueue.h>
  6. #include <sys/stat.h>
  7. #include <sys/wait.h>
  8.  
  9. #define MESSAGE_COUNT 5
  10. #define TEXT_SIZE 50
  11.  
  12. char text_message[TEXT_SIZE];
  13.  
  14. struct message {
  15. char mtext[TEXT_SIZE];
  16. };
  17.  
  18. struct mq_attr message_attr = {
  19. .mq_maxmsg = MESSAGE_COUNT,
  20. .mq_msgsize = sizeof(struct message)
  21. };
  22.  
  23. const char *queue_name = "/gotowe";
  24.  
  25. int main(int argc, char *argv[]){
  26. printf("start %d\n");
  27.  
  28. mqd_t mqid = mq_open(queue_name, O_CREAT | O_RDWR, 0666, &message_attr);
  29.  
  30. if (mqid == (mqd_t) -1) {
  31. perror("mq_open"); exit(1);
  32. }
  33.  
  34. pid_t child_a, child_b;
  35.  
  36. child_a = fork();
  37.  
  38. if (child_a == 0) {
  39. printf("proces wpisujacy priorytet 1\n");
  40. int i;
  41. for (i=0; i<MESSAGE_COUNT; i++) {
  42. sprintf(text_message, "Message id = %d with priority %d", i, 1);
  43. int result = mq_send(mqid, text_message, strlen(text_message) + 1, 1);
  44. if (result == -1) {
  45. perror("mq_send");
  46. }
  47. sleep(1);
  48. }
  49. } else {
  50. child_b = fork();
  51.  
  52. if (child_b == 0) {
  53. printf("proces wpisujacy priorytet 2\n");
  54. int i;
  55. for (i=0; i<MESSAGE_COUNT; i++) {
  56. sprintf(text_message, "Message id = %d with priority %d", i+10, 2);
  57. int result = mq_send(mqid, text_message, strlen(text_message) + 1, 2);
  58. if (result == -1) {
  59. perror("mq_send");
  60. }
  61. sleep(1);
  62. }
  63.  
  64. } else {
  65. sleep(2);
  66. printf("proces wypisujacy\n");
  67. do {
  68. unsigned int priority;
  69. struct message msg;
  70. int len = mq_receive(mqid, (char *) &msg,sizeof(msg), &priority);
  71. if (len == -1) {
  72. perror("mq_receive");
  73. break;
  74. }
  75. printf("Otrzymalem wiadomosc (priorytet = %d): '%s'\n", priority, msg.mtext);
  76. int r = mq_getattr(mqid, &message_attr);
  77. if (r == -1) {
  78. perror("mq_getattr");
  79. break;
  80. }
  81. sleep(1);
  82. } while (message_attr.mq_curmsgs);
  83. }
  84. }
  85.  
  86. /*
  87.  
  88. //tworzenie
  89. int i;
  90. for (i=0; i<MESSAGE_COUNT; i++) {
  91. printf("Obrot numer: %d ", i);
  92. sprintf(text_message, "Message id = %d with priority %d", i, 1);
  93. int result = mq_send(mqid, text_message, strlen(text_message) + 1, 1);
  94. if (result == -1) {
  95. perror("mq_send");
  96. }
  97. printf("stop\n");
  98. sleep(1);
  99. }
  100.  
  101.  
  102. //czytanie
  103. do {
  104. unsigned int priority;
  105. struct message msg;
  106. int len = mq_receive(mqid, (char *) &msg,sizeof(msg), &priority);
  107. if (len == -1) {
  108. perror("mq_receive");
  109. break;
  110. }
  111. printf("Otrzymalem wiadomosc (priorytet = %d): '%s'\n", priority, msg.mtext);
  112. int r = mq_getattr(mqid, &message_attr);
  113. if (r == -1) {
  114. perror("mq_getattr");
  115. break;
  116. }
  117. } while (message_attr.mq_curmsgs);
  118. */
  119. mq_close(mqid);
  120.  
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement