my pipe exits from shell,

Jun 10, 2015 at 7:45pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
      //implement single pipe
            int fd[2];
            pipe(fd);
            cout<<command_stream[2];
            pid_t pid=fork();

            //for(int pipe_count=0; pipe_count<num_pipes; pipe_count++)
            //for(int _count=0; _count<num_commands; _count++)
            //{


            //parent will write to child through pipe ex. ls | wc => ls writes to wc
            if(pid<0 )
                exit(EXIT_FAILURE);


            if(pid==0)
            {


                close(fd[0]);
                dup2(fd[1], 1);

                execlp(command_stream[0], command_stream[0],NULL);
                _exit(EXIT_SUCCESS);


            }

            else
            {

                int child1;

                wait(&child1);

                close(fd[1]);
                dup2(fd[0], 0);
                execlp(command_stream[2], command_stream[2],NULL);

            }


This is part of my shell I wrote...

it is exiting on execution and does not follow while(true) logic

command_stream has three values => ls | wc
Last edited on Jun 10, 2015 at 7:46pm
Jun 11, 2015 at 5:10pm
In your else that starts on line 30, you wait for your child process to exit, then exec another command. It will have nothing to talk to as the child process is already dead.

Also, check your return codes for ALL system calls, such as wait(), execlp(), etc.
Jun 12, 2015 at 1:11am
even deleting exit command does not work...
Jun 12, 2015 at 5:06pm
What does execlp return?
Jun 12, 2015 at 10:16pm
nothing
Jun 15, 2015 at 7:03pm
What "while True" are you talking about? And you didn't need to delete the exit call to fix your program -- Your child process has exited by the time your call to wait() returns!
Jun 16, 2015 at 3:07pm
closed account (NUj6URfi)

from dtscode:
1) Next time, please post a proper test case: http://eel.is/iso-c++/testcase/
2) execlp does in fact return something, but only under certain circumstances. It is probably best if you learn when it does
3) If you read the documentation, you would understand why your shell is exiting. Read this: http://linux.die.net/man/3/execlp and see if you can figure it out
Topic archived. No new replies allowed.