X-Git-Url: https://git.piment-noir.org/?p=TD_SE.git;a=blobdiff_plain;f=lockf%2Flockf.c;fp=lockf%2Flockf.c;h=f684b17b45029a3607a77f7a37d93d0811d11feb;hp=0000000000000000000000000000000000000000;hb=d71fd5f57f0aa66e9b19916626fea98b0b3bfc57;hpb=7fffbcc8e55a361a1098dc914c2460af69991760 diff --git a/lockf/lockf.c b/lockf/lockf.c new file mode 100644 index 0000000..f684b17 --- /dev/null +++ b/lockf/lockf.c @@ -0,0 +1,39 @@ +#include +#include +#include + +int main() { + int i, fd = 1; + + if (fork()) //il s'agit du père + { + lseek(fd, 0, 0); + if (lockf(fd, F_LOCK, 1) < 0) { + write(fd, "pere lockf failed\n", 18); + return -1; + } + for (i = 0; i < 5; i++) { + write(fd, "pere ecrit\n", 12); + sleep(1); + } + write(fd, "pere va liberer le verrou\n", 26); + lseek(fd, 0, 0); + lockf(fd, F_ULOCK, 0); + wait(NULL); + } else { //il s'agit du fils + lseek(fd, 0, 0); //verrouillé l'octet 0 + if (lockf(fd, F_LOCK, 1) < 0) { + write(fd, "fils lockf failed\n", 18); + return -1; + } + for (i = 0; i < 4; i++) { + write(fd, "fils ecrit\n", 12); + sleep(1); + } + write(fd, "fils va liberer le verrou\n", 26); + lseek(fd, 0, 0); + lockf(fd, F_ULOCK, 0); + } + close(fd); + return 0; +}