1 #include <stdio.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <linux/sched.h>
5 #include <stdlib.h>
6 
7 int v, fd;
8 
child_proc()9 int child_proc()
10 {
11     v = 42;
12     close(fd);
13     exit(0);
14 }
15 
16 #define STACK_SIZE 1024
17 
main(int argc,char * argv[])18 int main(int argc, char *argv[])
19 {
20     void **child_stack;
21     char ch;
22 
23     v = 9;
24     fd = open(argv[0], O_RDONLY);
25     if (read(fd, &ch, 1) < 1) {
26         printf("Can't read file");
27         exit(1);
28     }
29     child_stack = (void **) malloc(STACK_SIZE * sizeof(void *));
30     printf("v = %d\n", v);
31 
32     clone(child_proc, child_stack + STACK_SIZE, CLONE_VM|CLONE_FILES, NULL);
33     sleep(1);
34 
35     printf("v = %d\n", v);
36     if (read(fd, &ch, 1) < 1) {
37         printf("Can't read file because it's closed by child.\n");
38         return 0;
39     } else {
40         printf("We shouldn't be able to read from file which is closed by child.\n");
41         return 0;
42     }
43 }
44 
45