Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice13 / ex2.c
blob: 029fcfa1e892683cdfb8f8117aef85ea9f58fd79 (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
29
30
31
32
33
34
35
36
#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

int main() {
	struct mq_attr mqattr = {
		.mq_maxmsg = 1,
		.mq_msgsize = 1,
	};
	mqd_t mq = mq_open("/mq", O_RDWR | O_CREAT, 0755, &mqattr);

	int fpid = fork();
	if (fpid == 0) {
		char tmp[1];
		while (1) {
			mq_receive(mq, tmp, 1, NULL);
			printf("child\n");
		}
	}
	else {
		while (1) {
			printf("parent\n");
			mq_send(mq, "", 1, 0);
			sleep(1);
		}
	}

	mq_close(mq);
	if (fpid != 0)
		mq_unlink("/mq");

	return 0;
}