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