if (res == -1) {
printf("Process %d — failed to lock region\n", getpid());
} else {
printf("Process %d — obtained lock, on region\n", getpid());
}
printf ("Process %d ending\n", getpid());
close(file_desc);
exit(EXIT_SUCCESS);
}
Если вы сначала запустите программу lock3 в фоновом режиме, далее сразу запускайте новую программу:
$ ./lock3 &
$ process 227 locking file
$ ./lock5
Вы получите следующий вывод:
Process 227 locking file
Process 228, trying F_RDLCK, region 10 to 15
Process 228 — obtained lock on region
Process 228, trying F_UNLCK, region 10 to 15
Process 228 — unlocked region
Process 228, trying F_UNLCK, region 0 to 50
Process 228 — unlocked region
Process 228, trying F_WRLCK, region 16 to 21
Process 228 — failed to lock on region
Process 228, trying F_RDLCK, region 4 0 to 50
Process 228 - failed to lock on region
Process 228, trying F_WRLCK with wait, region 16 to 21
Process 227 closing file
Process 228 — obtained lock on region
Process 228 ending
Как это работает