C Clone wait to exit problem
hello
im trying to create a new thread using clone function ... everything looks ok but it's not working !!
Code:
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#define THREAD_MEMORY_SIZE 1024
int
child_func(void * data) {
static const char msg[] = "Child process is called\n";
write(0, msg, sizeof(msg) - 1);
exit(0);
}
int
main() {
int pid;
void * memory;
static const char ERROR_MEMORY_ALLOC_FAILED[] = "ERROR-> Memory allocation failed\n";
static const char ERROR_CLONE_FAILED[] = "ERROR-> Unable to create a new child process\n";
if((memory = malloc(THREAD_MEMORY_SIZE)) == NULL) {
write(0, ERROR_MEMORY_ALLOC_FAILED, sizeof(ERROR_MEMORY_ALLOC_FAILED) - 1);
exit(1);
}
if((pid = clone(child_func, (memory + THREAD_MEMORY_SIZE),
CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_PARENT | CLONE_THREAD | CLONE_IO, NULL)) < 0) {
free(memory);
write(0, ERROR_CLONE_FAILED, sizeof(ERROR_CLONE_FAILED) - 1);
exit(1);
}
waitpid(pid, NULL, __WCLONE);
free(memory);
exit(0);
}
here i created a thread and the thread function (child_func) Must be called and it Must write "Child process is called " message but nothing happen !!
what is the problem !!! ? i think there is a problem in (waitpid) section !!! i think even by calling waitpid, the parent is not wait for child to exit or (execute) !