summaryrefslogtreecommitdiff
path: root/tools/forktest.c
blob: 49dc40e963fda8be13be8d0f12ebc7f0ee58a183 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

int main() {
    int fd = open("testfile", O_WRONLY| O_CREAT, 0644);
    if (fd < 0) {
        perror("open");
        return 1;
    }
    int flags = fcntl(fd, F_GETFL);
    printf("Parent: File access mode is O_RDWR|O_CREAT (%d %d %d)\n", flags, O_RDWR|O_CREAT, O_WRONLY|O_CREAT);

    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        return 1;
    } else if (pid == 0) { // Child process
        // Change file access mode
        if (fcntl(fd, F_SETFL, O_RDONLY) < 0) {
            perror("fcntl");
            return 1;
        }
        int flags = fcntl(fd, F_GETFL);
        printf("Child: Changed file access mode to O_RDONLY|.. (%d)\n", flags);
        _exit(0);
    } else { // Parent process
        sleep(2);
        int flags = fcntl(fd, F_GETFL);
        if (flags < 0) {
            perror("fcntl");
            return 1;
        }
        if (flags & O_RDONLY) {
            printf("Parent: File access mode changed to O_RDONLY|.. (%d)\n", flags);
        } else {
            printf("Parent: File access mode is still O_RDWR|.. (%d)\n", flags);
        }
    }

    close(fd);
    return 0;
}