diff options
| author | Paul Buetow <paul@buetow.org> | 2025-03-24 21:37:10 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-03-24 21:37:10 +0200 |
| commit | 6834a4c4094d133e771491b134e8da73a9f00a07 (patch) | |
| tree | 2c0206f643f7d59085f82014b8d22fd358dfbd23 /tools/forktest.c | |
| parent | eefcf3c73695d23897335590c74ee83b4ef4e75f (diff) | |
add mini fork test tool
Diffstat (limited to 'tools/forktest.c')
| -rw-r--r-- | tools/forktest.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/tools/forktest.c b/tools/forktest.c new file mode 100644 index 0000000..49dc40e --- /dev/null +++ b/tools/forktest.c @@ -0,0 +1,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; +} |
