【列表5.6】等待一个fork过程的子进程的结束。
{
forkWait - Illustrate forking to a child
process and waiting for it to complete before
continuing.
}
program ForkWait;
{$APPTYPE CONSOLE}
uses
Libc,
SysUtils;
var
forkResult : __pid_t;
waitResult : __pid_t;
procedure DoChildProcess;
var
s : String;
begin
writeln;
writeln('Child: ProcessID = ', getpid);
write('Child: Press the [ENTER] key...');
readln(s);
writeln('Child: Exiting');
end;
begin
writeln;
writeln('Parent: ProcessID = ', getpid);
forkResult := fork;
case forkResult of
-1 : writeln('Error creating child process');
0 : DoChildProcess;
else begin
waitResult := waitpid(forkResult, nil, 0);
if waitResult forkResult
then writeln('Parent: Error terminating child process')
else writeln('Parent: Exiting');
end;
end; { case }
writeln;
end.