FORK

Split the current process

 

USupported on Unix

 

 

xcall FORK(child_id)

Arguments

child_id

Returned with the process ID of the child process or with a zero, depending on which process is returning. (n)

Discussion

The FORK subroutine splits the current process by invoking the Unix/POSIX system call fork(2).

This subroutine invokes fork(2), which splits the current process into two processes, both of which return from the FORK call.

The original process is the parent. When it returns, the return value is the process ID of the created subprocess.

The created subprocess, or child, is an exact copy of the parent except that all channels opened by the parent have been closed in the child. (Therefore, you must reopen any files needed by the child.) It also returns from the FORK call, but the return value is 0, so your program can detect the fact that it is the child process.

Both processes return from the call with exactly the same memory contents and files open. Your program must examine the child_id argument to decide on the different actions for the two processes.

Child_id should be no smaller than an i4 or d10 to ensure getting the entire process ID.

The child process can call the JBNO subroutine to find its parent.

Note

When using the Synergy socket API with FORK, you may want to specify the SS_PURGE option on the %SS_CLOSE routine to ensure that the FORK’d process does not force the connection to be terminated for the parent process when it closes the socket.

The accepted socket (with %SS_ACCEPT) on the server needs to be purged after (and only after) it knows the client has cloned the socket and is using it, because the child now owns the socket. The server socket should be purged in the child program.

Note

If channels are open to a Synergy/DE xfServer from a program using FORK, and the server channel for a file is closed by either the parent or child program, subsequent I/O to the channel will generate errors.

Note

When FORK is performed, the Unix exec command creates a duplicate process which then closes channels that are open. If you have not flushed data for a file opened for output, the data may be duplicated due to the deferred flush of cached write data.

Examples

This example routine forks a process and returns 1 or 0. On 1, the CHILD returns its process ID and its parent’s; on 0, the PARENT returns its process ID and the new child’s.

.define CHILD           ,1
.define PARENT          ,2
subroutine split
    a_which     ,n
    a_parent    ,n
    a_child     ,n
record
    pid         ,i4
proc
    xcall fork(pid)
    a_which = .not.pid                          ;Set a_which to parent or child
    if (pid) then                               ;This is the parent process
      begin
        a_child = pid
        a_parent = %jbno()
      end
    else                                        ;This is the child process
      a_child = %jbno(a_parent)
    xreturn
endsubroutine