-1

I am making a test My task will be one child pipelining the output to the input of another another child. I am using ps -ef as the first command(by using execlp()) and grep root as the second command(bu using execlp()).I am getting the desired output but I am blocked in the process that is executing the grep.I have used top command to verify the current running processes.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>

int main(){
    int status,i;
    int fds[2];
    pid_t pid;
    //pid=fork();;
    if(pipe(fds)==-1){
        perror("Error:");
        exit(0);
    }
    if(fork()>0){
        printf("The pid of the parent is %d\n",getpid());
        if(fork()==0){

            printf("The PID of the 2nd child %d\n",getpid());
            //printf("Inside 2nd child\n");
            close(0);
            dup(fds[0]);
            close(fds[1]);
            //fcntl(fds[0],F_SETFD,FD_CLOEXEC);
            //printf("The PID of the 2nd child %d\n",getpid());
            if(execlp("grep","grep","root",NULL)==-1){
                perror("ERROR FROM SECOND CHILD: ");
                exit(1);
            }

        }
        else{
            printf("The parent is waiting\n");
            /*for(i=0;i<2;i++){
                printf("%d The PID OF THE EXIT CHILD is %d\n",i,wait(&status));
            }*/
            while(wait(NULL)>0)
                perror("Error From Main:\n");
            //wait(&status);
            printf("The parent exited from the waiting stage\n");
            return 0;
        }

        //while(1);
    }
    else{
        close(1);
        dup(fds[1]);
        close(fds[0]);
        //close(1);
        printf("The pid of the First child is %d\n",getpid());
        if(execlp("ps","ps","-ef",NULL)==-1){
            perror("Error:");
            exit(1);
        }

    }

}

please help me out.As I am on the process making a shell.The execution of this task is necessary.

8
  • 1
    You need all the fds (in all processes) on the writing end of the pipe to be closed for grep to see the end-of-file and terminate. Commented Aug 7, 2015 at 9:10
  • I didnt get you .At the writing end of the pipe I can close only the close(0)(stdin) and close(fds[0]) Commented Aug 7, 2015 at 9:20
  • You need all the processes that don't write to the pipe to close(fds[1]). You forgot one. Commented Aug 7, 2015 at 9:23
  • Yes its is the main function.But why do I need to close it in main function.Do m I lacking some basic concepts? Commented Aug 7, 2015 at 9:28
  • it is not working Commented Aug 7, 2015 at 9:38

1 Answer 1

1

The grep process was blocked because it was not getting the EOF from other write end of the pipe.EOF will be send to the reader end when associated write file(fds[1]) descriptors will be closed.My mistake was I didn't closed the write descriptors(fds[1]) from the parent process(main) though I have closed it from process which was writing to the pipe.

So the question may arise that do we need to close the descriptors even though we are not using it?? Yes yes!!beacuse the parent process and child processes has the same copy of the descriptors which are basically memory references(I am not sure).So even we close from one process the another copy might be open in another process which may block a third process that may be reading from the pipe.

I debugged by studing the file descriptors using cat /proc//.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.