Thread: Help with a simple C program!

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Question Help with a simple C program!

    Hello!
    I need some help on a simple C program I am making. The program is equivalent to this command:

    ps -ef | grep USER_NAME | wc

    So far... I've gotten the "ps -ef" and the "grep" to work via the pipe and fork. I can't get the "wc" to work for the life of me...

    Any help would be much appreciated!

    The .c file should be attached...
    Attached Files Attached Files

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Brandon Fornaro View Post
    So far... I've gotten the "ps -ef" and the "grep" to work via the pipe and fork.
    You really should just include your code within [CODE][/CODE] tags; I too almost skipped your post because I couldn't see the code right away. We humans are fickle creatures.

    Quote Originally Posted by Brandon Fornaro View Post
    I can't get the "wc" to work for the life of me...
    You're very close, actually.

    The two thins you need is a second pipe, say pfd2, just like the one you already have; and another fork() instead of the else you already have.

    Connect the write end of the new pipe (pfd2[1]) to the standard output (descriptor 1) of the grep process, and its read end ( pfd2[0]) to the standard input (descriptor 0) of the wc process.

    It's just about the same as your existing code, except that for the grep process, you'll connect one pipe to standard output and another to its standard input.

    If you wrote the code yourself, instead of just blindly copying it from someone/somewhere else, it should not be too difficult, just add another pipe, and extend your if-else chain by one step, adding another fork(). The fact that you attached the code instead of listed it right there in your message is quite suspicious, though.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Quote Originally Posted by Nominal Animal View Post
    You really should just include your code within [CODE][/CODE] tags; I too almost skipped your post because I couldn't see the code right away. We humans are fickle creatures.


    You're very close, actually.

    The two thins you need is a second pipe, say pfd2, just like the one you already have; and another fork() instead of the else you already have.

    Connect the write end of the new pipe (pfd2[1]) to the standard output (descriptor 1) of the grep process, and its read end ( pfd2[0]) to the standard input (descriptor 0) of the wc process.

    It's just about the same as your existing code, except that for the grep process, you'll connect one pipe to standard output and another to its standard input.

    If you wrote the code yourself, instead of just blindly copying it from someone/somewhere else, it should not be too difficult, just add another pipe, and extend your if-else chain by one step, adding another fork(). The fact that you attached the code instead of listed it right there in your message is quite suspicious, though.
    Haha thank you so much. That definitely helps. No, I did not take this from anyone, it is my own work. This is my first time on this site and was not sure if I should have attached my code to the post itself or as an attachment. Sorry about that. And thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM