CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2007
    Posts
    2

    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.

  2. #2
    Join Date
    Jan 2003
    Posts
    615

    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

  3. #3
    Join Date
    Mar 2005
    Location
    Romania,Cluj-Napoca
    Posts
    1,073

    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

  4. #4
    Join Date
    Feb 2007
    Posts
    2

    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.

  5. #5
    Join Date
    Feb 2007
    Posts
    1

    Talking 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

  6. #6
    Join Date
    May 2003
    Posts
    424

    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
  •  





Click Here to Expand Forum to Full Width

Featured