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 FILE_MODE 0600
- #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 sigint_handler(int sig)
- {
- /* Mq_close(_m);
- Mq_unlink(pid_m);
- Mq_close(_s);
- Mq_unlink(pid_s);*/
- }
- mqd_t mq_create(char *name, int size, int n)
- {
- struct mq_attr attr;
- //remove existing queue
- Mq_unlink(name);
- attr.mq_maxmsg = n; // queue length
- attr.mq_msgsize = size; // message length
- // create the queue
- mqd_t mq_pout = Mq_open(name, O_RDWR | O_CREAT, FILE_MODE, &attr);
- printf("mq_open: %s\n", name);
- return mq_pout;
- }
- void create_pin_h(mqd_t *mq_pin);
- void pin_handler(sigval_t sigval)
- {
- mqd_t *_s = (mqd_t *)sigval.sival_ptr;
- int ret;
- struct msg f_cl;
- ret = Mq_receive(*_s, (char *) &f_cl, sizeof(f_cl), NULL);
- char pid_c[20];
- ret = sprintf(pid_c, "/%d_c", f_cl.pid);
- if (ret < 0) ERR("sprintf");
- mqd_t s_cl;
- if((s_cl=TEMP_FAILURE_RETRY(mq_open(pid_c, O_WRONLY, FILE_MODE, NULL)))==(mqd_t)-1) ERR("mq open in");
- float res = f_cl.a - f_cl.b;
- ret = Mq_send(s_cl, (char *) &res, sizeof(res), 0);
- Mq_close(s_cl);
- }
- void create_pin_h(mqd_t *mq_pin)
- {
- struct sigevent mq_h;
- mq_h.sigev_notify = SIGEV_THREAD;
- mq_h.sigev_notify_function = pin_handler;
- mq_h.sigev_notify_attributes = NULL;
- mq_h.sigev_value.sival_ptr = mq_pin;
- mq_notify(*mq_pin, &mq_h);
- }
- void create_pin_m(mqd_t *mq_pin);
- void pin_handler_m(sigval_t sigval)
- {
- mqd_t *_s = (mqd_t *)sigval.sival_ptr;
- create_pin_m(_s);
- int ret;
- struct msg f_cl;
- ret = Mq_receive(*_s, (char *) &f_cl, sizeof(f_cl), NULL);
- char pid_c[20];
- ret = sprintf(pid_c, "/%d_c", f_cl.pid);
- if (ret < 0) ERR("sprintf");
- mqd_t s_cl;
- if((s_cl=TEMP_FAILURE_RETRY(mq_open(pid_c, O_WRONLY, FILE_MODE, NULL)))==(mqd_t)-1) ERR("mq open in");
- float res = f_cl.a*f_cl.b;
- ret = Mq_send(s_cl, (char *) &res, sizeof(res), 0);
- Mq_close(s_cl);
- }
- void create_pin_m(mqd_t *mq_pin)
- {
- struct sigevent mq_h;
- mq_h.sigev_notify = SIGEV_THREAD;
- mq_h.sigev_notify_function = pin_handler_m;
- mq_h.sigev_notify_attributes = NULL;
- mq_h.sigev_value.sival_ptr = mq_pin;
- mq_notify(*mq_pin, &mq_h);
- }
- void server(mqd_t _m, mqd_t _s)
- {
- create_pin_h(&_s);
- create_pin_m(&_m);
- sleep(3600);
- }
- int main(int argc, char *argv[])
- {
- sethandler(sigint_handler,SIGINT);
- int ret;
- pid_t pid = getpid();
- char pid_m[20];
- ret = sprintf(pid_m, "/%d_m", pid);
- if (ret < 0) ERR("sprintf");
- mqd_t _m, _s;
- struct mq_attr attr;
- attr.mq_maxmsg=10;
- attr.mq_msgsize=sizeof(struct msg);
- if((_m=TEMP_FAILURE_RETRY(mq_open(pid_m, O_RDONLY | O_CREAT, FILE_MODE, &attr)))==(mqd_t)-1) ERR("mq open in");
- printf("open: %s\n", pid_m);
- char pid_s[20];
- ret = sprintf(pid_s, "/%d_s", pid);
- if (ret < 0) ERR("sprintf");
- // mqd_t _s = mq_create(pid_s, sizeof(uint8_t), MSG_LIMIT);
- if((_s=TEMP_FAILURE_RETRY(mq_open(pid_s, O_RDONLY | O_CREAT, FILE_MODE, &attr)))==(mqd_t)-1) ERR("mq open out");
- printf("open: %s\n", pid_s);
- server(_m, _s);
- Mq_close(_m);
- Mq_unlink(pid_m);
- Mq_close(_s);
- Mq_unlink(pid_s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement