Commit | Line | Data |
---|---|---|
ad787160 C |
1 | .\" This module is believed to contain source code proprietary to AT&T. |
2 | .\" Use and redistribution is subject to the Berkeley Software License | |
3 | .\" Agreement and your Software Agreement with AT&T (Western Electric). | |
3edcb7c8 | 4 | .\" |
ad787160 | 5 | .\" @(#)p3 8.1 (Berkeley) 6/8/93 |
94bea93d NC |
6 | .\" |
7 | .SH | |
8 | V. PROCESSES AND IMAGES | |
9 | .PP | |
10 | An | |
11 | .IT image | |
12 | is a computer execution environment. | |
13 | It includes a memory image, | |
14 | general register values, | |
15 | status of open files, | |
16 | current directory and the like. | |
17 | An image is the current state of a pseudo-computer. | |
18 | .PP | |
19 | A | |
20 | .IT process | |
21 | is the execution of an image. | |
22 | While the processor is executing on behalf of a process, | |
23 | the image must reside in main memory; | |
24 | during the execution of other processes it remains in main memory | |
25 | unless the appearance of an active, higher-priority | |
26 | process | |
27 | forces it to be swapped out to the disk. | |
28 | .PP | |
29 | The user-memory part of an image is divided into three logical segments. | |
30 | The program text segment begins at location 0 in the virtual address space. | |
31 | During execution, this segment is write-protected | |
32 | and a single copy of it is shared among | |
33 | all processes executing the same program. | |
34 | At the first hardware protection byte boundary above the program text segment in the | |
35 | virtual address space begins a non-shared, writable data segment, | |
36 | the size of which may be extended by a system call. | |
37 | Starting at the highest | |
38 | address in the virtual address space is a stack segment, | |
39 | which automatically grows downward | |
40 | as the stack pointer fluctuates. | |
41 | .SH | |
42 | 5.1 Processes | |
43 | .PP | |
44 | Except while | |
45 | the system | |
46 | is bootstrapping itself into operation, a new | |
47 | process can come into existence only | |
48 | by use of the | |
49 | .UL fork | |
50 | system call: | |
51 | .P1 | |
52 | processid = fork\|(\|\|)\| | |
53 | .P2 | |
54 | When | |
55 | .UL fork | |
56 | is executed, the process | |
57 | splits into two independently executing processes. | |
58 | The two processes have independent | |
59 | copies of the original memory image, | |
60 | and share all open files. | |
61 | The new processes differ only in that one is considered | |
62 | the parent process: | |
63 | in the parent, | |
64 | the returned | |
65 | .UL processid | |
66 | actually identifies the child process | |
67 | and is never 0, | |
68 | while in the child, | |
69 | the returned value is always 0. | |
70 | .PP | |
71 | Because the values returned by | |
72 | .UL fork | |
73 | in the parent and child process are distinguishable, | |
74 | each process may determine whether | |
75 | it is the parent or child. | |
76 | .SH | |
77 | 5.2 Pipes | |
78 | .PP | |
79 | Processes may communicate | |
80 | with related processes using the same system | |
81 | .UL read | |
82 | and | |
83 | .UL write | |
84 | calls that are used for file-system I/O. | |
85 | The call: | |
86 | .P1 | |
87 | filep = pipe\|(\|\|)\| | |
88 | .P2 | |
89 | returns a file descriptor | |
90 | .UL filep | |
91 | and | |
92 | creates an inter-process channel called a | |
93 | .IT pipe . | |
94 | This channel, like other open files, is passed from parent to child process in | |
95 | the image by the | |
96 | .UL fork | |
97 | call. | |
98 | A | |
99 | .UL read | |
100 | using a pipe file descriptor | |
101 | waits until another process writes using the | |
102 | file descriptor for the same pipe. | |
103 | At this point, data are passed between the images of the | |
104 | two processes. | |
105 | Neither process need know that a pipe, | |
106 | rather than an ordinary file, | |
107 | is involved. | |
108 | .PP | |
109 | Although | |
110 | inter-process communication | |
111 | via pipes is a quite valuable tool | |
112 | (see Section 6.2), | |
113 | it is not a completely general | |
114 | mechanism, | |
115 | because the pipe must be set up by a common ancestor | |
116 | of the processes involved. | |
117 | .SH | |
118 | 5.3 Execution of programs | |
119 | .PP | |
120 | Another major system primitive | |
121 | is invoked by | |
122 | .P1 | |
123 | execute\|(\|file, arg\*s\d1\u\*n, arg\*s\d2\u\*n, .\|.\|. , arg\*s\dn\u\*n\|)\| | |
124 | .P2 | |
125 | which requests the system to read in and execute the program | |
126 | named by | |
127 | .UL file , | |
128 | passing it string arguments | |
129 | .UL arg\v'.3'\*s1\*n\v'-.3'\| , | |
130 | .UL arg\v'.3'\*s2\*n\v'-.3'\| , | |
131 | .UL .\|.\|.\|\| , | |
132 | .UL arg\v'.3'\*sn\*n\v'-.3' . | |
133 | All the code and data in the process invoking | |
134 | .UL execute | |
135 | is replaced from the | |
136 | .UL file , | |
137 | but | |
138 | open files, current directory, and | |
139 | inter-process relationships are unaltered. | |
140 | Only if the call fails, for example | |
141 | because | |
142 | .UL file | |
143 | could not be found or because | |
144 | its execute-permission bit was not set, does a return | |
145 | take place from the | |
146 | .UL execute | |
147 | primitive; | |
148 | it resembles a ``jump'' machine instruction | |
149 | rather than a subroutine call. | |
150 | .SH | |
151 | 5.4 Process synchronization | |
152 | .PP | |
153 | Another process control system call: | |
154 | .P1 | |
155 | processid = wait\|(\|status\|)\| | |
156 | .P2 | |
157 | causes its caller to suspend | |
158 | execution until one of its children has completed execution. | |
159 | Then | |
160 | .UL wait | |
161 | returns the | |
162 | .UL processid | |
163 | of the terminated process. | |
164 | An error return is taken if the calling process has no | |
165 | descendants. | |
166 | Certain status from the child process | |
167 | is also available. | |
168 | .SH | |
169 | 5.5 Termination | |
170 | .PP | |
171 | Lastly: | |
172 | .P1 | |
173 | exit\|(\|status\|)\| | |
174 | .P2 | |
175 | terminates a process, | |
176 | destroys its image, | |
177 | closes its open files, | |
178 | and generally obliterates it. | |
179 | The parent is notified through | |
180 | the | |
181 | .UL wait | |
182 | primitive, | |
183 | and | |
184 | .UL status | |
185 | is made available | |
186 | to it. | |
187 | Processes may also terminate as a result of | |
188 | various illegal actions or user-generated signals | |
189 | (Section VII below). |