Next: , Previous: , Up: Extension Samples   [Contents][Index]


16.7.3 Interface to fork(), wait(), and waitpid()

The fork extension adds three functions, as follows:

@load "fork"

This is how you load the extension.

pid = fork()

This function creates a new process. The return value is zero in the child and the process ID number of the child in the parent, or -1 upon error. In the latter case, ERRNO indicates the problem. In the child, PROCINFO["pid"] and PROCINFO["ppid"] are updated to reflect the correct values.

ret = waitpid(pid)

This function takes a numeric argument, which is the process ID to wait for. The return value is that of the waitpid() system call.

ret = wait()

This function waits for the first child to die. The return value is that of the wait() system call.

There is no corresponding exec() function.

Here is an example:

@load "fork"
…
if ((pid = fork()) == 0)
    print "hello from the child"
else
    print "hello from the parent"