1

I'm writting a C program in which I need some directory to be copied in the middle of the code. So I wrote this function, where I try to use fork and then execvp. However this code doesn't seem to enter pid==0, and is not less than 0 aswell. What can be wrong? I'm using minix if that matters

    void execCopy() {
    printf("I'm in execCopy\n");
    printf("ERROR 0: %s\n",strerror(errno));


    int pid = fork();

    if(pid < 0) {
        printf ("fork failed with error code= %d\n", pid);
        fprintf(stderr,"FORK error\n");
    }

    printf("ERROR 1: %s\n",strerror(errno));

    char *execArgs[] = { "cpdir", "-R", copy_path,paste_path, NULL };

    printf("Copy from %s to %s\n",copy_path,paste_path);

    if(pid == 0) {

        printf("I'm gonna exec\n");
        execvp("cpdir", execArgs);
        printf("I should never get here \n");

    }

    else {
        printf("I'm the father, going to return\n");
        printf("ERROR 2: %s\n",strerror(errno));

        return;
    }
}

OUTPUT

Dec 26 20:34:11 192 kernel: I'm in execCopy
Dec 26 20:34:11 192 kernel: ERROR 0: Not a directory
Dec 26 20:34:11 192 kernel: ERROR 1: Not a directory
Dec 26 20:34:11 192 kernel: Copy from /./home to /./home/lcom
Dec 26 20:34:11 192 kernel: I'm the father, going to return
Dec 26 20:34:11 192 kernel: ERROR 2: Not a directory
Mr. Phil
  • 1,099
  • 2
  • 14
  • 31
  • 1
    Why you are calling strerror(errno) without letting change the value of errno – µtex Dec 26 '14 at 20:00
  • That `errno` is already set to `ENOTDIR` as soon as you *enter* this doesn't bode well from the outset. – WhozCraig Dec 26 '14 at 20:02
  • Have you tried to `printf("PID: %d\n", pid);`? Especially note the use of `\n` which outputs the line just assembled. Thus would lead to a better readable output *and* to output at all without `fflush()`. – glglgl Dec 26 '14 at 22:53
  • @WhozCraig I know, but I honestly have no idea where that comes from. BUt could it be related to this problem in particular? thanks for your help – Mr. Phil Dec 29 '14 at 19:36
  • @glglgl Hi, the pid was 756 and once again I couldn't see any output from the child. Thanks for your help – Mr. Phil Dec 29 '14 at 19:40
  • @sas not sure if I understood what you asked, but if it was why I called sterror in the beginning of the function it was because I suspected the error came from before, as it turned out to be the case – Mr. Phil Dec 29 '14 at 19:41

1 Answers1

2

output buffering might be swallowing the child process output. Try to fflush(stdout) before the exec.

Edit: after the fork, you should see two ERROR 1 and two Copy from lines. You're not seeing any of the child process output.

Andras
  • 2,995
  • 11
  • 17
  • I tried to flush before the "printf("I'm gonna exec\n");" as you explained but I'm still not seeing any output...Did I misunderstand where to write that line? Also is there any way to verify if the exec is executing correctly? Because no copy is being made whatsoever. Thanks for the effort – Mr. Phil Dec 29 '14 at 19:29
  • Also using \n in the end of each print should do the job, not needing to flush, am I correct? I added \n at the end of each printf and still getting only 1 process after fork – Mr. Phil Dec 30 '14 at 14:57
  • try the fflush right after the "Copy from" printf (and before the pid == 0 if). Yes, stdout is traditionally line buffered, but minix might be different. To check whether the fork itself worked, insert a `write(1, "forked\n", 7)` right after the fork. It should print twice (parent + child). – Andras Dec 31 '14 at 16:16