【问题标题】:use exec in c program在c程序中使用exec
【发布时间】:2015-06-24 10:44:20
【问题描述】:

我正在使用 linux,我想在 c 中编写一个程序,该程序从用户读取命令,直到我输入 stop。对于每个命令,主程序将创建一个进程 A,该进程将创建另一个进程 B。进程 B 将执行用户输入的命令。我想使用 exec 使其工作,但它仅适用于一个单词命令(例如:pwdls),如果我输入例如ls -l,它表示没有这样的文件或目录。到目前为止我写的代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define N 100

int main() {
    char input[N];
    scanf(" %[^\n]s",input); //reads from user inputs that can include space 
    while (strcmp(input,"stop") != 0) { // verifies if entered command == stop
        int a;
        a = fork();//creates the process A
        if (a == -1) {
            perror("fork imposibil!");
            exit(1);
        }
        if (a == 0) {
            printf("a\n");//i printed that to verify if my fork works
            int b;

            b = fork();//creates process B
            if (b == -1) {
                perror("fork imposibil!");
                exit(1);
            }
            if (b == 0) { // the part that makes me problems , i try to use exec to execute the command that is stored in input
                printf("b\n");
                if (execlp(input,"",(char*) 0) < 0) {
                    perror("Error exec");
                    exit(0);
                }
                exit(0);
            }
            wait(0);
            exit(0);
        }
        wait(0);

        scanf(" %[^\n]s",input);
    }
    return 0;
}

有人可以帮帮我吗? 附言我不认为我的代码难以阅读,但我希望现在更好

【问题讨论】:

  • 首先学习如何编写可读的代码,然后你可能会问一个问题,因为阅读你的代码几乎是不可能的。例如,我在perror() 之后没有看到exit(),根本没有看到,我想,哎呀,他报告了错误,但仍然忽略它们。而且我认为您应该从子进程中调用_exit(0) 而不是exit(0)

标签: c linux command exec


【解决方案1】:

从您的示例ls -l 中,-l 应传递给参数。

int execlp(const char *file, const char *arg, ...);

help 的引用说

函数可以被认为是arg0, arg1, ..., argn。他们一起 描述一个或多个指向以空字符结尾的字符串的指针列表 表示可用于执行程序的参数列表。 第一个参数,按照惯例,应该指向文件名 与正在执行的文件相关联。参数列表必须是 由 NULL 指针终止,并且由于这些是可变参数函数, 此指针必须强制转换 (char *) NULL。

【讨论】:

    【解决方案2】:

    您没有按照预期的方式将参数传递给函数 execlp()。

    这些函数的初始参数是文件名 被执行。

    execl()、execlp() 中的 const char *arg 和后续省略号, 和 execle() 函数可以被认为是 arg0, arg1, ..., argn。 它们一起描述了一个或多个指针的列表 以 null 结尾的字符串,表示可用于 执行的程序。按照惯例,第一个论点应该指出 到与正在执行的文件关联的文件名。名单 参数必须由 NULL 指针终止,并且,因为这些是 可变参数函数,此指针必须强制转换 (char *) NULL。

    看看这个关于如何使用 execlp() 函数的解释 - I do not understand how execlp() works in Linux

    我猜你必须使用带有分隔符" " 的字符串拆分器函数来获取像-l 这样的命令行参数,在你的情况下为ls -l

    strtok() 在 C 中提供此功能。

    #include <string.h>
    #include <stdio.h>
    
    int main(void)
    {
        char input[] = "A bird came down the walk";
        printf("Parsing the input string '%s'\n", input);
        char *token = strtok(input, " ");
        while(token) {
            puts(token);
            token = strtok(NULL, " ");
        }
    
        printf("Contents of the input string now: '");
        for(size_t n = 0; n < sizeof input; ++n)
            input[n] ? printf("%c", input[n]) : printf("\\0");
        puts("'");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      相关资源
      最近更新 更多