LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ Systems Programming Linux Piping (https://www.linuxquestions.org/questions/programming-9/c-systems-programming-linux-piping-4175437029/)

Leumash 11-13-2012 10:24 PM

C++ Systems Programming Linux Piping
 
Hello, I'm a student at a university currently taking a course in Software Construction.

For an assignment I had to turn in I had to implement a subset of shell, my_shell. For my_shell I had to implement multiple piping, such as the bash shell command:

ls | grep cpp | sed 's/cpp/h/'

I'll be using the above as an example, but I should be able to do as many pipes as I want. i.e. echo hello | cat | cat | cat | cat | cat ... etc.

Now I figured out that I have to call fork three times for each ls, grep, and sed. So to do this, I implemented a piping() function.

For the first program, ls, I only have to redirect stdout which I did before fork/exec'ing.

Then for the middle, I implemented a loop, because for everything in between (grep, and could be more) I have to redirect stdin and stdout and then fork/exec.

Finally, for the last program, sed in this case, I only have to redirect stdin and then fork/exec.

void piping(cstring of arguments)
{
//first piping (ls, first process)
pipe(ints)
if(!fork())
{
..redirect stdout
}
//I don't wait

//second piping (grep, etc.)
while(another pipe exists)
{
..if(!fork())
..{
....redirect stdout, stdin
..}
//I don't wait for ANY of these
}

//Last piping (sed, last process)
if(!fork())
{
..redirect stdin
}
else
{
..if(!&)
....wait()//conditional wait
}
}

Above is pretty much pseudo code of how I implemented my piping. First off, is this idea correct?

Secondly, and I am very confused on this point, is how I'm supposed to treat each program before the last one (ls and grep before sed). The way I implemented the multi-piping is by NOT waiting for anything except the last program. So I fork/exec w/o waiting for ls and grep. Finally if there's no &, then I wait for the last program, sed. If there is an &, then I just don't wait for sed either.

I talked to my professor about this, and he gave me a fuddled answer like, "I wouldn't have done it that way." However, that's not saying that it's wrong. He said he would have done his piping by having the child fork every extra process. I don't see how this is feasible. As you can see, the parent forks every child, so the depth of children is only ever 1. If I wanted to make a process group, how else is that possible unless my_shell is the one who forks all the children?

tl;dr My parent (my_shell) forks every process of a pipeline and doesn't wait for any except the last program, is this correct?

I've already turned this assignment in but it's driving me up the wall on how to correctly implement multiple pipes. As for this week, I have to implement job control, hence why I don't see how that's possible unless the process group is made by my_shell where my_shell forks every process itself.

Sorry for the verbose explanation, and thanks.

smallpond 11-14-2012 03:10 PM

Why do you have to call fork three times for each process? Why not once?

Leumash 11-14-2012 05:24 PM

I meant that for each process, there being three, I have to call three forks.

smallpond 11-15-2012 05:28 PM

ok - then sounds good.


All times are GMT -5. The time now is 06:25 AM.