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