现在的位置: 首页 > 综合 > 正文

clone

2014年01月11日 ⁄ 综合 ⁄ 共 1231字 ⁄ 字号 评论关闭

The stack argument to clone is a simple
void*. Also, the stack grows down on most architectures, so the stack pointer should point to the
end of the allocated memory.

In any case, you shouldn't use clone directly unless you really need to avoid any dependencies on external libraries. Use the pthreads library instead. It provides a much easier
(and safer) API

#include <stdio.h>
#include <sched.h>
#include <stdlib.h>

int data = 10;

int child_process()
{
 printf("Child process %d, data %d\n",getpid(),data);
 data = 20;
 printf("Child process %d, data %d\n",getpid(),data);
 //while(1);
}

int main(int argc, char* argv[])
{
 void **child_stack;
 child_stack = (void **) malloc(1024);
 clone(child_process, child_stack, CLONE_THREAD, NULL);

 sleep(1);
 printf("Parent process %d, data %d\n",getpid(), data);
 //while(1);
exit(0);
}

CLONE_PARENT  创建的子进程的父进程是调用者的父进程,新进程与创建它的进程成了“兄弟”而不是“父子”

 CLONE_FS          子进程与父进程共享相同的文件系统,包括root、当前目录、umask

 CLONE_FILES     子进程与父进程共享相同的文件描述符(file descriptor)表

 CLONE_NEWNS  在新的namespace启动子进程,namespace描述了进程的文件hierarchy

 CLONE_SIGHAND  子进程与父进程共享相同的信号处理(signal handler)表

 CLONE_PTRACE  若父进程被trace,子进程也被trace

 CLONE_VFORK    父进程被挂起,直至子进程释放虚拟内存资源

 CLONE_VM          子进程与父进程运行于相同的内存空间

 CLONE_PID         子进程在创建时PID与父进程一致

 CLONE_THREAD   Linux 2.4中增加以支持POSIX线程标准,子进程与父进程共享相同的线程群

Program received signal SIGSEGV, Segmentation fault.????

抱歉!评论已关闭.