LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2012, 10:24 PM   #1
Leumash
LQ Newbie
 
Registered: Nov 2012
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 11-14-2012, 03:10 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Why do you have to call fork three times for each process? Why not once?
 
Old 11-14-2012, 05:24 PM   #3
Leumash
LQ Newbie
 
Registered: Nov 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
I meant that for each process, there being three, I have to call three forks.
 
Old 11-15-2012, 05:28 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
ok - then sounds good.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux piping vs Windows piping emanabela Linux - Newbie 1 05-10-2009 12:58 PM
Systems Programming - Linux vs Windows dipperdan Programming 8 09-30-2004 08:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration