Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice09 / ex5-1.c
blob: 716a940e06e5c3c9282c5bd93bc93bb4ac471ee0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <error.h>
#include <stdio.h>
#include <unistd.h>
#include <mqueue.h>

#include "ex5.h"

int main(int argc, char* argv[]) {
	const struct mq_attr mqattr = {
		.mq_maxmsg = 10,
		.mq_msgsize = 64,
	};
	mqd_t mq = mq_open(queueName, O_WRONLY | O_CREAT, 0700, &mqattr);

	int fd = open(argv[1], O_RDONLY);
	char buf[mqattr.mq_msgsize];
	int amount;
	while ((amount = read(fd, buf, mqattr.mq_msgsize))) {
		if (amount < mqattr.mq_msgsize) buf[amount] = '\0'; 
		mq_send(mq, buf, mqattr.mq_msgsize, 0);
	}
	mq_send(mq, "", 1, 0);

	close(fd);
	mq_close(mq);
	mq_unlink(queueName);
	return 0;
}