Thread: fork and pipe

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    fork and pipe

    I need to use fork and pipe to read a text file. This is an assignment for a class.

    Basically, I need to make 4 subprocesses to each read a portion of the input file, do some processing and return a result to the parent process so it can aggregate the children's results.

    From what I understand, I should use fork to create the children, then pipe to tell each child which part of the file to do.

    My question is, how do I use pipe. How do I know which child process I am talking to?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tbarsness View Post
    My question is, how do I use pipe. How do I know which child process I am talking to?
    You create a pipe by calling pipe(). You do this before you fork(). You know which process you are talking to, because each process has its own pipe.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    Thanks, I will try fork() before pipe()

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tbarsness View Post
    Thanks, I will try fork() before pipe()
    No no, pipe() before fork(). After the fork() it is too late.

    And you actually need two pipes per process. Pipes are one-way, but you need two-way communication. So each process needs two pipes, one for reading, one for writing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-02-2009, 06:13 PM
  2. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 05:16 PM
  3. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  4. Fork - Pipe trouble
    By Markyboy in forum C Programming
    Replies: 4
    Last Post: 07-06-2008, 11:09 PM
  5. fork() and pipe()
    By scioner in forum C Programming
    Replies: 3
    Last Post: 06-13-2008, 11:32 PM