Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice11 / ex2.c
blob: 94ca590bc03b30a8df8e7ae9202d05b347d536d9 (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
#include <unistd.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>

int main() {
	sem_t* sem = sem_open("/sem", O_CREAT, 0755, 0);

	int fod = fork();
	if (fod == 0) {
		while (1) {
			sem_wait(sem);
			printf("world\n");
		}
	}
	else {
		while (1) {
			printf("hello\n");
			sem_post(sem);
			sleep(1);
		}
	}

	sem_close(sem);
	if (fod != 0)
		sem_unlink("/sem");

	return 0;
}