12 void chd_lock_unlock(int);
15 int main(int argc, char **argv)
21 fprintf(stderr, "\nUSAGE: flock_test filepath\n");
24 strncpy(fname, argv[1], 1023);
26 fd = open(fname, O_RDWR|O_CREAT, (mode_t)0666);
28 fprintf(stderr, "flock_test: failed to open %s : ", fname);
32 if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
33 fprintf(stderr, "flock_test: parent attempt to lock %s failed : ", \
41 fprintf(stderr, "flock_test: fork failed : ");
49 if ((cfd = open(fname, O_RDWR)) == -1) {
50 fprintf(stderr, "flock_test child (%d) cannot open %s: ", \
55 if(flock(cfd, LOCK_EX | LOCK_NB) != -1) {
56 fprintf(stderr, "flock_test child (%d): %s not yet locked : ", \
60 if(flock(fd, LOCK_UN) == -1) {
61 fprintf(stderr, "flock_test child (%d): cannot unlock %s: ", \
66 if(flock(cfd, LOCK_EX | LOCK_NB) == -1 ) {
68 "flock_test: child (%d) cannot re-lock %s after unlocking : ", \
80 if (WIFEXITED(rc) && WEXITSTATUS(rc) != 0) {
81 fprintf(stderr, "flock_test: child (%d) exit code = %d\n", \
82 pid, WEXITSTATUS(rc));