繁体   English   中英

为什么子进程中的execlp不会将任何内容返回到stdout?

[英]Why does the execlp in child not return anything into stdout?

我有一个程序从文本文件中逐行读取。 每一行都有布局

command arg1 arg2 arg3

我已经读过它,所以我有2个数组,1个包含字符串,另一个指向每个字符串值。 例如

char read_in_line[128]
char* command[100]

所以:

command[0] = command arg1 arg2 arg3
command[1] = command arg1 

等等

然后我将此命令数组作为使用fork和管道的函数的输入。 以下是此函数的片段,请注意它是在while循环中,它将在* cmd!= NULL时继续

void piping(char* cmd[100]{

else if(pid == 0){
//child does not need to read
close(thepipe[0]);
dup2(thepipe[1],1);
close(thepipe[1]);
execlp(*cmd,*cmd,NULL);

但是,这不会返回任何内容。 我的C程序编译时没有显示任何错误,但是在我的stdout中我看不到执行我发送到函数的任何命令。

编辑:

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

    #define BUFFERSIZE 128
    #define oops(m,x) {perror(m); exit(x);}

    void piping(char **cmd[BUFFERSIZE]){
    pid_t pid;
    int thepipe[2];
    int in = 0;
    //while there are still commands
    while (*cmd != NULL){
        pipe(thepipe);
        //fork error case
        if((pid = fork()) < 0)
          oops("cannot fork",1);

        //child
        if(pid == 0){
            //child does not need to read
            close(thepipe[0]);
            if(dup2(thepipe[1],1)== -1)
              oops("Error redirecting stdout",2);
            //duplication succesful can now close thepipe[1]
            close(thepipe[1]);
            //execute the command
            execvp(*cmd[0], *cmd);
            exit(-1);
        }

        else{
          //parent does not write to pipe
          close(thepipe[1]);
          //setting up parent input to read from the pipe
          dup2(thepipe[0],0);
          close(thepipe[0]);

            //wait until child finishes
            wait(NULL);
            cmd++;
        }
    }
}

int main(int argc, char* argv[]){

    char **command[BUFFERSIZE];
    char read_in_line[BUFFERSIZE];

    int i = 0;
    int counter =0;
    int counter2 =0;
    //reading in line by line until end of file is reached
    FILE* fp = fopen("test.txt","r");
    while( fgets(read_in_line, BUFFERSIZE, fp) != NULL ){
        int j = 0;
        //setting up memory for arguments given that we know there is a max
        //of 10 arguments per line
        char **arguments = (char**) calloc(16, sizeof(char*));
        command[i] = arguments;
        //Will break up the line read in when a newline is argument resulting in one
        //string containing the commands and arguments
        //this string will then be broken up every time a space is met so that
        //commands and arguments can be seperated, and saved to command[i][j]
        char *t = strtok(read_in_line, "\n");
        char *argument = strtok(t, " ");
        command[i][j] = strdup(argument);

        while(argument != NULL){
            argument =strtok(NULL, " ");

            if(argument != NULL){
                command[i][++j] = strdup(argument);
            }
        }
        i++;

    }


    piping(command);

    return (0);
}

以下程序按预期工作:


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>


int main(void)
{
int rc;

rc=execlp("/bin/date", "deet", (char*) NULL);

printf("Rc=%d,%d(%s)\n", rc, errno, strerror(errno));

return 0;
}

下一步:添加一些参数。 (下一步:修理管道)


rc=execlp("/bin/ls", "ls", "-li", (char*) NULL);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM