Advertisement
Guest User

server.c

a guest
Mar 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <unistd.h>
  3. #include <sys/wait.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <mqueue.h>
  7. #include <stdint.h>
  8. #include <errno.h>
  9. #include <signal.h>
  10. #include <string.h>
  11.  
  12. #include "mqcommon.h"
  13. #include "common.h"
  14.  
  15. //#define FILE_MODE 0600
  16. #define MSG_LIMIT 20
  17. #define MAX_MSG 10
  18. #define ERR(source) (perror(source),\
  19. fprintf(stderr,"%s:%d\n",__FILE__,__LINE__),\
  20. exit(EXIT_FAILURE))
  21.  
  22. void sethandler( void (*f)(int), int sigNo) {
  23. struct sigaction act;
  24. memset(&act, 0, sizeof(struct sigaction));
  25. act.sa_handler = f;
  26. if (-1==sigaction(sigNo, &act, NULL)) ERR("sigaction");
  27. }
  28.  
  29. void sigint_handler(int sig)
  30. {
  31. /* Mq_close(_m);
  32. Mq_unlink(pid_m);
  33. Mq_close(_s);
  34. Mq_unlink(pid_s);*/
  35. }
  36.  
  37. mqd_t mq_create(char *name, int size, int n)
  38. {
  39. struct mq_attr attr;
  40. //remove existing queue
  41. Mq_unlink(name);
  42. attr.mq_maxmsg = n; // queue length
  43. attr.mq_msgsize = size; // message length
  44. // create the queue
  45. mqd_t mq_pout = Mq_open(name, O_RDWR | O_CREAT, FILE_MODE, &attr);
  46. printf("mq_open: %s\n", name);
  47. return mq_pout;
  48. }
  49.  
  50. void create_pin_h(mqd_t *mq_pin);
  51. void pin_handler(sigval_t sigval)
  52. {
  53. mqd_t *_s = (mqd_t *)sigval.sival_ptr;
  54. int ret;
  55. struct msg f_cl;
  56. ret = Mq_receive(*_s, (char *) &f_cl, sizeof(f_cl), NULL);
  57. char pid_c[20];
  58. ret = sprintf(pid_c, "/%d_c", f_cl.pid);
  59. if (ret < 0) ERR("sprintf");
  60. mqd_t s_cl;
  61. if((s_cl=TEMP_FAILURE_RETRY(mq_open(pid_c, O_WRONLY, FILE_MODE, NULL)))==(mqd_t)-1) ERR("mq open in");
  62. float res = f_cl.a - f_cl.b;
  63. ret = Mq_send(s_cl, (char *) &res, sizeof(res), 0);
  64. Mq_close(s_cl);
  65. }
  66.  
  67. void create_pin_h(mqd_t *mq_pin)
  68. {
  69. struct sigevent mq_h;
  70. mq_h.sigev_notify = SIGEV_THREAD;
  71. mq_h.sigev_notify_function = pin_handler;
  72. mq_h.sigev_notify_attributes = NULL;
  73. mq_h.sigev_value.sival_ptr = mq_pin;
  74. mq_notify(*mq_pin, &mq_h);
  75. }
  76.  
  77. void create_pin_m(mqd_t *mq_pin);
  78. void pin_handler_m(sigval_t sigval)
  79. {
  80. mqd_t *_s = (mqd_t *)sigval.sival_ptr;
  81. create_pin_m(_s);
  82. int ret;
  83. struct msg f_cl;
  84. ret = Mq_receive(*_s, (char *) &f_cl, sizeof(f_cl), NULL);
  85. char pid_c[20];
  86. ret = sprintf(pid_c, "/%d_c", f_cl.pid);
  87. if (ret < 0) ERR("sprintf");
  88. mqd_t s_cl;
  89. if((s_cl=TEMP_FAILURE_RETRY(mq_open(pid_c, O_WRONLY, FILE_MODE, NULL)))==(mqd_t)-1) ERR("mq open in");
  90. float res = f_cl.a*f_cl.b;
  91. ret = Mq_send(s_cl, (char *) &res, sizeof(res), 0);
  92. Mq_close(s_cl);
  93. }
  94.  
  95. void create_pin_m(mqd_t *mq_pin)
  96. {
  97. struct sigevent mq_h;
  98. mq_h.sigev_notify = SIGEV_THREAD;
  99. mq_h.sigev_notify_function = pin_handler_m;
  100. mq_h.sigev_notify_attributes = NULL;
  101. mq_h.sigev_value.sival_ptr = mq_pin;
  102. mq_notify(*mq_pin, &mq_h);
  103. }
  104.  
  105. void server(mqd_t _m, mqd_t _s)
  106. {
  107. create_pin_h(&_s);
  108. create_pin_m(&_m);
  109. sleep(3600);
  110. }
  111.  
  112. int main(int argc, char *argv[])
  113. {
  114. sethandler(sigint_handler,SIGINT);
  115. int ret;
  116. pid_t pid = getpid();
  117. char pid_m[20];
  118. ret = sprintf(pid_m, "/%d_m", pid);
  119. if (ret < 0) ERR("sprintf");
  120. mqd_t _m, _s;
  121. struct mq_attr attr;
  122. attr.mq_maxmsg=10;
  123. attr.mq_msgsize=sizeof(struct msg);
  124. if((_m=TEMP_FAILURE_RETRY(mq_open(pid_m, O_RDONLY | O_CREAT, FILE_MODE, &attr)))==(mqd_t)-1) ERR("mq open in");
  125. printf("open: %s\n", pid_m);
  126.  
  127. char pid_s[20];
  128. ret = sprintf(pid_s, "/%d_s", pid);
  129. if (ret < 0) ERR("sprintf");
  130. // mqd_t _s = mq_create(pid_s, sizeof(uint8_t), MSG_LIMIT);
  131. if((_s=TEMP_FAILURE_RETRY(mq_open(pid_s, O_RDONLY | O_CREAT, FILE_MODE, &attr)))==(mqd_t)-1) ERR("mq open out");
  132. printf("open: %s\n", pid_s);
  133.  
  134. server(_m, _s);
  135.  
  136. Mq_close(_m);
  137. Mq_unlink(pid_m);
  138. Mq_close(_s);
  139. Mq_unlink(pid_s);
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement