7 Read_lock(fd, 0, SEEK_SET, 0); /* родительский процесс блокирует весь файл на чтение */
8 printf("%s: parent has read lock\n", Gf_time());
9 if (Fork() == 0) {
10 /* первый дочерний процесс */
11 sleep(1);
12 printf("%s: first child tries to obtain write lock\n", Gf_time());
13 Writew_lock(fd, 0, SEEK_SET, 0); /* здесь он будет заблокирован */
14 printf("%s: first child obtains write lock\n", Gf_time());
15 sleep(2);
16 Un_lock(fd, 0, SEEK_SET, 0);
17 printf("ls: first child releases write lock\n", Gf_time());
18 exit(0);
19 }
20 if (Fork() == 0) {
21 /* второй дочерний процесс */
22 sleep(3);
23 printf("%s: second child tries to obtain read lock\n", Gf_time());
24 Readw_lock(fd, 0, SEEK_SET, 0);
25 printf("%s: second child obtains read lock\n", Gf_time());
26 sleep(4);
27 Un_lock(fd, 0, SEEK_SET, 0);
28 printf("%s: second child releases read lock\n", Gf_time());
29 exit(0);
30 }
31 /* родительский процесс */
32 sleep(5);
33 Un_lock(fd, 0, SEEK_SET, 0);
34 printf("%s: parent releases read lock\n", Gf_time());
35 exit(0);
36 }