--sas

message-queue-interchange

Nov 16th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. // prog1.c
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7.  
  8. #include <sys/ipc.h>
  9. #include <sys/msg.h>
  10. #include <sys/stat.h>
  11.  
  12. int main() {
  13.     int msqid;
  14.     char pathname[] = "prog1.c";
  15.     key_t  key;
  16.     int i, len, maxlen;
  17.  
  18.     struct mymsgbuf
  19.     {
  20.        long mtype;
  21.        struct info {
  22.           char mtext[81];
  23.           int count;
  24.        } a;
  25.     } mybuf;
  26.  
  27.     /* Create or attach message queue  */
  28.  
  29.     key = ftok(pathname, 0);
  30.  
  31.     (void) umask(0000);
  32.     if ((msqid = msgget(key, 0666 | IPC_CREAT)) < 0){
  33.        printf("Can\'t get msqid\n");
  34.        exit(-1);
  35.     }
  36.  
  37.     /* Send information */
  38.  
  39.     for (i = 1; i <= 5; i++) {
  40.        mybuf.mtype = 1;
  41.        strcpy(mybuf.a.mtext, "Hi, second. I'm first\n");
  42.        len = sizeof(struct info);
  43.  
  44.        if (msgsnd(msqid, (struct msgbuf*) &mybuf, len, 0) < 0) {
  45.          printf("Can\'t send message to queue\n");
  46.          msgctl(msqid, IPC_RMID, (struct msqid_ds *) NULL);
  47.          exit(-1);
  48.        }
  49.  
  50.        maxlen = len;
  51.  
  52.        if ((len = msgrcv(msqid, (struct msgbuf *) &mybuf, maxlen, 2, 0)) < 0) {
  53.          printf("Can\'t receive message from queue\n");
  54.          exit(-1);
  55.        }
  56.  
  57.        printf("I am first and I found string: %s", mybuf.a.mtext);
  58.     }
  59.  
  60.     return 0;
  61. }
  62.  
  63.  
  64.  
  65. // prog2.c
  66.  
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69. #include <unistd.h>
  70. #include <sys/types.h>
  71.  
  72. #include <sys/ipc.h>
  73. #include <sys/msg.h>
  74. #include <sys/stat.h>
  75.  
  76. int main() {
  77.     int msqid;
  78.     char pathname[] = "prog1.c";
  79.     key_t  key;
  80.     int i, len, maxlen;
  81.  
  82.     struct mymsgbuf
  83.     {
  84.        long mtype;
  85.        struct info {
  86.           char mtext[81];
  87.           int count;
  88.        } a;
  89.     } mybuf;
  90.  
  91.     /* Create or attach message queue  */
  92.  
  93.     key = ftok(pathname, 0);
  94.  
  95.     (void) umask(0000);
  96.     if ((msqid = msgget(key, 0666 | IPC_CREAT)) < 0){
  97.        printf("Can\'t get msqid\n");
  98.        exit(-1);
  99.     }
  100.  
  101.     /* Send information */
  102.  
  103.     for (i = 1; i <= 5; i++) {
  104.        mybuf.mtype = 2;
  105.        strcpy(mybuf.a.mtext, "Hi, first. I'm second\n");
  106.        len = sizeof(struct info);
  107.  
  108.        if (msgsnd(msqid, (struct msgbuf*) &mybuf, len, 0) < 0) {
  109.          printf("Can\'t send message to queue\n");
  110.          msgctl(msqid, IPC_RMID, (struct msqid_ds *) NULL);
  111.          exit(-1);
  112.        }
  113.  
  114.        maxlen = len;
  115.  
  116.        if ((len = msgrcv(msqid, (struct msgbuf *) &mybuf, maxlen, 1, 0)) < 0) {
  117.          printf("Can\'t receive message from queue\n");
  118.          exit(-1);
  119.        }
  120.  
  121.        printf("I am second and I found string: %s", mybuf.a.mtext);
  122.     }
  123.  
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment