Spawn (computing)
Function that loads and executes a new child process
title: "Spawn (computing)" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["process-(computing)", "c-posix-library", "process.h"] description: "Function that loads and executes a new child process" topic_path: "technology/operating-systems" source: "https://en.wikipedia.org/wiki/Spawn_(computing)" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0
::summary Function that loads and executes a new child process ::
Spawn in computing refers to a function that loads and executes a new child process. The current process may wait for the child to terminate or may continue to execute concurrent computing. Creating a new subprocess requires enough memory in which both the child process and the current program can execute.
There is a family of spawn functions in DOS, inherited by Microsoft Windows.
There is also a different family of spawn functions in an optional extension of the POSIX standards.
DOS/Windows spawn functions
The DOS/Windows spawn functions are inspired by Unix functions [fork](fork-operating-system) and [exec](exec-operating-system); however, as these operating systems do not support fork, the spawn function was supplied as a replacement for the fork-exec combination. However, the spawn function, although it deals adequately with the most common use cases, lacks the full power of fork-exec, since after fork any process settings which will survive an exec may be changed. However, in most cases, this deficiency can be made up for by using the more low-level API.
In the , , , and calls, the child process inherits the environment of the parent. Files that are open when a call is made remain open in the child process.
Prototype
:int spawnl(int mode, char* path, char* arg0, ...); :int spawnle(int mode, char* path, char* arg0, ..., char* envp[]); :int spawnlp(int mode, char* path, char* arg0, ...); :int spawnlpe(int mode, char* path, char* arg0, ..., char* envp[]); :int spawnv(int mode, char* path, char* argv[]); :int spawnve(int mode, char* path, char* argv[], char* envp[]); :int spawnvp(int mode, char* path, char* argv[]); :int spawnvpe(int mode, char* path, char* argv[], char* envp[]);
Function names
The base name of each function is , followed by one or more letters: ::data[format=table] | Letter||Notes | |---| | | | | | | | | ::
Mode
The argument determines the way the child is run. Values for are:
::data[format=table] | Name || Notes | |---| | | | | | , | | | ::
Path
The argument specifies the filename of the program to execute. For and only, if the filename does not have a path and is not in the current directory, the PATH environment variable determines which directories to search for the file. The string pointed to by is the name of the program to run.
The command line passed to the spawned program is made up of the character strings, through , in the call. The accepted maximum combined length of these strings differs between compilers, ranging from 128 characters on Digital Mars to 1024 on Microsoft Visual C++ or as much as memory permits, on DJGPP. The last argument after has to be a null pointer.
argv
The argument is an array of character pointers. The last pointer in the array must be null to indicate the end of the list.
envp
The , , , and calls allow the user to alter the child process's environment by passing a list of environment settings in the argument. This argument is an array of character pointers; each pointer (except for the last one) points to a null-terminated string defining an environment variable. An environment variable has the form: name=value where is the variable name and is its value. The last pointer in the array is null. When the argument is null, the child inherits the parent's environment settings.
Under Microsoft Windows, the functions use to run the spawned process; and if this fails, an attempt is made to spawn a normal MS-DOS process. If a Windows application is spawned, the instance handle can be obtained using . It is possible to specify how the spawned program will be shown using the functions , , and .
Return values
The return value indicates the exit status of the spawned program. A value of zero indicates that the spawned program executed successfully. A positive value indicates that the spawned program executed, but was aborted or ended in error, the value returned is the exit status of the child process. A negative value indicates that the spawned program did not execute, and is set.
Under Microsoft Windows, returns the negated error code returned from for compatibility with the C run-time library. The following error codes may be encountered:
::data[format=table]
| Value || Notes |
|---|
| -2 |
| -3 |
| -11 |
| -13 |
| -14 |
::
POSIX spawn functions
The and its sibling posix_spawnp can be used as replacements for fork and exec, but does not provide the same flexibility as using fork and exec separately. They may be efficient replacements for fork and exec, but their purpose is to provide process creation primitives in embedded environments where fork is not supported due to lack of dynamic address translation; however, may Unix-like operating systems with full dynamic address translation support also implement them.
History
The metaphor, i.e., to produce offspring as in egg deposition, had its early use in the VMS, now OpenVMS, operating system (1977). In academia, there existed a lively debate between proponents of the Unix (crude copy of memory layout, but fast) versus VMS's (reliable construction of process parameters, but slower). This debate revived when the VMS spawning mechanism was inherited by Windows NT (1993).
Notes
References
References
- [http://www.opengroup.org/onlinepubs/9699919799/basedefs/spawn.h.html Posix.1-2008 spawn.h]
- [http://www.digitalmars.com/rtl/process.html Digital Mars process.h]
- [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/crt_system.2c._wsystem.asp Microsoft MSDN]
- [http://www.delorie.com/djgpp/doc/libc/libc_736.html DJGPP spawn*]
::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::