LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-23-2004, 07:43 PM   #1
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Rep: Reputation: 15
C++ with system commands


Hello everyone,
I am trying to write a program in c++ that will take in unix commands from the command line and from there it will split (using fork() ) for each command after that and then using pipes, will pass the data to the next command (using pipe() ). For example, I want to be able to type:

$ a.out "ls" "grep character" "sort"

and from there my program will take in each command, do them, then pass their input on to the next command... only I'm a little lost while doing this. I have been reading that I can use execlp but all the documentation on that is extremely confusing, any ideas?? and as always, thanks for your time.

-AquamaN

Last edited by AquamaN; 03-23-2004 at 07:45 PM.
 
Old 03-23-2004, 08:25 PM   #2
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Original Poster
Rep: Reputation: 15
This is how I was thinking of doing it so far... Not sure if it is the best way...

Code:
#include <iostream.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
	int x, y, pid, pipeName[2];
	char * argument;
	y = 1;
	
	for(x = 1;  x <= argc; x++)
	{
		pid = fork();
		
		if(pid == 0) /* child */
		{
			argument = arg[y];
			y++;
			y++;  //these are there so it gets odd numbered commands
			//execute command here

			//pass data to parent with pipe
		}
		else  /* parent */
		{
			y++;      // this is there so it gets the even numbered commands
			argument = arg[y];
			y++;

			//recieve data with pipe

			//execute command with child date
		}
	}
Thanks again!
 
Old 03-24-2004, 01:19 AM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
This interested me so I went and wrote it myself. As usual the devil was in the details; the most annoying part turned out to be getting the initial process to wait until the others had finished before exiting (otherwise, you get the shell prompt followed by output, which is just plain ugly )

It's in my cvs thingy at http://alf.hopto.org/cgi-bin/viewcvs.cgi/pipe/
 
Old 03-24-2004, 01:29 AM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
That could be too much of a spoiler, or it could be difficult to understand -- ask more specific questions and I can answer.

As you can see, I felt recursion was a more natural solution than what you posted: handle running the first command, and pipe its output into the same function which is now told to run the rest of the commands. Break when you get to the last command.

It could be that going backwards, that is having the first process run the last command in the line, would have avoided the timing problem I had with the process exiting and dumping the prompt before all the output prints.
 
Old 03-24-2004, 02:05 AM   #5
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by aluser
That could be too much of a spoiler, or it could be difficult to understand -- ask more specific questions and I can answer.

As you can see, I felt recursion was a more natural solution than what you posted: handle running the first command, and pipe its output into the same function which is now told to run the rest of the commands. Break when you get to the last command.

It could be that going backwards, that is having the first process run the last command in the line, would have avoided the timing problem I had with the process exiting and dumping the prompt before all the output prints.
woah.... that is truly impressive. A lot more than what I was looking for but I think I can learn from it. Thanks a lot aluser!!! You are awesome!!

-AquamaN

P.s. Are you a programmer by trade?
 
Old 03-24-2004, 02:12 AM   #6
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Original Poster
Rep: Reputation: 15
Seriously, this is impressive, I keep on going over it and looking at how long it took you to write it, I'm astonished, seriously.

-AquamaN
 
Old 03-24-2004, 12:17 PM   #7
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Quote:
P.s. Are you a programmer by trade?
Lol nope, just a bored college student. I was familiar with how these calls work so it wasn't that big a deal : )


I just updated the source in cvs a little: it now uses the fact that descriptors are closed when a process exits to implicitly close the pipe() fd opened in do_it(). This saves a fork and some passing around of fd through function arguments.
 
  


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
rescue system commands jessj SUSE / openSUSE 2 05-27-2005 05:32 PM
c++ uses system to give commands to bash darkRoom Programming 2 03-10-2005 04:27 AM
System Related Commands shivprasad Linux - Certification 1 09-16-2004 08:08 AM
essential system commands? k4sh Slackware 9 11-24-2003 01:21 PM
System monitoring commands? AMDPwred Linux - General 6 09-25-2002 05:44 PM

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

All times are GMT -5. The time now is 02:05 AM.

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