Pipe, Fork, and Exec - Two Way Communication Between Parent and Child Process

前端 未结 3 499
孤城傲影
孤城傲影 2021-02-10 07:01

An assignment in my Operating Systems class requires me to build a binary process tree by recursively calling exec on the same program. The goal is to split some arbitrary task

3条回答
  •  甜味超标
    2021-02-10 07:29

    Besides the issue mentioned by Jonathon Reinhart, most probably the call to execv() fails.

    To test this modify these lines

    execvp("two_way_pipes", argv2);
    _exit(0);
    

    to be

    ...
    #include 
    ...
    
    
    execvp("two_way_pipes", argv2); /* On sucess exec*() functions never return. */
    perror("execvp() failed); /* Getting here means execvp() failed. */
    _exit(errno);
    

    Expect to receive

    execvp() failed: No such file or directory
    

    To fix this change

    execvp("two_way_pipes", argv2);
    

    to be

    execvp("./two_way_pipes", argv2);
    

    Also if the child was not exec*()ed then this line

    read(PARENT_READ, buff, 4); // should read "test" which was written by the child to stdout
    

    fails and in turn buff is not initialised and therefore this line

    fprintf(stderr, "in parent | message received: %s\n", buff);  
    

    provokes undefined behaviour.

    To fix this at least properly initialise buff by changing

    char buff[5];
    

    to be

    char buff[5] = "";
    

提交回复
热议问题