#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sched.h>
#include <string.h>
#include <sys/wait.h>
#define STACK_SIZE (1024 * 1024)
int second_function(char *str, int depth) {
printf("%sn", str);
if (depth < 10) {
char new_str[20] = "hello world";
return second_function(new_str, depth + 1);
} else {
return 0;
}
}
int first_function(void *arg) {
char *str = "hello world";
return second_function(str, 1);
}
int main() {
char *stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (stack == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
pid_t pid = clone(first_function, stack + STACK_SIZE, CLONE_VM | CLONE_FILES | CLONE_FS | CLONE_SIGHAND | CLONE_THREAD | CLONE_PARENT_SETTID, NULL, NULL, NULL, NULL);
if (pid == -1) {
perror("clone");
exit(EXIT_FAILURE);
}
int status;
if (waitpid(pid, &status, 0) == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
return 0;
}
Почему
waitpid
возращает такую ошибку?
waitpid: No child processes