创建进程准备
linux创建进程/线程通过用户空间的fork()、vfork()、__clone()函数实现,这些函数会通过传递不同的参数给调用系统调用clone(),然后clone()再调用do_fork()去实现创建进程/线程的主要部分。
fork与vfork都以写时拷贝技术(cow)创建进程/线程,并且内核会优先让子进程投入运行。vfork不会复制页表项,并且vfork会让子进程信号通知才唤醒父进程,然后从do_fork函数返回。
创建进程与线程不同在于是否共享一些资源,包括进程地址空间、文件系统资源、文件描述符资源、信号处理函数、命名空间等。
clone参数标志定义在/include/linux/sched.h中,如下:
// 向下滚动/** cloning flags:*/#define CSIGNAL 0x000000ff /* signal mask to be sent at exit */#define CLONE_VM 0x00000100 /* set if VM shared between processes */#define CLONE_FS 0x00000200 /* set if fs info shared between processes */#define CLONE_FILES 0x00000400 /* set if open files shared between processes */#define CLONE_SIGHAND 0x00000800 /* set if signal handlers and blocked signals shared */#define CLONE_PTRACE 0x00002000 /* set if we want to let tracing continue on the child too */#define CLONE_VFORK 0x00004000 /* set if the parent wants the child to wake it up on mm_release */#define CLONE_PARENT 0x00008000 /* set if we want to have the same parent as the cloner */#define CLONE_THREAD 0x00010000 /* Same thread group? */#define CLONE_NEWNS 0x00020000 /* New namespace group? */#define CLONE_SYSVSEM 0x00040000 /* share system V SEM_UNDO semantics */#define CLONE_SETTLS 0x00080000 /* create a new TLS for the child */#define CLONE_PARENT_SETTID 0x00100000 /* set the TID in the parent */#define CLONE_CHILD_CLEARTID 0x00200000 /* clear the TID in the child */#define CLONE_DETACHED 0x00400000 /* Unused, ignored */#define CLONE_UNTRACED 0x00800000 /* set if the tracing process can't force CLONE_PTRACE on this clone */#define CLONE_CHILD_SETTID 0x01000000 /* set the TID in the child */#define CLONE_STOPPED 0x02000000 /* Start in stopped state */#define CLONE_NEWUTS 0x04000000 /* New utsname group? */#define CLONE_NEWIPC 0x08000000 /* New ipcs */#define CLONE_NEWUSER 0x10000000 /* New user namespace */#define CLONE_NEWPID 0x20000000 /* New pid namespace */#define CLONE_NEWNET 0x40000000 /* New network namespace */#define CLONE_IO 0x80000000 /* Clone io context */
signal信号量定义在各个体系结构下的/arch/x/include/asm/signal.h文件中:
// 向下滚动#define SIGHUP 1#define SIGINT 2#define SIGQUIT 3#define SIGILL 4#define SIGTRAP 5#define SIGABRT 6#define SIGIOT 6#define SIGBUS 7#define SIGFPE 8#define SIGKILL 9#define SIGUSR1 10#define SIGSEGV 11#define SIGUSR2 12#define SIGPIPE 13#define SIGALRM 14#define SIGTERM 15#define SIGSTKFLT 16#define SIGCHLD 17 // 标志子进程#define SIGCONT 18#define SIGSTOP 19#define SIGTSTP 20#define SIGTTIN 21#define SIGTTOU 22#define SIGURG 23#define SIGXCPU 24#define SIGXFSZ 25#define SIGVTALRM 26#define SIGPROF 27#define SIGWINCH 28#define SIGIO 29#define SIGPOLL SIGIO/*#define SIGLOST 29*/#define SIGPWR 30#define SIGSYS 31#define SIGUNUSED 31/* These should not be considered constants from userland. */#define SIGRTMIN 32#define SIGRTMAX _NSIG
fork()创建进程的参数如下:
clone(SIGCHLD, 0); // 发送创建子进程信号
vfork()创建进程时调用clone传递的参数如下:
// 传递vfork标志,父进程准备睡眠,等待子进程发送信号唤醒// 父子共享地址空间clone(CLONE_VFORK | CLONE_VM | SIGCHLD, 0);
创建线程时调用clone传递的参数如下:
// 共享地址空间,共享文件系统,共享文件描述符,共享信号处理程序clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0)
