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

前端 未结 3 492
孤城傲影
孤城傲影 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:23

    As Jonathon Reinhart says you should change this lines:

    char *argv2[] = {"some random arg to make sure that argc == 2 in the child", NULL};
    execvp("two_way_pipes", argv2);
    

    to:

    char *argv2[] = {argv[0], "some random arg...", NULL};
    execvp(argv[0], argv2);
    

    then it works as expected:

    echo test | ./two_way_pipes
    in parent | message received: test
    

    In your program you wrote "two_way_pipes" but it is not in your PATH so you really need the extra ./ so argv[0] then is ("./two_way_pipes").

提交回复
热议问题