I am trying to use the clone () script to create a thread that shares resources with the parent process. I read in a book that if I use the following flags, I can do this: CLONE_VM | CLONE_FILES | CLONE_SIGHAND | CLONE_FS
But it seems that the variables are not shared.
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <linux/sched.h> #include <string.h> #define STACK_SIZE 65536 #define BUFSIZE 200 int n = 5; int Child(void *); int main() { pid_t pid; char *stack; stack = malloc(STACK_SIZE); pid = clone(Child,stack + STACK_SIZE, CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES); wait(NULL); char buf[BUFSIZE]; sprintf(buf,"Back to parent: Value of n: %d\n",n); write(1,buf,strlen(buf)); return 0; } int Child(void *args) { n += 15; char buf[BUFSIZE]; sprintf(buf,"In child: Value of n: %d\n",n); write(1,buf,strlen(buf)); }
The output continues to change. I'm confused.
source share