|
| 1 | +#include <fcntl.h> |
| 2 | +#include <signal.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <sys/file.h> |
| 8 | +#include <sys/wait.h> |
| 9 | +#include <sys/types.h> |
| 10 | +#include <sys/stat.h> |
| 11 | + |
| 12 | +#define EXIT_FAILURE 1 |
| 13 | + |
| 14 | +// Compilation: |
| 15 | +// gcc "02 - Flock.c" -o flockc |
| 16 | + |
| 17 | +// This program imitates work of 3 processes (parent and 2 childs) with some file with flock-blocking, which means |
| 18 | +// that 2 processes cannot both write into file, but can both read file in the same time. However it cannot write and read in the same time. |
| 19 | + |
| 20 | +int lock_write() |
| 21 | +{ |
| 22 | + int fd; |
| 23 | + if ((fd = open("/tmp/file.txt", O_RDWR | O_CREAT, 00700)) == -1) |
| 24 | + { |
| 25 | + printf("Access to READ&WRITE error.\n"); |
| 26 | + return 0; |
| 27 | + } |
| 28 | + |
| 29 | + flock(fd, LOCK_EX | LOCK_NB); |
| 30 | + |
| 31 | + const char buf[] = "ABRACADABRA"; |
| 32 | + write(fd, &buf, strlen(buf) + 1); |
| 33 | + |
| 34 | + printf("Write imitation 2 seconds...\n"); |
| 35 | + sleep(2); |
| 36 | + |
| 37 | + close(fd); |
| 38 | + flock(fd, LOCK_UN); |
| 39 | + |
| 40 | + return 1; |
| 41 | +} |
| 42 | + |
| 43 | +int lock_read(pid_t pid) |
| 44 | +{ |
| 45 | + int fd; |
| 46 | + if ((fd = open("/tmp/file.txt", O_RDONLY, 0)) == -1) |
| 47 | + { |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + flock(fd, LOCK_SH | LOCK_NB); |
| 52 | + |
| 53 | + char buf[12]; |
| 54 | + read(fd, &buf, 12); |
| 55 | + |
| 56 | + printf("Process %d read the file: %s\n", pid, buf); |
| 57 | + |
| 58 | + close(fd); |
| 59 | + flock(fd, LOCK_UN); |
| 60 | + |
| 61 | + return 1; |
| 62 | +} |
| 63 | + |
| 64 | +void main() |
| 65 | +{ |
| 66 | + pid_t main_pid = getpid(); |
| 67 | + |
| 68 | + printf("Start of process %d\n", main_pid); |
| 69 | + |
| 70 | + // Lock file for writing. |
| 71 | + if (lock_write()) |
| 72 | + { |
| 73 | + printf("Write finished, file unblocked.\n"); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + perror("Error of calling lock_write"); |
| 78 | + exit(EXIT_FAILURE); |
| 79 | + } |
| 80 | + |
| 81 | + // Parent makes child 1. |
| 82 | + pid_t child_pid; |
| 83 | + if ((child_pid = fork()) > 0) |
| 84 | + { |
| 85 | + printf("Start of child process %d.\n", child_pid); |
| 86 | + |
| 87 | + // Parent makes child 2. |
| 88 | + pid_t next_child_pid; |
| 89 | + if ((next_child_pid = fork()) > 0) |
| 90 | + { |
| 91 | + printf("Start of child process %d.\n", next_child_pid); |
| 92 | + |
| 93 | + printf("Parent process %d sleeps 5 seconds.\n", getpid()); |
| 94 | + sleep(5); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + printf("Process %d tries to access the file...\n", getpid()); |
| 99 | + |
| 100 | + int canread = 0; |
| 101 | + while(!canread) |
| 102 | + { |
| 103 | + canread = lock_read(getpid()); |
| 104 | + } |
| 105 | + |
| 106 | + printf("End of process %d\n", getpid()); |
| 107 | + |
| 108 | + exit(0); |
| 109 | +} |
0 commit comments