|
-
February 10th, 2007, 06:55 PM
#1
Forking Processes Help!
Need a start on how to tackle this, thanks in advance!
Write a simple shell-like program that reads user commands and executes them using a combination of "fork", "exec", and "pipe".
history | head -10 | tail -5
Print the 6th through the 10th history commands. Your shell will have to set up a loop to create three processes and link them together using pipes so that the first child process runs "history" (and writes its output to a pipe), the second child process run "head" (with argument "-10"), reads from its standard input (which is the pipe into which the first child process writes) and writes to a pipe from which the third child process that runs "tail", reads. "tail" writes its output to the standard output.
The program has to take into account Pipes | and input and output redirection <, >. There are presumably no wrong inputs. i.e. the commands are all linux commands. Also the pipes are not finite meaning that input can be any number of pipes or redirections
Any advice would be greatly appreciated!
Last edited by RickRoss; February 10th, 2007 at 06:58 PM.
-
February 11th, 2007, 07:21 AM
#2
Re: Forking Processes Help!
My advice is that you should make an effort yourself and post here if you have more specific question(s).
Before post, make an effort yourself, try googling or search here.
When posting, give a proper description of your problem, include code* and error messages.
*All code should include code tags
-
February 11th, 2007, 08:07 AM
#3
Re: Forking Processes Help!
Here are some reference examples :
FORK
Exec
PIPES
Good luck,
Gili
Please use code tags [code] [/code]
We would change the world, but God won't give us the sourcecode.. 
Undocumented futures are fun and useful....
_________
Gili
-
February 12th, 2007, 05:38 PM
#4
Re: Forking Processes Help!
Okay
I need to read in an input line something like
ls -l | sort - n -r | head
I have a struct to do this, something like:
struct getCommand
{
string command;
vector < string > args;
int numPipes;
};
So in my main() I will need to read the first string ("ls" in this example) and store it as a command string, then i need to store the arguments of that command (everything leading up to a '|' character) into a vector so I can call execvp on them later in the program. And then of course I need to parse to count the number of Pipes in the input.
The catch is, I need to loop this until '\n' reading any number of commands, args and pipes and I'm not sure on the syntax to do this exactly.
Thanks for help in advance
Last edited by RickRoss; February 12th, 2007 at 05:41 PM.
-
February 18th, 2007, 11:07 PM
#5
Re: Forking Processes Help!
Dear Mr. Ross,
I feel that you will find the following steps helpful:
1) in parent, pipe ur 2 element p1 and p2, close the write end of the p1[1]
2) fork
3) in child, dup2 ur p1 for reading and dup2 ur p2 for writing
4) execute ur command
5) close both ends of ur p1 and ur p2 in child then kill your child!
then..
6) in parent process, close ur p2 write
7) stick it up u face and shake it shake it!! u make me sick! learn how to code before you flex nuts. read a book or something.
Cordially yours,
Dr. Stff in u FACE
P.S. I am gay
-
February 19th, 2007, 11:10 PM
#6
Re: Forking Processes Help!
It's good to see more and more people programming for Linux and other operating systems besides M$ Windows.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|