fork 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. .TH FORK 2
  2. .SH NAME
  3. fork, rfork \- manipulate process resources
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .PP
  9. .nf
  10. .B
  11. int fork(void)
  12. .PP
  13. .B
  14. int rfork(int flags)
  15. .fi
  16. .SH DESCRIPTION
  17. Forking is the only way new processes are created.
  18. The
  19. .I flags
  20. argument to
  21. .I rfork
  22. selects which resources of the
  23. invoking process (parent) are shared
  24. by the new process (child) or initialized to
  25. their default values.
  26. The resources include
  27. the file name space,
  28. the open file descriptor table (which, when shared, permits processes
  29. to open and close files for other processes),
  30. the set of environment variables
  31. (see
  32. .IR env (3)),
  33. the note group
  34. (the set of processes that receive notes written to a member's
  35. .B notepg
  36. file;
  37. see
  38. .IR proc (3)),
  39. the set of rendezvous tags
  40. (see
  41. .IR rendezvous (2));
  42. and open files.
  43. .I Flags
  44. is the logical OR of some subset of
  45. .TF RFCNAMEG
  46. .TP
  47. .B RFPROC
  48. If set a new process is created; otherwise changes affect the
  49. current process.
  50. .TP
  51. .B RFNOWAIT
  52. If set, the child process will be dissociated from the parent. Upon
  53. exit the child will leave no
  54. .B Waitmsg
  55. (see
  56. .IR wait (2))
  57. for the parent to collect.
  58. .TP
  59. .B RFNAMEG
  60. If set, the new process inherits a copy of the parent's name space;
  61. otherwise the new process shares the parent's name space.
  62. Is mutually exclusive with
  63. .BR RFCNAMEG .
  64. .TP
  65. .B RFCNAMEG
  66. If set, the new process starts with a clean name space. A new
  67. name space must be built from a mount of an open file descriptor.
  68. Is mutually exclusive with
  69. .BR RFNAMEG .
  70. .TP
  71. .B RFNOMNT
  72. If set, subsequent mounts into the new name space and dereferencing
  73. of pathnames starting with
  74. .B #
  75. are disallowed.
  76. .TP
  77. .B RFENVG
  78. If set, the environment variables are copied;
  79. otherwise the two processes share environment variables.
  80. Is mutually exclusive with
  81. .BR RFCENVG .
  82. .TP
  83. .B RFCENVG
  84. If set, the new process starts with an empty environment.
  85. Is mutually exclusive with
  86. .BR RFENVG .
  87. .TP
  88. .B RFNOTEG
  89. Each process is a member of a group of processes that all
  90. receive notes when a note is written to any of their
  91. .B notepg
  92. files (see
  93. .IR proc (3)).
  94. The group of a new process is by default the same as its parent, but if
  95. .B RFNOTEG
  96. is set (regardless of
  97. .BR RFPROC ),
  98. the process becomes the first in a new group, isolated from
  99. previous processes.
  100. .TP
  101. .B RFFDG
  102. If set, the invoker's file descriptor table (see
  103. .IR intro (2))
  104. is copied; otherwise the two processes share a
  105. single table.
  106. .TP
  107. .B RFCFDG
  108. If set, the new process starts with a clean file descriptor table.
  109. Is mutually exclusive with
  110. .BR RFFDG .
  111. .TP
  112. .B RFREND
  113. If set, the process will be unable to
  114. .IR rendezvous (2)
  115. with any of its ancestors; its children will, however, be able to
  116. .B rendezvous
  117. with it. In effect,
  118. .B RFREND
  119. makes the process the first in a group of processes that share a space for
  120. .B rendezvous
  121. tags.
  122. .TP
  123. .B RFMEM
  124. If set, the child and the parent will share
  125. .B data
  126. and
  127. .B bss
  128. segments.
  129. Otherwise, the child inherits a copy of those segments.
  130. Other segment types, in particular stack segments, will be unaffected.
  131. May be set only with
  132. .BR RFPROC .
  133. .PD
  134. .PP
  135. File descriptors in a shared file descriptor table are kept
  136. open until either they are explicitly closed
  137. or all processes sharing the table exit.
  138. .PP
  139. If
  140. .B RFPROC
  141. is set, the
  142. value returned in the parent process
  143. is the process id
  144. of the child process; the value returned in the child is zero.
  145. Without
  146. .BR RFPROC ,
  147. the return value is zero.
  148. Process ids range from 1 to the maximum integer
  149. .RB ( int )
  150. value.
  151. .I Rfork
  152. will sleep, if necessary, until required process resources are available.
  153. .PP
  154. .I Fork
  155. is just a call of
  156. .BR rfork(RFFDG|RFREND|RFPROC) .
  157. .SH SOURCE
  158. .B /sys/src/libc/9syscall
  159. .br
  160. .B /sys/src/libc/9sys/fork.c
  161. .SH SEE ALSO
  162. .IR intro (2),
  163. .IR proc (3),
  164. .SH DIAGNOSTICS
  165. These functions set
  166. .IR errstr .