17 library(unix): Unix specific operations
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog C-library
        • library(unix): Unix specific operations
          • fork/1
          • fork_exec/1
          • exec/1
          • wait/2
          • kill/2
          • pipe/2
          • dup/2
          • detach_IO/1
          • detach_IO/0
          • prctl/1
          • sysconf/1
Availability::- use_module(library(unix)).(can be autoloaded)
Sourceexec(+Command)
Replace the running program by starting Command. Command is a callable term. The functor is the command and the arguments provide the command-line arguments for the command. Each command-line argument must be atomic and is converted to a string before passed to the Unix call execvp(). Here are some examples:

  • exec(ls('-l'))
  • exec('/bin/ls'('-l', '/home/jan'))

Unix exec() is the only way to start an executable file executing. It is commonly used together with fork/1. For example to start netscape on an URL in the background, do:

run_netscape(URL) :-
        (    fork(child),
             exec(netscape(URL))
        ;    true
        ).

Using this code, netscape remains part of the process-group of the invoking Prolog process and Prolog does not wait for netscape to terminate. The predicate wait/2 allows waiting for a child, while detach_IO/0 disconnects the child as a deamon process.