-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Description
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid > 0) { /* parent */
for (i = 0; i < NLOOPS; i += 2) {
sleep(1);
printf("parent: before lock\n");
writew_lock(fd, 0, SEEK_SET, 1);
printf("parent: got lock\n");
if ((counter = update((long *)area)) != i)
err_quit("parent: expected %d, got %d", i, counter);
un_lock(fd, 0, SEEK_SET, 1);
printf("parent: unlock\n");
}
} else { /* child */
for (i = 1; i < NLOOPS + 1; i += 2) {
sleep(1);
printf("child: before lock\n");
writew_lock(fd, 0, SEEK_SET, 1);
printf("child: got lock\n");
if ((counter = update((long *)area)) != i)
err_quit("child: expected %d, got %d", i, counter);
un_lock(fd, 0, SEEK_SET, 1);
printf("child: unlock\n");
}
}
After parent un_lock(fd, 0, SEEK_SET, 1); , parent itself can still have a change to acquire the lock again (althrough there's a sleep(1) here, but it just can't make sure child always go next).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels