Advertisement
Guest User

client.c

a guest
Mar 23rd, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 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 MSG_LIMIT 20
  16. #define MAX_MSG 10
  17. #define ERR(source) (perror(source),\
  18. fprintf(stderr,"%s:%d\n",__FILE__,__LINE__),\
  19. exit(EXIT_FAILURE))
  20.  
  21. void sethandler( void (*f)(int), int sigNo) {
  22. struct sigaction act;
  23. memset(&act, 0, sizeof(struct sigaction));
  24. act.sa_handler = f;
  25. if (-1==sigaction(sigNo, &act, NULL)) ERR("sigaction");
  26. }
  27.  
  28. void client(mqd_t _c, char *arg)
  29. {
  30. int ret;
  31. mqd_t s_sv;
  32. if((s_sv=TEMP_FAILURE_RETRY(mq_open(arg, O_WRONLY, FILE_MODE, NULL)))==(mqd_t)-1) ERR("mq open in");
  33. struct msg to_sv;
  34. to_sv.pid = getpid();
  35. scanf("%f %f", &to_sv.a, &to_sv.b);
  36. ret = Mq_send(s_sv, (char *) &to_sv, sizeof(to_sv), 0);
  37. float res;
  38. ret = Mq_receive(_c, (char *) &res, sizeof(res), NULL);
  39. printf("%f\n", res);
  40. }
  41.  
  42. int main(int argc, char *argv[])
  43. {
  44. int ret;
  45. pid_t pid = getpid();
  46. char pid_c[20];
  47. ret = sprintf(pid_c, "/%d_c", pid);
  48. if (ret < 0) ERR("sprintf");
  49. mqd_t _c;
  50. struct mq_attr attr;
  51. attr.mq_maxmsg=MAX_MSG;
  52. attr.mq_msgsize=sizeof(float);
  53. if((_c=TEMP_FAILURE_RETRY(mq_open(pid_c, O_RDONLY| O_CREAT, FILE_MODE, &attr)))==(mqd_t)-1) ERR("mq open in");
  54. printf("open: %s\n", pid_c);
  55. client(_c, argv[1]);
  56. Mq_close(_c);
  57. Mq_unlink(pid_c);
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement