xref: /freebsd/share/doc/psd/01.cacm/p3 (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
This module is believed to contain source code proprietary to AT&T.
Use and redistribution is subject to the Berkeley Software License
Agreement and your Software Agreement with AT&T (Western Electric).

@(#)p3 8.1 (Berkeley) 6/8/93

$FreeBSD$
V. PROCESSES AND IMAGES

An T image is a computer execution environment. It includes a memory image, general register values, status of open files, current directory and the like. An image is the current state of a pseudo-computer.

A T process is the execution of an image. While the processor is executing on behalf of a process, the image must reside in main memory; during the execution of other processes it remains in main memory unless the appearance of an active, higher-priority process forces it to be swapped out to the disk.

The user-memory part of an image is divided into three logical segments. The program text segment begins at location 0 in the virtual address space. During execution, this segment is write-protected and a single copy of it is shared among all processes executing the same program. At the first hardware protection byte boundary above the program text segment in the virtual address space begins a non-shared, writable data segment, the size of which may be extended by a system call. Starting at the highest address in the virtual address space is a stack segment, which automatically grows downward as the stack pointer fluctuates.

5.1 Processes

Except while the system is bootstrapping itself into operation, a new process can come into existence only by use of the fork system call:

1 processid = fork\|(\|\|)\|

2 When fork is executed, the process splits into two independently executing processes. The two processes have independent copies of the original memory image, and share all open files. The new processes differ only in that one is considered the parent process: in the parent, the returned processid actually identifies the child process and is never 0, while in the child, the returned value is always 0.

Because the values returned by fork in the parent and child process are distinguishable, each process may determine whether it is the parent or child.

5.2 Pipes

Processes may communicate with related processes using the same system read and write calls that are used for file-system I/O. The call:

1 filep = pipe\|(\|\|)\|

2 returns a file descriptor filep and creates an inter-process channel called a T pipe . This channel, like other open files, is passed from parent to child process in the image by the fork call. A read using a pipe file descriptor waits until another process writes using the file descriptor for the same pipe. At this point, data are passed between the images of the two processes. Neither process need know that a pipe, rather than an ordinary file, is involved.

Although inter-process communication via pipes is a quite valuable tool (see Section 6.2), it is not a completely general mechanism, because the pipe must be set up by a common ancestor of the processes involved.

5.3 Execution of programs

Another major system primitive is invoked by

1 execute\|(\|file, arg\*s\d1\u\*n, arg\*s\d2\u\*n, .\|.\|. , arg\*s\dn\u\*n\|)\|

2 which requests the system to read in and execute the program named by file , passing it string arguments arg\v'.3'\*s1\*n\v'-.3'\| , arg\v'.3'\*s2\*n\v'-.3'\| , .\|.\|.\|\| , arg\v'.3'\*sn\*n\v'-.3' . All the code and data in the process invoking execute is replaced from the file , but open files, current directory, and inter-process relationships are unaltered. Only if the call fails, for example because file could not be found or because its execute-permission bit was not set, does a return take place from the execute primitive; it resembles a ``jump'' machine instruction rather than a subroutine call.

5.4 Process synchronization

Another process control system call:

1 processid = wait\|(\|status\|)\|

2 causes its caller to suspend execution until one of its children has completed execution. Then wait returns the processid of the terminated process. An error return is taken if the calling process has no descendants. Certain status from the child process is also available.

5.5 Termination

Lastly:

1 exit\|(\|status\|)\|

2 terminates a process, destroys its image, closes its open files, and generally obliterates it. The parent is notified through the wait primitive, and status is made available to it. Processes may also terminate as a result of various illegal actions or user-generated signals (Section VII below).