为什么子进程返回UNIX退出状态= 32512? [英] Why child process returns exit status = 32512 in unix?

查看:2155
本文介绍了为什么子进程返回UNIX退出状态= 32512?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的计划,我执行给定的命令和获得的结果(日志,并退出状态)。另外我的程序必须支持的shell特定的命令(其中包含shell特定字符〜(tild),即命令|(管道),*)。但是,当我尝试运行上海-c LS | WC 在通过我的程序它失败,退出状态我的主目录是32512,也是在标准错误流SH:LS |厕所:找不到命令印。

In my program I'm executing given command and getting result (log, and exit status). Also my program have to support shell specific commands (i.e. commands which contains shell specific characters ~(tild),|(pipe),*). But when I try to run sh -c ls | wc in my home directory via my program it failed and its exit status was 32512, also in stderr stream "sh: ls | wc: command not found" was printed.

但有趣的是,该命令 SH -c LS |如果我在shell中运行它WC 工作正确。

But the interesting thing is that the command sh -c ls | wc works correct if I run it in shell.

这是什么问题?以上preferable我如何通过我的程序运行shell特定的命令(i.ec它与我应该运行的参数命令)?

What is the problem? Or more preferable how can I run shell specific commands via my program (i.ec which command with which parameters should I run)?

在code部分波纹管在孩子后叉部分()。它executs命令。

The code part bellow is in child part after fork(). It executs the command.

tokenized_command 的std ::矢量<标准::字符串> ,其中在我的情况SH,-c,LS,|,WC存储,也是我试图存储在那里嘘, - C,\\LS | WC \\,但结果是一样的。 命令的char * ,其中完整的命令行的存储位置。

tokenized_command is std::vector<std::string> where in my case "sh", "-c", "ls", "|", "wc" are stored, also I have tried to store there "sh", "-c", "\"ls | wc\"" but result is same. command is char * where full command line is stored.

        boost::shared_array<const char *> bargv(new const char *[tokenized_command.size() + 1]);
        const char **argv = bargv.get();
        for(int i = 0; i < tokenized_command.size(); ++i)
        {
            argv[i] = tokenized_command[i].c_str();
            printf("argv[%d]: %s\n", i, argv[i]); //trace
        }
        argv[tokenized_command.size()] = NULL;

        if(execvp(argv[0], (char * const *)argv) == -1)
        {
            fprintf(stderr, "Failed to execute command %s: %s", command, strerror(errno));
            _exit(EXIT_FAILURE);
        }

P.S。

我知道,使用系统(命令)而不是 execvp 可以解决我的问题。但系统()等待,直到命令完成,而这不是我的程序不够好。而且我敢肯定,在实施的系统()的EXEC,家庭功能一个被使用,所以这个问题可以通过 EXEC解决为好,但我不知道怎么办。

I know that using system(command) instead execvp can solve my problem. But system() waits until command is finished, and this is not good enough for my program. And also I'm sure that in implementation of system() one of exec-family functions is used, so the problem can be solved via exec as well, but I don't know how.

推荐答案

execvp 需要一个路径的可执行文件和参数,用以启动该可执行文件。它并不需要Bourne shell命令。

execvp takes a path to an executable, and arguments with which to launch that executable. It doesn't take bourne shell commands.

LS | WC 是一个Bourne shell命令(等等),它不能被分解成路径的可执行文件和一些参数,由于采用了管道。这意味着它可以不使用执行 execvp

ls | wc is a bourne shell command (among others), and it can't be broken down into the path to an executable and some arguments due to the use of a pipe. This means it can't be executed using execvp.

要执行使用 execvp 一个Bourne shell命令,一有执行 SH 并通过 -c 和参数的命令。

To execute a bourne shell command using execvp, one has to execute sh and pass -c and the command for arguments.

所以,你要执行 LS | WC 使用 execvp

char *const argv[] = {
    "sh",
    "-c", "ls | wc",  // Command to execute.
    NULL
};

execvp(argv[0], argv)

您显然试图

char *const argv[] = {
    "sh",
    "-c", "ls",  // Command to execute.
    "|",         // Stored in called sh's $0.
    "wc",        // Stored in called sh's $1.
    NULL
};

这将是相同的Bourne shell命令上海-c LS| WC

That would be the same as bourne shell command sh -c ls '|' wc.

和两者都比shell命令上海-c LS截然不同| WC 。这将是

And both are very different than shell command sh -c ls | wc. That would be

char *const argv[] = {
    "sh",
    "-c", "sh -c ls | wc",  // Command to execute.
    NULL
};

您似乎认为 | 厕所传递给 SH ,但事实并非在所有的情况。 | 是导致管道特殊字符,而不是一个参数

You seem to think | and wc are passed to the sh, but that's not the case at all. | is a special character which results in a pipe, not an argument.

至于出口code,

Bits 15-8 = Exit code.
Bit     7 = 1 if a core dump was produced.
Bits  6-0 = Signal number that killed the process.

32512 = 0x7F00

32512 = 0x7F00

因此​​,它不从信号死,是不是产生了核心转储,并将它与code 127(0x7F的)退出。

So it didn't die from a signal, a core dump wasn't produced, and it exited with code 127 (0x7F).

127的意思是不清楚的,这就是为什么它应该伴有错误消息。试图执行程序 LS | WC ,但目前还没有这样的计划。

What 127 means is unclear, which is why it should accompanied by an error message. You tried to execute program ls | wc, but there is no such program.

这篇关于为什么子进程返回UNIX退出状态= 32512?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆