进程和环境控制Process and Environment Control

使用进程控制例程启动、停止和管理程序内的进程。Use the process-control routines to start, stop, and manage processes from within a program. 使用环境控制例程获取和更改与操作系统环境有关的信息。Use the environment-control routines to get and change information about the operating-system environment.

进程和环境控制函数Process and Environment Control Functions

例程Routine 使用Use
abortabort 中止进程而无需刷新缓冲区或调用通过 atexit_onexit 注册的函数Abort process without flushing buffers or calling functions registered by atexit and _onexit
assertassert 测试逻辑错误Test for logic error
_ASSERT、_ASSERTE_ASSERT, _ASSERTE macros 类似于 assert,但仅在运行时库的调试版本中可用Similar to assert, but only available in the debug versions of the run-time libraries
atexitatexit 在程序终止时执行的计划例程Schedule routines for execution at program termination
_beginthread、_beginthreadex_beginthread, _beginthreadex 在 Windows 操作系统进程中创建新的线程Create a new thread on a Windows operating system process
_cexit_cexit 执行 exit 终止过程(例如刷新缓冲区),然后在不终止进程的情况下将控制权返回到调用程序Perform exit termination procedures (such as flushing buffers), then return control to calling program without terminating process
_c_exit_c_exit 执行 _exit 终止过程,然后在不终止进程的情况下将控制权返回到调用程序Perform _exit termination procedures, then return control to calling program without terminating process
_cwait_cwait 请等待,直到另一个进程终止Wait until another process terminates
_endthread、_endthreadex_endthread, _endthreadex 终止一个 Windows 操作系统线程Terminate a Windows operating system thread
_execl、_wexecl_execl, _wexecl 使用参数列表执行新进程Execute new process with argument list
_execle、_wexecle_execle, _wexecle 使用参数列表和给定的环境执行新进程Execute new process with argument list and given environment
_execlp、_wexeclp_execlp, _wexeclp 使用 PATH 变量和参数列表执行新进程Execute new process using PATH variable and argument list
_execlpe、_wexeclpe_execlpe, _wexeclpe 使用 PATH 变量、给定的环境和参数列表执行新进程Execute new process using PATH variable, given environment, and argument list
_execv、_wexecv_execv, _wexecv 使用参数数组执行新进程Execute new process with argument array
_execve、_wexecve_execve, _wexecve 使用参数数组和给定的环境执行新进程Execute new process with argument array and given environment
_execvp、_wexecvp_execvp, _wexecvp 使用 PATH 变量和参数数组执行新进程Execute new process using PATH variable and argument array
_execvpe、_wexecvpe_execvpe, _wexecvpe 使用 PATH 变量、给定的环境和参数数组执行新进程Execute new process using PATH variable, given environment, and argument array
exitexit 调用通过 atexit_onexit 注册的函数,刷新所有缓冲区,关闭所有打开的文件,然后终止进程Call functions registered by atexit and _onexit, flush all buffers, close all open files, and terminate process
_exit_exit 立即终止进程,无需调用 atexit_onexit 或刷新缓冲区Terminate process immediately without calling atexit or _onexit or flushing buffers
getenv, _wgetenvgetenv_s, _wgetenv_sgetenv, _wgetenv, getenv_s, _wgetenv_s 获取环境变量的值Get value of environment variable
_getpid_getpid 获取进程 ID 号Get process ID number
longjmplongjmp 还原保存的堆栈环境;使用它来执行非本地 gotoRestore saved stack environment; use it to execute a nonlocal goto
_onexit_onexit 在程序终止时执行的计划例程;用于与 Microsoft C/C++ 版本 7.0 和更早版本的兼容性Schedule routines for execution at program termination; use for compatibility with Microsoft C/C++ version 7.0 and earlier
_pclose_pclose 等待新命令处理器并关闭关联管道上的流Wait for new command processor and close stream on associated pipe
perror、_wperrorperror, _wperror 打印错误消息Print error message
_pipe_pipe 创建用于读取和写入的管道Create pipe for reading and writing
_popen、_wpopen_popen, _wpopen 创建管道,然后执行命令Create pipe and execute command
_putenv, _wputenv_putenv_s, _wputenv_s_putenv, _wputenv, _putenv_s, _wputenv_s 添加或更改环境变量的值Add or change value of environment variable
raiseraise 将信号发送到调用进程Send signal to calling process
setjmpsetjmp 保存堆栈环境;用于执行非本地 gotoSave stack environment; use to execute non local goto
signalsignal 处理中断信号Handle interrupt signal
_spawnl、_wspawnl_spawnl, _wspawnl 使用指定的参数列表创建和执行新进程Create and execute new process with specified argument list
_spawnle、_wspawnle_spawnle, _wspawnle 使用指定的参数列表和环境创建和执行新进程Create and execute new process with specified argument list and environment
_spawnlp、_wspawnlp_spawnlp, _wspawnlp 使用 PATH 变量和指定的参数列表创建和执行新进程Create and execute new process using PATH variable and specified argument list
_spawnlpe、_wspawnlpe_spawnlpe, _wspawnlpe 使用 PATH 变量、指定的环境和参数列表创建和执行新进程Create and execute new process using PATH variable, specified environment, and argument list
_spawnv、_wspawnv_spawnv, _wspawnv 使用指定的参数数组创建和执行新进程Create and execute new process with specified argument array
_spawnve、_wspawnve_spawnve, _wspawnve 使用指定的环境和参数数组创建和执行新进程Create and execute new process with specified environment and argument array
_spawnvp、_wspawnvp_spawnvp, _wspawnvp 使用 PATH 变量和指定的参数数组创建和执行新进程Create and execute new process using PATH variable and specified argument array
_spawnvpe、_wspawnvpe_spawnvpe, _wspawnvpe 使用 PATH 变量、指定的环境和参数数组创建和执行新进程Create and execute new process using PATH variable, specified environment, and argument array
system、_wsystemsystem, _wsystem 执行操作系统命令Execute operating-system command

在 Windows 操作系统中,生成的进程等同于正在生成的进程。In the Windows operating system, the spawned process is equivalent to the spawning process. 任何进程都可以使用 _cwait 等待其进程 ID 为已知的任何其他进程。Any process can use _cwait to wait for any other process for which the process ID is known.

_exec_spawn 系列间的差异是:_spawn 函数可以将新进程中的控制权返回到调用进程。The difference between the _exec and _spawn families is that a _spawn function can return control from the new process to the calling process. _spawn 函数中,除非已指定 _P_OVERLAY,否则调用进程以及新进程都存在于内存中。In a _spawn function, both the calling process and the new process are present in memory unless _P_OVERLAY is specified. _exec 函数中,新进程将覆盖调用进程,因此除非在尝试启动新进程的执行中发生错误,否则控制权不能返回到调用进程。In an _exec function, the new process overlays the calling process, so control cannot return to the calling process unless an error occurs in the attempt to start execution of the new process.

如下表所示,_exec 系列中函数间的差异以及 _spawn 系列中函数间的差异涉及以下方面:查找要作为新进程执行的文件的方法、参数传递到新进程的形式和设置环境的方法。The differences among the functions in the _exec family, as well as among those in the _spawn family, involve the method of locating the file to be executed as the new process, the form in which arguments are passed to the new process, and the method of setting the environment, as shown in the following table. 如果参数的数目为常数或在编译时已知,请使用传递参数列表的函数。Use a function that passes an argument list when the number of arguments is constant or is known at compile time. 如果参数的数目需要在运行时确定,请使用将指针传递到包含参数的数组中的函数。Use a function that passes a pointer to an array containing the arguments when the number of arguments is to be determined at run time. 下表中的信息还适用于 _spawn_exec 函数的宽字符对应项。The information in the following table also applies to the wide-character counterparts of the _spawn and _exec functions.

_spawn 和 _exec 函数系列_spawn and _exec Function Families

函数Functions 使用 PATH 变量查找文件Use PATH variable to locate file 参数传递约定Argument-passing convention 环境设置Environment settings
_execl, _spawnl NoNo 列表List 继承自调用进程Inherited from calling process
_execle, _spawnle NoNo 列表List 指向作为最后一个参数传递的新进程的环境表Pointer to environment table for new process passed as last argument
_execlp, _spawnlp Yes 列表List 继承自调用进程Inherited from calling process
_execlpe, _spawnlpe Yes 列表List 指向作为最后一个参数传递的新进程的环境表Pointer to environment table for new process passed as last argument
_execv, _spawnv NoNo 数组Array 继承自调用进程Inherited from calling process
_execve, _spawnve NoNo 数组Array 指向作为最后一个参数传递的新进程的环境表Pointer to environment table for new process passed as last argument
_execvp, _spawnvp Yes 数组Array 继承自调用进程Inherited from calling process
_execvpe, _spawnvpe Yes 数组Array 指向作为最后一个参数传递的新进程的环境表Pointer to environment table for new process passed as last argument

另请参阅See Also

按类别分的运行时例程Run-Time Routines by Category