fork()函数得到的子进程会继承父进程的哪些资源

mmzhzjia 2013-08-21 02:05:07
fork()函数得到的子进程会继承父进程的哪些资源
...全文
2781 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-08-22
  • 打赏
  • 举报
回复
引用 12 楼 qq120848369 的回复:
man 3p fork
FORK Section: POSIX Programmer's Manual (P) Updated: 2003 -------------------------------------------------------------------------------- NAME fork - create a new process SYNOPSIS #include <unistd.h> pid_t fork(void); DESCRIPTION The fork() function shall create a new process. The new process (child process) shall be an exact copy of the calling process (parent process) except as detailed below: * The child process shall have a unique process ID. * The child process ID also shall not match any active process group ID. * The child process shall have a different parent process ID, which shall be the process ID of the calling process. * The child process shall have its own copy of the parent's file descriptors. Each of the child's file descriptors shall refer to the same open file description with the corresponding file descriptor of the parent. * The child process shall have its own copy of the parent's open directory streams. Each open directory stream in the child process may share directory stream positioning with the corresponding directory stream of the parent. * The child process shall have its own copy of the parent's message catalog descriptors. * The child process' values of tms_utime, tms_stime, tms_cutime, and tms_cstime shall be set to 0. * The time left until an alarm clock signal shall be reset to zero, and the alarm, if any, shall be canceled; see alarm() . * All semadj values shall be cleared. * File locks set by the parent process shall not be inherited by the child process. * The set of signals pending for the child process shall be initialized to the empty set. * Interval timers shall be reset in the child process. * Any semaphores that are open in the parent process shall also be open in the child process. * The child process shall not inherit any address space memory locks established by the parent process via calls to mlockall() or mlock(). * Memory mappings created in the parent shall be retained in the child process. MAP_PRIVATE mappings inherited from the parent shall also be MAP_PRIVATE mappings in the child, and any modifications to the data in these mappings made by the parent prior to calling fork() shall be visible to the child. Any modifications to the data in MAP_PRIVATE mappings made by the parent after fork() returns shall be visible only to the parent. Modifications to the data in MAP_PRIVATE mappings made by the child shall be visible only to the child. * For the SCHED_FIFO and SCHED_RR scheduling policies, the child process shall inherit the policy and priority settings of the parent process during a fork() function. For other scheduling policies, the policy and priority settings on fork() are implementation-defined. * Per-process timers created by the parent shall not be inherited by the child process. * The child process shall have its own copy of the message queue descriptors of the parent. Each of the message descriptors of the child shall refer to the same open message queue description as the corresponding message descriptor of the parent. * No asynchronous input or asynchronous output operations shall be inherited by the child process. * A process shall be created with a single thread. If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources. Consequently, to avoid errors, the child process may only execute async-signal-safe operations until such time as one of the exec functions is called. Fork handlers may be established by means of the pthread_atfork() function in order to maintain application invariants across fork() calls. When the application calls fork() from a signal handler and any of the fork handlers registered by pthread_atfork() calls a function that is not asynch-signal-safe, the behavior is undefined. * If the Trace option and the Trace Inherit option are both supported: If the calling process was being traced in a trace stream that had its inheritance policy set to POSIX_TRACE_INHERITED, the child process shall be traced into that trace stream, and the child process shall inherit the parent's mapping of trace event names to trace event type identifiers. If the trace stream in which the calling process was being traced had its inheritance policy set to POSIX_TRACE_CLOSE_FOR_CHILD, the child process shall not be traced into that trace stream. The inheritance policy is set by a call to the posix_trace_attr_setinherited() function. * If the Trace option is supported, but the Trace Inherit option is not supported: The child process shall not be traced into any of the trace streams of its parent process. * If the Trace option is supported, the child process of a trace controller process shall not control the trace streams controlled by its parent process. * The initial value of the CPU-time clock of the child process shall be set to zero. * The initial value of the CPU-time clock of the single thread of the child process shall be set to zero. All other process characteristics defined by IEEE Std 1003.1-2001 shall be the same in the parent and child processes. The inheritance of process characteristics not defined by IEEE Std 1003.1-2001 is unspecified by IEEE Std 1003.1-2001. After fork(), both the parent and the child processes shall be capable of executing independently before either one terminates.
一根烂笔头 2013-08-22
  • 打赏
  • 举报
回复

FORK(2)                                                       Linux Programmer's Manual                                                      FORK(2)



NAME
       fork - create a child process

SYNOPSIS
       #include <unistd.h>

       pid_t fork(void);

DESCRIPTION
       fork()  creates  a  new  process by duplicating the calling process.  The new process, referred to as the child, is an exact duplicate of the
       calling process, referred to as the parent, except for the following points:

       *  The child has its own unique process ID, and this PID does not match the ID of any existing process group (setpgid(2)).

       *  The child's parent process ID is the same as the parent's process ID.

       *  The child does not inherit its parent's memory locks (mlock(2), mlockall(2)).

       *  Process resource utilizations (getrusage(2)) and CPU time counters (times(2)) are reset to zero in the child.

       *  The child's set of pending signals is initially empty (sigpending(2)).

       *  The child does not inherit semaphore adjustments from its parent (semop(2)).

       *  The child does not inherit record locks from its parent (fcntl(2)).

       *  The child does not inherit timers from its parent (setitimer(2), alarm(2), timer_create(2)).

       *  The child does not inherit outstanding asynchronous I/O operations from its parent (aio_read(3), aio_write(3)), nor does  it  inherit  any
          asynchronous I/O contexts from its parent (see io_setup(2)).

       The process attributes in the preceding list are all specified in POSIX.1-2001.  The parent and child also differ with respect to the follow‐
       ing Linux-specific process attributes:

       *  The child does not inherit directory change notifications (dnotify) from its parent (see the description of F_NOTIFY in fcntl(2)).

       *  The prctl(2) PR_SET_PDEATHSIG setting is reset so that the child does not receive a signal when its parent terminates.

       *  The default timer slack value is set to the parent's current timer slack value.  See the description of PR_SET_TIMERSLACK in prctl(2).

       *  Memory mappings that have been marked with the madvise(2) MADV_DONTFORK flag are not inherited across a fork().

       *  The termination signal of the child is always SIGCHLD (see clone(2)).

       Note the following further points:

       *  The child process is created with a single thread—the one that called fork().  The entire virtual address space of the  parent  is  repli‐
          cated  in the child, including the states of mutexes, condition variables, and other pthreads objects; the use of pthread_atfork(3) may be
          helpful for dealing with problems that this can cause.

       *  The child inherits copies of the parent's set of open file descriptors.  Each file descriptor in the child refers to the  same  open  file
          description  (see open(2)) as the corresponding file descriptor in the parent.  This means that the two descriptors share open file status
          flags, current file offset, and signal-driven I/O attributes (see the description of F_SETOWN and F_SETSIG in fcntl(2)).

       *  The child inherits copies of the parent's set of open message queue descriptors (see mq_overview(7)).  Each descriptor in the child refers
          to  the  same open message queue description as the corresponding descriptor in the parent.  This means that the two descriptors share the
          same flags (mq_flags).

       *  The child inherits copies of the parent's set of open directory streams (see opendir(3)).  POSIX.1-2001 says that the corresponding direc‐
          tory streams in the parent and child may share the directory stream positioning; on Linux/glibc they do not.

RETURN VALUE
       On  success,  the PID of the child process is returned in the parent, and 0 is returned in the child.  On failure, -1 is returned in the par‐
       ent, no child process is created, and errno is set appropriately.

ERRORS
       EAGAIN fork() cannot allocate sufficient memory to copy the parent's page tables and allocate a task structure for the child.

       EAGAIN It was not possible to create a new process because the caller's RLIMIT_NPROC resource limit was encountered.  To exceed  this  limit,
              the process must have either the CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capability.

       ENOMEM fork() failed to allocate the necessary kernel structures because memory is tight.

       ENOSYS fork() is not supported on this platform (for example, hardware without a Memory-Management Unit).

CONFORMING TO
       SVr4, 4.3BSD, POSIX.1-2001.

NOTES
       Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory required to duplicate
       the parent's page tables, and to create a unique task structure for the child.

       Since version 2.3.3, rather than invoking the kernel's fork() system call, the glibc fork() wrapper that is provided  as  part  of  the  NPTL
       threading  implementation  invokes  clone(2)  with  flags  that provide the same effect as the traditional system call.  (A call to fork() is
       equivalent to a call to clone(2) specifying flags as just SIGCHLD.)  The glibc wrapper invokes any fork handlers that have  been  established
       using pthread_atfork(3).

EXAMPLE
       See pipe(2) and wait(2).

SEE ALSO
       clone(2), execve(2), setrlimit(2), unshare(2), vfork(2), wait(2), daemon(3), capabilities(7), credentials(7)

COLOPHON
       This page is part of release 3.44 of the Linux man-pages project.  A description of the project, and information about reporting bugs, can be
       found at http://www.kernel.org/doc/man-pages/.



Linux                                                                2012-10-27                                                              FORK(2)
qq120848369 2013-08-21
  • 打赏
  • 举报
回复
man 3p fork
一根烂笔头 2013-08-21
  • 打赏
  • 举报
回复
次奥,打错两个关键字:完整,减少
一根烂笔头 2013-08-21
  • 打赏
  • 举报
回复
父进程一个完成副本,然后作为子进程执行,为了加少一些开销,Linux采用了写时复制技术!
  • 打赏
  • 举报
回复
引用 6 楼 mmzhzjia 的回复:
言简意赅 谢谢 如果我需要父子进程共同操作继承文件描述符(用户空间),用互斥锁事是信号量是不是能得到正确的应用呢?
继承父进程的互斥锁? 那样就不行了,互斥锁也拷贝了一份。。
mmzhzjia 2013-08-21
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
环境变量 ……
搞不明白什么意思!
赵4老师 2013-08-21
  • 打赏
  • 举报
回复
环境变量 ……
mmzhzjia 2013-08-21
  • 打赏
  • 举报
回复
言简意赅 谢谢 如果我需要父子进程共同操作继承文件描述符(用户空间),用互斥锁事是信号量是不是能得到正确的应用呢?
大漠孤鸿 2013-08-21
  • 打赏
  • 举报
回复
fork后父子进程只共享代码段,其它父进程资源会得到继承。 这样的继承会产生很多问题:比如fork之前曾打开过某个文件,fork后父子进程的文件描述符(用户空间)会指向相同的文件表项(内核空间),导致父(子)进程对文件的操作影响到子(父)进程。
mmzhzjia 2013-08-21
  • 打赏
  • 举报
回复
fork是将父进程复制一份出来 父子进程之间不共享资源
mmzhzjia 2013-08-21
  • 打赏
  • 举报
回复
在其他地方看到了这样的解答 下面的说法对吗 子进程是父进程的副本,它将获得父进程数据空间、堆、栈等资源的副本。注意,子进程持有的是上述存储空间的“副本”,这意味着父子进程间不共享这些存储空间,它们之间共享的存储空间只有代码段。
mmzhzjia 2013-08-21
  • 打赏
  • 举报
回复
为什么是几乎呢 有什么没有继承下来吗
max_min_ 2013-08-21
  • 打赏
  • 举报
回复
没记错的话,是继承父进程的几乎全部资源吧! 拷贝一份父进程资源独立使用

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧