Product SiteDocumentation Site

8.28. SysFork (Unix only)


>>-SysFork()---------------------------------------------------><

Returns
Returns the process id to the parent process.
Returns 0 to the spawned process.
Example:
This is a complete working example. It can be cut and pasted into a file and executed on a Unix system.

Example 8.14. RexxUtil - SysFork



/* Example Unix SysFork() and SysWait() */

  pid = SysFork()

  if pid == 0 then do
    say "I am the child."
    code = executeChild()
    say "Child : done with execution, will exit with" code
    exit code
  end
  else do
    say 'I am the parent, child pid is:' pid
    code = executeParent()
    say 'Parent: going to wait for child.'
    code = SysWait()
    say 'Parent: back from waiting. Child exit code:' code
  end

  say 'Operating system version:' SysVersion()

::routine executeChild

    say 'Child : will sleep 1 second.'
    j = SysSleep(1)
    say 'Child : done sleeping 1. Will do some calculations.'

    total = 0
    do 786
      total += 3
    end
    say 'Child : 3 * 786 is:' total

    say 'Child : will sleep 2 seconds.'
    j = SysSleep(2)
    say 'Child : done sleeping 2. Will do some calculations.'

    total = 0
    do 1865
      total += 7
    end
    say 'Child : 7 * 1865 is:' total

    say 'Child : will sleep 2 seconds.'
    j = SysSleep(2)
    say 'Child : done sleeping 2.'
    say 'Child : done executing, will return 0.'
    return 0

::routine executeParent

    say 'Parent: 3 * 786 is:' (3 * 786)
    j = SysSleep(2)
    say 'Parent: 7 * 1865 is:' (7 * 1865)
    return 0