Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. * prod.c - the program reads lines of text from the standard input
  2.  * and sends them to the POSIX message queue, specified with
  3.  * the first parameter. Optional second parameter can specify
  4.  * the queue length (default: 1), and the third: sleep time before
  5.  * each message was sent.
  6.  * Example call:
  7.  *  ./prod /mq 5
  8.  */#include "mqcommon.h"
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     char buf[25], *mq_name;
  13.     int msgnr = 0, quelen = 0, timeout=0;
  14.     mqd_t mqdes;
  15.     struct mq_attr attr;
  16.  
  17.     if (argc < 2 || argc > 4)
  18.         err_quit("usage: prod MQ_NAME [quelen [ timeout]]\n\n");
  19.     mq_name = argv[1];
  20.     if (argc > 2)
  21.         quelen = atoi(argv[2]);
  22.     if (quelen < 1)
  23.         quelen = 1;
  24.     if (argc > 3)
  25.         timeout = atoi(argv[3]);
  26.     //remove existing queue (we don't care if it succeeds)
  27.     Mq_unlink(mq_name);
  28.     attr.mq_maxmsg = quelen;    // queue length
  29.     attr.mq_msgsize = sizeof(buf);  // message length
  30.     // create the queue
  31.     mqdes = Mq_open(mq_name, O_RDWR | O_CREAT, FILE_MODE, &attr);
  32.     fprintf(stderr, "Message queue %s created, PID=%ld\n", mq_name,
  33.         (long)getpid());
  34.     Mq_getattr(mqdes, &attr);
  35.     fprintf(stderr, "maxmsg=%ld, msgsize%ld, in queue: %ld\n",
  36.         attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
  37.     while (fgets(buf, sizeof(buf), stdin)) {
  38.         char *ptr;
  39.         int pri = msgnr % 3;
  40.         ptr = strchr(buf, '\n');
  41.         if (ptr)
  42.             *ptr = '\0';
  43.         else
  44.             buf[sizeof(buf) - 1] = '\0';
  45.         if (timeout>0) sleep(timeout);
  46.         if (Mq_send(mqdes, buf, strlen(buf) + 1, pri))
  47.             break;
  48.         msgnr++;
  49.         printf("Prod: sent msg nr  %d, pri=%d\n", msgnr, pri);
  50.     }
  51.     // close the producer access to the queue
  52.     Mq_close(mqdes);
  53.  
  54.     printf("Prod: exiting, msgnr=%d\n", msgnr);
  55.     exit(0);
  56. }
  57. /* cons.c - the program reads messages from the POSIX message queue,
  58.  * specified with the first parameter. Optional parameters can specify
  59.  * - delay after reading a message (before an attempt to read again),
  60.  * - timeout -i.e. the maximum waiting time for a meassage.
  61.  * Example call:
  62.  *  ./cons /mq 1 20
  63.  */
  64. #include "mqcommon.h"
  65. #include <signal.h>
  66. #include <stdlib.h>
  67. #include <time.h>
  68. #include <unistd.h>
  69. static struct sigaction sa;
  70. void alrm(int sig)
  71. {
  72.     return;
  73. }
  74.  
  75. int main(int argc, char *argv[])
  76. {
  77.     char buf[25], *mq_name;
  78.     int msgnr = 0, sleep_time = 0, timeout = 0;
  79.     mqd_t mqdes;
  80.     struct mq_attr attr;
  81.     struct timespec req_sleep_time, rem;
  82.     unsigned int pri;
  83.  
  84.     if (argc < 2 || argc > 4)
  85.         err_quit("usage: cons MQ_NAME [sleep_time [timeout]]\n\n");
  86.     mq_name = argv[1];
  87.     if (argc >= 3)
  88.         sleep_time = atoi(argv[2]);
  89.     req_sleep_time.tv_sec = sleep_time;
  90.     req_sleep_time.tv_nsec = 0;
  91.     if (argc >= 4)
  92.         timeout = atoi(argv[3]);
  93.  
  94.     mqdes = Mq_open_short(mq_name);
  95.     sa.sa_handler = alrm;
  96.     sigaction(SIGALRM, &sa, NULL);
  97.     Mq_getattr(mqdes, &attr);
  98.     fprintf(stderr, "maxmsg=%ld, msgsize=%ld, in queue: %ld\n",
  99.         attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
  100.     if (attr.mq_msgsize > sizeof(buf)) {
  101.         fprintf(stderr, "Buffer is too short\n");
  102.         exit(1);
  103.     }
  104.     while (1) {
  105.         int ret;
  106.         if (timeout > 0)
  107.             alarm(timeout);
  108.         if ((ret = Mq_receive(mqdes, buf, sizeof(buf), &pri)) < 0)
  109.             break;
  110.         alarm(0);
  111.         msgnr++;
  112.         printf("Cons: received msg nr %d, pri=%d, ret=%d\n", msgnr, pri,
  113.                ret);
  114.         buf[sizeof(buf) - 1] = '\0';
  115.         puts(buf);  //write(1,buf,attr.mq_msgsize);
  116.         if (sleep_time > 0) {
  117.             while (nanosleep(&req_sleep_time, &rem) == -1
  118.                    && errno == EINTR) {
  119.                 req_sleep_time.tv_sec = rem.tv_sec;
  120.                 req_sleep_time.tv_nsec = rem.tv_nsec;
  121.             }
  122.         }
  123.     }
  124.  
  125.     Mq_close(mqdes);
  126.  
  127.     printf("Cons: exiting, msgnr=%d\n", msgnr);
  128.     exit(0);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement