ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
}
}
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 )
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.
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!!
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.