Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <mqueue.h>
- #include <errno.h>
- #include <fcntl.h>
- int main() {
- struct mq_attr attrs;
- attrs.mq_maxmsg = 10;
- attrs.mq_msgsize = sizeof(int);
- const char name[] = "/test-queue";
- mqd_t q = mq_open(name, O_CREAT | O_RDWR, 0600, &attrs);
- if (q == (mqd_t)-1) {
- perror("mq_open");
- exit(EXIT_FAILURE);
- }
- if (fork()) {
- int msg = 666;
- if (mq_send(q, (const char *)&msg, sizeof(msg), 1)) {
- perror("mq_send");
- exit(EXIT_FAILURE);
- }
- } else {
- int msg;
- unsigned priority;
- if (mq_receive(q, (char *)&msg, sizeof(msg), &priority) == -1) {
- perror("mq_receive");
- exit(EXIT_FAILURE);
- }
- printf("%d\n", msg);
- }
- mq_unlink(name);
- mq_close(q);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement