-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipesem.c
More file actions
111 lines (91 loc) · 1.87 KB
/
pipesem.c
File metadata and controls
111 lines (91 loc) · 1.87 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Binary semaphore using named pipes.
* reserve, release cond reserve.
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <signal.h>
int release(int fd) {
char buf[100];
// Drain the pipe first.
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1)
return -1;
while (read(fd, &buf, 100) != -1)
;
if (fcntl(fd, F_SETFL, 0) == -1)
return -1;
return write(fd, &buf, 1);
}
int reserve(int fd) {
char buf;
if (fcntl(fd, F_SETFL, 0) == -1)
return -1;
return read(fd, &buf, 1);
}
int reserveCond(int fd) {
char buf;
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1)
return -1;
return read(fd, &buf, 1);
}
// Test it out
static void handler(int sig) {
}
int main(int argc, char **argv) {
int fd;
struct sigaction sigact;
// Make a fifo to use.
mkfifo("/tmp/test_sem", S_IRWXU);
if ((fd = open("/tmp/test_sem", O_RDWR)) == -1) {
perror("open");
exit(EXIT_FAILURE);
}
// Setup a timeout
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigact.sa_handler = handler;
sigaction(SIGALRM, &sigact, NULL);
alarm(1);
// Reserve it. Should block.
puts("reserve");
if (reserve(fd) == -1) {
puts("timed out");
}
alarm(0);
// Release.
puts("Release");
release(fd);
puts("Release");
release(fd);
puts("Release");
release(fd);
switch (fork()) {
case -1:
perror("fork");
exit(EXIT_FAILURE);
case 0:
puts("[Child] Reserving ..");
reserve(fd);
puts("[Child] Doing some work..");
sleep(2);
puts("[Child] Releasing...");
release(fd);
exit(EXIT_SUCCESS);
default:
puts("[Parent] Doing some work...");
sleep(1);
puts("[Parent] Reserving...");
if (reserveCond(fd) == -1)
puts("[Parent] Sem already reserved.. reserve anyway.");
reserve(fd);
puts("[Parent] Has resource!");
release(fd);
}
//unlink("/tmp/test_sem");
puts("");
exit(EXIT_SUCCESS);
}