有时候,我们需要在自己的程序(进程)中启动另一个程序(进程)来帮助我们完成一些工作,那么我们需要怎么才能在自己的进程中启动其他的进程呢?在Linux中提供了不少的方法来实现这一点,下面就来介绍一个这些方法及它们之间的区别。
system()
函数调用
Function
The system()
function hands the argument command to the command interpreter sh. The calling process waits for the shell to finish executing the command, ignoring SIGINT and SIGQUIT, and blocking SIGCHLD.
If command is a NULL pointer, system() will return non-zero if the command interpreter sh(1) is available, and zero if it is not.
The system()
library function uses fork to create a childprocess that executes the shell command specified in command using execl as follows:
execl("/bin/sh", "sh", "-c", command, (char *)
system()
returns after the command has been completed.
SYNOPSIS
#include <stdlib.h>
int system (const char *string);
它的作用是,运行以字符串参数的形式传递给它的命令并等待该命令的完成。命令的执行情况就如同在shell中执行命令:sh -c string
。如果无法启动shell来运行这个命令,system()
函数返回错误代码127;如果是其他错误,则返回-1。否则,system函数将返回该命令的退出码。
...