【问题标题】:How to handle errors in execvp?如何处理 execvp 中的错误?
【发布时间】:2015-04-09 10:08:07
【问题描述】:

我编写了一个小程序(使用来自 SO 的代码)来促进 printenv | sort | less。现在我想实现错误处理,我从 execvp 开始。是否只是检查返回值等等? AFAIK 我只是检查这个函数return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv); 中的返回值是否为0。对吗?

#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct command
{
    const char **argv;
};
/* Helper function that spawns processes */
int spawn_proc (int in, int out, struct command *cmd) {
    pid_t pid;
    if ((pid = fork ()) == 0) {
        if (in != 0) {
            dup2 (in, 0);
            close (in);
        }
        if (out != 1) {
            dup2 (out, 1);
            close (out);
        }
        return execvp (cmd->argv [0], (char * const *)cmd->argv);
    }
    return pid;
}
/* Helper function that forks pipes */
int fork_pipes (int n, struct command *cmd) {
    int i;
    int in, fd [2];
    for (i = 0; i < n - 1; ++i) {
        pipe (fd);
        spawn_proc (in, fd [1], cmd + i);
        close (fd [1]);
        in = fd [0];
    }
    dup2 (in, 0);
    return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}

int main (int argc, char ** argv) {
    int i;
    if (argc == 1) { /* There were no arguments */
        const char *printenv[] = { "printenv", 0};
        const char *sort[] = { "sort", 0 };
        const char *less[] = { "less", 0 };
        struct command cmd [] = { {printenv}, {sort}, {less} };
        return fork_pipes (3, cmd);
    }
    if (argc > 1) { /* I'd like an argument */

        if (strncmp(argv[1], "cd", 2) && strncmp(argv[1], "exit", 2)) {
            char *tmp;
            int len = 1;
            for( i=1; i<argc; i++)
            {
                len += strlen(argv[i]) + 2;
            }
            tmp = (char*) malloc(len);
            tmp[0] = '\0';
            int pos = 0;
            for( i=1; i<argc; i++)
            {
                pos += sprintf(tmp+pos, "%s%s", (i==1?"":"|"), argv[i]);
            }
            const char *printenv[] = { "printenv", 0};
            const char *grep[] = { "grep", "-E", tmp, NULL};
            const char *sort[] = { "sort", 0 };
            const char *less[] = { "less", 0 };
            struct command cmd [] = { {printenv}, {grep}, {sort}, {less} };
            return fork_pipes (4, cmd);
            free(tmp);
        } else if (! strncmp(argv[1], "cd", 2)) { /* change directory */
            printf("change directory to %s\n" , argv[2]);
            chdir(argv[2]);
        } else if (! strncmp(argv[1], "exit", 2)) { /* change directory */
            printf("exit\n");
            exit(0);
        }
    }
    exit(0);
}

【问题讨论】:

  • 这段代码不是内核代码。

标签: c linux exec pipeline


【解决方案1】:

查看manualexecvp 将您的进程的程序映像更改为另一个。所以如果有返回值,显然是-1,因为它显然已经失败了。

对于任何系统调用或调用系统调用的函数,使用 set errno 执行 execvp 以获得适当的错误代码,您可以使用 perror 来了解函数失败的原因。 (顺便说一句,您应该对任何系统调用执行相同的操作:至少 fork、pipe 和 dup2。)

然后,如果“错误处理”是指处理您启动的程序中的错误,您可以使用wait or waitpid 进行处理。它可以让你知道程序是如何结束的,它的返回值,它是否以一个信号结束,如果是的话,等等。

【讨论】:

  • "stop[s] your current processus to start another" 这是不正确的,因为没有停止进程也没有创建新进程,但是当前过程映像被另一个替换。本质上进程的 PID 不会改变,这是进程保持不变。
  • 已编辑。感谢您的精确。
猜你喜欢
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 2013-10-29
相关资源
最近更新 更多