CLONE_FS Example
This post we will write a code, which will give us an observation what changes when CLONE_FS is added and when it is removed.
Code:
Output:
Notes:
1. You can see when CLONE_FS is set, changing the working directory in the child process has changed in the parent process.
2. Similarly other attributes like root directory will also change if CLONE_FS is set.
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <stdio.h> | |
#include <sched.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
#define STACK_SIZE 65536 | |
static int child_func(void *arg) | |
{ | |
printf("Child:Current Working Directory:%s\n", | |
get_current_dir_name()); | |
chdir("/opt"); | |
printf("Child:Current Working Directory:%s\n", | |
get_current_dir_name()); | |
return 0; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
//Allocate stack for child task | |
char *stack = malloc(STACK_SIZE); | |
unsigned long flags = 0; | |
char buf[256]; | |
int status; | |
printf("Parent:Current Working Directory:%s\n", | |
get_current_dir_name()); | |
if (!stack) { | |
perror("Failed to allocate memory\n"); | |
exit(1); | |
} | |
if (argc == 2 && !strcmp(argv[1], "fs")) | |
flags |= CLONE_FS; | |
if (clone(child_func, stack + STACK_SIZE, flags | SIGCHLD, NULL) == -1) { | |
perror("clone"); | |
exit(1); | |
} | |
if (wait(&status) == -1) { | |
perror("Wait"); | |
exit(1); | |
} | |
printf("Child exited with status:%d\t cwd:%s\n", | |
status, get_current_dir_name()); | |
return 0; | |
} |
Notes:
1. You can see when CLONE_FS is set, changing the working directory in the child process has changed in the parent process.
2. Similarly other attributes like root directory will also change if CLONE_FS is set.
Comments
Post a Comment