1

コードの途中でディレクトリをコピーする必要があるCプログラムを書いています。そこで、fork を使用してから execvp を使用しようとするこの関数を作成しました。ただし、このコードは pid==0 に入っていないようで、0 未満でもありません。何が間違っている可能性がありますか?それが重要な場合、私はminixを使用しています

    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;
    }
}

出力

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
4

1 に答える 1

2

出力バッファリングが子プロセスの出力を飲み込んでいる可能性があります。fflush(stdout)exec の前に試してください。

編集: フォークの後、2 つの ERROR 1 と 2 つの Copy from 行が表示されます。子プロセスの出力は表示されません。

于 2014-12-26T22:25:11.353 に答える