Spawn (computing)

Last updated

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.

Contents

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 . [1]

DOS/Windows spawn functions

The DOS/Windows spawn functions are inspired by Unix functions fork and exec; however, as these operating systems do not support fork, [2] 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 CreateProcess API.

In the spawnl, spawnlp, spawnv, and spawnvp calls, the child process inherits the environment of the parent. Files that are open when a spawn call is made remain open in the child process.

Prototype

intspawnl(intmode,char*path,char*arg0,...);
intspawnle(intmode,char*path,char*arg0,...,char**envp);
intspawnlp(intmode,char*path,char*arg0,...);
intspawnlpe(intmode,char*path,char*arg0,...,char**envp);
intspawnv(intmode,char*path,char**argv);
intspawnve(intmode,char*path,char**argv,char**envp);
intspawnvp(intmode,char*path,char**argv);
intspawnvpe(intmode,char*path,char**argv,char**envp);

Function names

The base name of each function is spawn, followed by one or more letters:

LetterNotes
lCommand line arguments are passed individually to the function.
vCommand line arguments are passed to the function as an array of pointers.
pUses the PATH argument variable to find the file to be executed.
eAn array of pointers to environment arguments is explicitly passed to the child process.

Mode

The mode argument determines the way the child is run. Values for mode are:

NameNotes
P_OVERLAYOverlays parent process with child, which destroys the parent. This has the same effect as the exec* functions.
P_WAITSuspends parent process until the child process has finished executing (synchronous spawn).
P_NOWAIT, P_NOWAITOContinues to execute calling process concurrently with new process (asynchronous spawn).
P_DETACHthe child is run in background without access to the console or keyboard. Calls to _cwait upon the new process will fail (asynchronous spawn)

Path

The path argument specifies the filename of the program to execute. For spawnlp and spawnvp 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 argv[0] is the name of the program to run.

The command line passed to the spawned program is made up of the character strings, arg0 through argn, in the spawn call. The accepted maximum combined length of these strings differs between compilers, ranging from 128 characters on Digital Mars [3] to 1024 on Microsoft Visual C++ [4] or as much as memory permits, on DJGPP. [5] The last argument after argn has to be a NULL pointer.

argv

The argv 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 spawnle, spawnlpe, spawnve, and spawnvpe calls allow the user to alter the child process's environment by passing a list of environment settings in the envp 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 name is the variable name and value is its value. The last pointer in the array is null. When the envp argument is null, the child inherits the parent's environment settings.

Under Microsoft Windows, the spawn* functions use LoadModule 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 exec_instancehandleget. It is possible to specify how the spawned program will be shown using the functions _exec_showset, _exec_showget, and _exec_showreset.

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 errno is set. Under Microsoft Windows, spawn returns the negated error code returned from LoadModule for compatibility with the C run-time library. The following error codes may be encountered:

ValueNotes
-2 File not found
-3 Path not found
-11 Invalid .exe file (for Windows)
-13 DOS 4. 0 application
-14 Unknown .exe type (may be DOS extended)

POSIX spawn functions

The posix_spawn(3p) 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.

History

The spawn 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 fork (crude copy of memory layout, but fast) versus VMS's spawn (reliable construction of process parameters, but slower). This debate revived when the VMS spawning mechanism was inherited by Windows NT (1993).

See also

Related Research Articles

<span class="mw-page-title-main">Cygwin</span> Unix subsystem for Windows machines

Cygwin is a Unix-like environment and command-line interface for Microsoft Windows. Cygwin's purpose is expressed in its motto: "Get that Linux feeling – on Windows".

<span class="mw-page-title-main">GNU Debugger</span> Source-level debugger

The GNU Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, Assembly, C, C++, D, Fortran, Go, Objective-C, OpenCL C, Modula-2, Pascal, Rust, and partially others.

In computing, particularly in the context of the Unix operating system and its workalikes, fork is an operation whereby a process creates a copy of itself. It is an interface which is required for compliance with the POSIX and Single UNIX Specification standards. It is usually implemented as a C standard library wrapper to the fork, clone, or other system calls of the kernel. Fork is the primary method of process creation on Unix-like operating systems.

<span class="mw-page-title-main">DJGPP</span> Implementation of the GNU toolchain for DOS

DJ's GNU Programming Platform (DJGPP) is a software development suite for Intel 80386-level and above, IBM PC compatibles which supports DOS operating systems. It is guided by DJ Delorie, who began the project in 1989. It is a port of the GNU Compiler Collection (GCC), and mostly GNU utilities such as Bash, find, tar, ls, GAWK, sed, and ld to DOS Protected Mode Interface (DPMI). Supported languages include C, C++, Objective-C/C++, Ada, Fortran, and Pascal.

<span class="mw-page-title-main">Pointer (computer programming)</span> Object which stores memory addresses in a computer program

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.

In computing, the exit status, or exit code, of a terminated process is an integer number that is made available to its parent process. In DOS, this may be referred to as an errorlevel.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

In computing, sigaction is a function API defined by POSIX to give the programmer access to what should be a program's behavior when receiving specific OS signals.

typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type. As such, it is often used to simplify the syntax of declaring complex data structures consisting of struct and union types, although it is also commonly used to provide specific descriptive type names for integer data types of varying sizes.

Dynamic-link library (DLL) is Microsoft's implementation of the shared library concept in the Microsoft Windows and OS/2 operating systems. These libraries usually have the file extension DLL, OCX, or DRV . The file formats for DLLs are the same as for Windows EXE files – that is, Portable Executable (PE) for 32-bit and 64-bit Windows, and New Executable (NE) for 16-bit Windows. As with EXEs, DLLs can contain code, data, and resources, in any combination.

In computer programming, the term hooking covers a range of techniques used to alter or augment the behaviour of an operating system, of applications, or of other software components by intercepting function calls or messages or events passed between software components. Code that handles such intercepted function calls, events or messages is called a hook.

For most file systems, a program initializes access to a file in a file system using the open system call. This allocates resources associated to the file, and returns a handle that the process will use to refer to that file. In some cases the open is performed by the first access.

A scanf format string is a control parameter used in various functions to specify the layout of an input string. The functions can then divide the string and translate into values of appropriate data types. String scanning functions are often supplied in standard libraries. Scanf is a function that reads formatted data from the standard input string, which is usually the keyboard and writes the results whenever called in the specified arguments.

In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process, replacing the previous executable. This act is also referred to as an overlay. It is especially important in Unix-like systems, although it exists elsewhere. As no new process is created, the process identifier (PID) does not change, but the machine code, data, heap, and stack of the process are replaced by those of the new program.

In computer programming, DLL injection is a technique used for running code within the address space of another process by forcing it to load a dynamic-link library. DLL injection is often used by external programs to influence the behavior of another program in a way its authors did not anticipate or intend. For example, the injected code could hook system function calls, or read the contents of password textboxes, which cannot be done the usual way. A program used to inject arbitrary code into arbitrary processes is called a DLL injector.

<span class="mw-page-title-main">CPU time</span> Time used by a computer

CPU time is the amount of time for which a central processing unit (CPU) was used for processing instructions of a computer program or operating system, as opposed to elapsed time, which includes for example, waiting for input/output (I/O) operations or entering low-power (idle) mode. The CPU time is measured in clock ticks or seconds. Often, it is useful to measure CPU time as a percentage of the CPU's capacity, which is called the CPU usage. CPU time and CPU usage have two main uses.

PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.

Getopt is a C library function used to parse command-line options of the Unix/POSIX style. It is a part of the POSIX specification, and is universal to Unix-like systems. It is also the name of a Unix program for parsing command line arguments in shell scripts.

In Unix-like operating systems, dup and dup2 system calls create a copy of a given file descriptor. This new descriptor actually does not behave like a copy, but like an alias of the old one.

References

  1. Posix.1-2008 spawn.h
  2. Windows Subsystem for Linux implements fork. Other POSIX environments such as Cygwin may have an implementation, but using it is not recommended due to the differences in the process model between POSIX and Windows. Fork is not part of the Windows API and most Windows programs do not use these environments, so do not have access to fork.
  3. Digital Mars process.h
  4. Microsoft MSDN
  5. DJGPP spawn*