Sun Studio 12: Fortran Library Reference

1.4.12 fork: Create a Copy of the Current Process

The function is called by:

INTEGER*4 fork

n = fork()

Return value 

INTEGER*4

Output 

n>0: n=Process ID of copy

n<0, n=System error code

The fork function creates a copy of the calling process. The only distinction between the two processes is that the value returned to one of them, referred to as the parent process, will be the process ID of the copy. The copy is usually referred to as the child process. The value returned to the child process will be zero.

All logical units open for writing are flushed before the fork to avoid duplication of the contents of I/O buffers in the external files.

Example: fork():


       INTEGER*4 fork, pid
       pid = fork()
       if(pid.lt.0) stop ’fork error’
       if(pid.gt.0) then
              print *, ’I am the parent’
       else
              print *, ’I am the child’
       endif

A corresponding exec routine has not been provided because there is no satisfactory way to retain open logical units across the exec routine. However, the usual function of fork/exec can be performed using system(3F). See also: fork(2), wait(3F), kill(3F), system(3F), and perror(3F).