Skip to content

Commit

Permalink
added some c code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Garby committed May 3, 2024
1 parent 5660fa7 commit c3f429f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rewrite/client.c
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);
}
46 changes: 46 additions & 0 deletions rewrite/manager.c
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);
}

0 comments on commit c3f429f

Please sign in to comment.