1

I'm using pipes to generate a child process which reads a part from a file and send it through the pipe so the parent process can read it, and execute an extern program and redirect the stdout to a file.

The problem is that the stdout of the program that executes execv is not saved in my temporalFile.

1- Am I wrong using execv??(it I used before system function but the professor said that it was not performace was bad)

2- can be handle the stdout from execv ?

3-is there another alternative to execute an extern program passing parameters to the extern program and capture its stdout?

4-am I missing something in my code?

5-if I do a print, the stdout is capture by the temporalFile.

pd:I'm sorry my english is very bad.

int main()
{

int     fd[2];
pid_t   childpid;
char    readbuffer[80];

pipe(fd);

if((childpid = fork()) == -1){
        perror("error at fork");
        exit(1);
}

if(childpid == 0){
/* Child process closes up input side of pipe */
    close(fd[0]);

    FILE* archEntry=fopen("datosDeEntrada.txt","r");

    char string[3];

    fseek(archEntry,4,SEEK_SET);

    fread(string,3,1,archEntry);
    /* Send "string" through the output side of pipe */

    write(fd[1], string, (strlen(string)+1));

    exit(0);
}

else{
    /* Parent process closes up output side of pipe */

    close(fd[1]);

    FILE* fdArch=fopen("temporalFile.txt","w");

    dup2(fileno(fdArch),STDOUT_FILENO);

    /* Read in a string from the pipe */

    read(fd[0], readbuffer, sizeof(readbuffer));


    char *argv[]={ "workspace/project/Debug/./externProgram ", readbuffer, NULL};

    execv(argv[0],argv);


    fclose(fdArch);

}

return 0;

}

  • 2
    Usually you call one of the `exec` functions in the child, not in the parent like here. I don't see why you would choose that way. If I understand your program correctly, you read from a file in the child, send that data through a pipe to the parent, then pass that data as an argument to the executed program. This also seems unnecessarily complicated. Why not read the data first, then fork and exec – and avoid using a pipe altogether? – Andreas Bombe May 17 '15 at 22:44
  • you mean read before to do fork, but it would open twice the file, I understand that fork clones the entire program. Otherwise, if I do that way would be redirect the stdout the program into the temporalFile, would exist any change? – user4910023 May 17 '15 at 22:50
  • I mean, I do the exec in my child process, and after that in my parent process I redirect the stdout to the temporalFile? – user4910023 May 17 '15 at 22:57
  • No, you'd have to do the redirect after the fork before you do the exec, so that the other process isn't affected. But the whole pipe thing here is superfluous, since you could have just loaded the data before the fork. It's also not clear what it means that the output "is not saved" in the tempFile. Is it completely lost? Does it appear on the console instead? – Andreas Bombe May 17 '15 at 23:28
  • yes, it's lost, I mean when I do the dup2 I tell that the output is the file, but after the execution of the entire program, I open the temporalFile and nothing is written on it, where should be the output of the externalprogram execution. – user4910023 May 17 '15 at 23:38
  • the esternal program takes a parameter and does a printf, so when is executed in the exec, in the temporalFile should be written the print of the external program. – user4910023 May 17 '15 at 23:40
  • If the executed program exits cleanly, it should work. If it aborts, it might make a difference since `printf()` doesn;t flush its buffers on every new line like when it does when the output is a terminal. You should also check for errors, e.g. you don't know that `dup2` didn't fail for some reason. That's pretty much all I can think of right now. – Andreas Bombe May 18 '15 at 00:00
  • I didn't do it yet, I'd verify that, thanks for the help and time! – user4910023 May 18 '15 at 00:05

0 Answers0