#define _GNU_SOURCE
#include <sched.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int child_func(void *arg) {
char *buf = (char *)arg;
printf("Child sees buf = \"%s\"\n", buf);
strcpy(buf, "CHILD_DATA");
return 0;
}
int main(int argc, char **argv) {
const int STACK_SIZE = 65536;
char *stack = malloc(STACK_SIZE);
if (!stack) { perror("malloc"); exit(1); }
unsigned long flags = SIGCHLD; // базовый флаг для ожидания
// Если передан аргумент "vm", добавляем флаг CLONE_VM
if (argc > 1 && !strcmp(argv[1], "vm")) {
flags |= CLONE_VM;
}
char buffer[100];
strcpy(buffer, "PARENT_DATA");
printf("Parent before clone: buffer = \"%s\"\n", buffer);
if (clone(child_func, stack + STACK_SIZE, flags, buffer) == -1) {
perror("clone");
exit(1);
}
int status;
if (wait(&status) == -1) {
perror("wait");
exit(1);
}
printf("Parent after child exit: buffer = \"%s\"\n", buffer);
return 0;
}