-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jacob Garby
committed
May 3, 2024
1 parent
5660fa7
commit c3f429f
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <mqueue.h> | ||
#include <signal.h> | ||
|
||
void cont_handler(int sig) { | ||
if (sig != SIGCONT) return; | ||
} | ||
|
||
int main() { | ||
mqd_t q = mq_open("/res_request", O_WRONLY); | ||
|
||
if (q == (mqd_t) -1) { | ||
fprintf(stderr, "Error making queue: %s\n", strerror(errno)); | ||
} | ||
|
||
const char *str = "Hello, world! This is my message."; | ||
mq_send(q, str, strlen(str), 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <unistd.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <signal.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <mqueue.h> | ||
|
||
#define BUFSZ 256 | ||
|
||
void cleanup(int sig) { | ||
mq_unlink("/res_request"); | ||
|
||
exit(sig < 0 ? EXIT_FAILURE : EXIT_SUCCESS); | ||
} | ||
|
||
int main() { | ||
struct mq_attr attr; | ||
mqd_t q; | ||
|
||
char buff[BUFSZ + 1]; | ||
ssize_t nread; | ||
|
||
attr.mq_maxmsg = 10; | ||
attr.mq_msgsize = BUFSZ; | ||
|
||
mq_unlink("/res_request"); | ||
q = mq_open("/res_request", O_RDONLY | O_CREAT | O_EXCL, 0644, &attr); | ||
|
||
if (q == (mqd_t) -1) { | ||
fprintf(stderr, "Error making queue (%d): %s\n", errno, strerror(errno)); | ||
cleanup(-1); | ||
} | ||
|
||
signal(SIGINT, cleanup); | ||
printf("Waiting.\n"); | ||
|
||
while ((nread = mq_receive(q, buff, BUFSZ, NULL)) != -1) { | ||
printf("Got %zd bytes: %s\n", nread, buff); | ||
sleep(3); | ||
} | ||
|
||
fprintf(stderr, "Error receiving: %s\n", strerror(errno)); | ||
exit(EXIT_FAILURE); | ||
} |