【发布时间】:2011-08-02 07:38:05
【问题描述】:
我想在我的 c 程序中运行一个 shell 命令。但问题是我不想让我的程序等到命令执行。无需读取 shell 命令的输出(反正它不返回任何数据)所以基本上,这可能吗?
【问题讨论】:
-
顺便说一句,如果你想运行 shell 命令或其他可执行文件并不重要。无论您使用
system()还是fork()/exec()方法,只需要一个可执行文件即可。也许您想相应地编辑问题的标题?
我想在我的 c 程序中运行一个 shell 命令。但问题是我不想让我的程序等到命令执行。无需读取 shell 命令的输出(反正它不返回任何数据)所以基本上,这可能吗?
【问题讨论】:
system() 还是fork()/exec() 方法,只需要一个可执行文件即可。也许您想相应地编辑问题的标题?
fork() 和 system() 是您所需要的
【讨论】:
system 会让子进程等待 shell 命令 - exec 会用 shell 替换子进程。
当然,只需fork 和exec:使用fork 创建一个新进程,并在子进程中使用exec 使用您的命令启动shell。 execv 接受您通常会提供给 shell 的参数。
您的代码可能如下所示:
pid_t child_pid = fork();
if (child_pid == 0)
{ // in child
/* set up arguments */
// launch here
execv("/bin/sh", args);
// if you ever get here, there's been an error - handle it
}
else if (child_pid < 0)
{ // handle error
}
子进程在死亡时会发送一个SIGCHLD 信号。从 POSIX 标准 (SUSv4) 引用的这段代码将处理:
static void
handle_sigchld(int signum, siginfo_t *sinfo, void *unused)
{
int status;
/*
* Obtain status information for the child which
* caused the SIGCHLD signal and write its exit code
* to stdout.
*/
if (sinfo->si_code != CLD_EXITED)
{
static char msg[] = "wrong si_code\n";
write(2, msg, sizeof msg - 1);
}
else if (waitpid(sinfo->si_pid, &status, 0) == -1)
{
static char msg[] = "waitpid() failed\n";
write(2, msg, sizeof msg - 1);
}
else if (!WIFEXITED(status))
{
static char msg[] = "WIFEXITED was false\n";
write(2, msg, sizeof msg - 1);
}
else
{
int code = WEXITSTATUS(status);
char buf[2];
buf[0] = '0' + code;
buf[1] = '\n';
write(1, buf, 2);
}
}
【讨论】:
ls | grep -v hello。这将适用于系统,但不适用于 exec。
fork 和 exec)。
试试这样的代码:
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char ** argv)
{
if (!fork())
{
execv("ls", {"myDir"}); /* Your command with arguments instead of ls. */
}
}
【讨论】:
waitpid的其他答案。
用system ("command &") 简单地放大命令怎么样?
【讨论】: