Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <unistd.h>
- #include <sys/wait.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <mqueue.h>
- #include <stdint.h>
- #include <errno.h>
- #include <signal.h>
- #include <string.h>
- #include "mqcommon.h"
- #include "common.h"
- #define MSG_LIMIT 20
- #define MAX_MSG 10
- #define ERR(source) (perror(source),\
- fprintf(stderr,"%s:%d\n",__FILE__,__LINE__),\
- exit(EXIT_FAILURE))
- void sethandler( void (*f)(int), int sigNo) {
- struct sigaction act;
- memset(&act, 0, sizeof(struct sigaction));
- act.sa_handler = f;
- if (-1==sigaction(sigNo, &act, NULL)) ERR("sigaction");
- }
- void client(mqd_t _c, char *arg)
- {
- int ret;
- mqd_t s_sv;
- if((s_sv=TEMP_FAILURE_RETRY(mq_open(arg, O_WRONLY, FILE_MODE, NULL)))==(mqd_t)-1) ERR("mq open in");
- struct msg to_sv;
- to_sv.pid = getpid();
- scanf("%f %f", &to_sv.a, &to_sv.b);
- ret = Mq_send(s_sv, (char *) &to_sv, sizeof(to_sv), 0);
- float res;
- ret = Mq_receive(_c, (char *) &res, sizeof(res), NULL);
- printf("%f\n", res);
- }
- int main(int argc, char *argv[])
- {
- int ret;
- pid_t pid = getpid();
- char pid_c[20];
- ret = sprintf(pid_c, "/%d_c", pid);
- if (ret < 0) ERR("sprintf");
- mqd_t _c;
- struct mq_attr attr;
- attr.mq_maxmsg=MAX_MSG;
- attr.mq_msgsize=sizeof(float);
- if((_c=TEMP_FAILURE_RETRY(mq_open(pid_c, O_RDONLY| O_CREAT, FILE_MODE, &attr)))==(mqd_t)-1) ERR("mq open in");
- printf("open: %s\n", pid_c);
- client(_c, argv[1]);
- Mq_close(_c);
- Mq_unlink(pid_c);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement