do_fork使用的flags

来源:互联网 发布:剑三万花萝莉捏脸数据 编辑:程序博客网 时间:2024/04/28 06:59
1. fork, vfork and clone三者最终都会调用do_fork函数,三者的差别就是参数上的不同而已。
fork的实现:
     do_fork(CLONE_SIGCHLD,...)
clone的实现:
     do_fork(CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGCHLD,...)
vfork的实现:
     do_fork(CLONE_VFORK|CLONE_VM|CLONE_SIGCHLD,...)
2. Linux使用copy on wirte的技术,Linux中的fork代价仅仅是创建子进程的页表结构和创建一个task_struct结构。
3. 为了优化那些:fork然后就是exec的程序,Linux提供了vfork。vfork时,父进程会被阻塞 ,直到子进程调用了exec或exit,因为此时不复制页表结构。
4. clone()系统调用是fork()的推广形式,它允许新进程共享父进程的存储空间、文
   件描述符和信号处理程序

补充:通常Linux是让子进程优先执行的!下面是do_fork使用的flags
41 #define CSIGNAL         0x000000ff      /* signal mask to be sent at exit */
42 #define CLONE_VM 0x00000100 /* set if VM shared between processes */
43 #define CLONE_FS 0x00000200 /* set if fs info shared between processes */
44 #define CLONE_FILES 0x00000400 /* set if open files shared between processes */
45 #define CLONE_SIGHAND 0x00000800 /* set if signal handlers and blocked signals shared */
46 #define CLONE_PTRACE 0x00002000 /* set if we want to let tracing continue on the child too */
47 #define CLONE_VFORK 0x00004000 /* set if the parent wants the child to wake it up on mm_release */
48 #define CLONE_PARENT 0x00008000 /* set if we want to have the same parent as the cloner */
49 #define CLONE_THREAD 0x00010000 /* Same thread group? */
50 #define CLONE_NEWNS 0x00020000 /* New namespace group? */
51 #define CLONE_SYSVSEM 0x00040000 /* share system V SEM_UNDO semantics */
52 #define CLONE_SETTLS 0x00080000 /* create a new TLS for the child */
53 #define CLONE_PARENT_SETTID 0x00100000 /* set the TID in the parent */
54 #define CLONE_CHILD_CLEARTID 0x00200000 /* clear the TID in the child */
55 #define CLONE_DETACHED 0x00400000 /* Unused, ignored */
56 #define CLONE_UNTRACED 0x00800000 /* set if the tracing process can't force CLONE_PTRACE on this clone */
57 #define CLONE_CHILD_SETTID 0x01000000 /* set the TID in the child */
58 #define CLONE_STOPPED 0x02000000 /* Start in stopped state */