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