4 # Copyright (c) 2014 EMC Corp.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 # fcntl(2) locking scenario. No problems seen.
35 sed '1,/^EOF/d' < $here/$0 > fcntl.c
36 mycc -o fcntl -Wall -Wextra -O0 -g fcntl.c || exit 1
47 #include <sys/types.h>
61 static volatile sig_atomic_t completed;
62 const char name[] = "work";
66 ahandler(int s __unused)
73 handler(int s __unused)
79 add(int n, int increment)
86 pos = n * sizeof(val);
87 memset(&fl, 0, sizeof(fl));
89 fl.l_len = sizeof(val);
91 fl.l_whence = SEEK_SET;
93 while (fcntl(fd, F_SETLK, &fl) < 0) {
95 err(1, "F_SETLK (child)");
99 if (lseek(fd, pos, SEEK_SET) == -1)
102 while ((r = read(fd, &val, sizeof(val)) != sizeof(val))) {
103 if (r == -1 && errno != EAGAIN)
105 if (lseek(fd, pos, SEEK_SET) == -1)
109 val = val + increment;
111 fprintf(stderr, "add(%d, %d) @ pos %ld: %ld = %ld + %d\n",
112 n, increment, (long)pos, val, oval, increment);
114 if (lseek(fd, pos, SEEK_SET) == -1)
116 while ((r = write(fd, &val, sizeof(val)) != sizeof(val))) {
117 if (r == -1 && errno != EAGAIN)
119 if (lseek(fd, pos, SEEK_SET) == -1)
124 if (fcntl(fd, F_SETLK, &fl) < 0)
134 /* Need to re-open after a fork() */
136 if ((fd = open(name, O_RDWR)) == -1)
137 err(1, "open(%s)", name);
138 if ((flags = fcntl(fd, F_GETFL)) == -1)
139 err(1, "fcntl(%d, T_GETFL)", fd);
141 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
142 err(1, "fcntl(%d, T_SETFL, %d)", fd, flags);
144 for (i = 0; i < N; i++)
147 kill(getppid(), SIGHUP);
148 while (access("work", R_OK) == 0)
160 if ((fd = open(name, O_RDWR)) == -1)
161 err(1, "open(%s)", name);
162 if ((flags = fcntl(fd, F_GETFL)) == -1)
163 err(1, "fcntl(%d, T_GETFL)", fd);
165 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
166 err(1, "fcntl(%d, T_SETFL, %d)", fd, flags);
168 for (i = 0; i < N; i++)
171 kill(getppid(), SIGHUP);
172 while (access("work", R_OK) == 0)
185 signal(SIGHUP, handler);
186 signal(SIGALRM, ahandler);
188 if ((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
189 err(1, "open(%s)", name);
190 len = N * sizeof(val);
191 if (ftruncate(fd, len) == -1)
194 if ((flags = fcntl(fd, F_GETFL)) == -1)
195 err(1, "fcntl(%d, T_GETFL)", fd);
197 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
198 err(1, "fcntl(%d, T_SETFL, %d)", fd, flags);
200 for (i = 0; i < PARALLEL; i++) {
204 for (i = 0; i < PARALLEL; i++) {
209 while (completed != PARALLEL * 2)
212 if (lseek(fd, 0, SEEK_SET) == -1)
215 for (i = 0; i < N; i++) {
216 if (read(fd, &val, sizeof(val)) != sizeof(val))
217 err(1, "Final read");
219 fprintf(stderr, "index %d: %ld\n", i, val);
223 fprintf(stderr, "FAIL\n");
226 for (i = 0; i < PARALLEL; i++) {