Thread: Fork - Pipe trouble

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    Fork - Pipe trouble

    Hy. I want to create a program that uses the TEE command and instead of sending what I type to 1 file.txt, I want to make it send to 3 separate files. t1.txt and t2.txt are ok. But somewhere along the way I loose the info for t3.txt.

    by the way TEE sends the STD_IN to STD_OUT and File (argument).

    Can you point me in the right direction ?
    Thanks

    Code:
    marktee()
    {
    	pid_t pid;
    	int i;
    	int pi[2];
    	int fi[2];
    	int status;
    	
    	pipe(pi);
    	pipe(fi);	
    	
    	for(i=0; i<2 && (pid=fork()); i++);
    	
    	if(i==0 && pid==0)
    	{
    	printf("Child 1\n");
    	close(fi[0]);
    	
    	dup2(pi[1],1);
    	close(pi[1]);
    	execlp("/usr/bin/tee","tee","t1.txt",0);
    	}
    	
    	if(i==1 && pid==0)
    	{
    	printf("Child 2 \n");
    	//close(pi[1]);
    	close(fi[0]);
    	dup2(pi[0],0);
    	dup2(fi[1],1);
    	close(fi[1]);
    	execlp("/usr/bin/tee","tee","t2.txt",0);
    	}
    
    	else
    	{
    	printf("Parent\n");
    	close(pi[0]);
    	close(pi[1]);
    
    	close(fi[1]);
    	dup2(fi[0],0);
    	execl("/usr/bin/tee","tee","t3.txt",0);
    
    	wait(&status);
    	
    	}

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    [By the way, from what I feel, the right place for this thread would be in the "Linux programming section". ]

    From what I understand, you are trying to output the content of the standard input into 3 different files, isn't it ? Then, is this solution overly complex ? In fact, I'm not sure to understand what you are trying to do with this scheme. Could you explain more what the goal is ? And I see some potential errors, but I would need to check the documentation to be sure if it's errors or if it's only me.

    Beside, you know that tee can takes multiple files arguments ?
    I hate real numbers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    practice

    This is actually an exercise. I'm trying to complete it. Its purpose is to familiarise ourselves with pipes connecting diferent child processes. So in this case, we gotta use the TEE, and make each child process write to a diferent file. 3 TEEs runnign at the same time, isn't enough of course because only 1 picks the STD_IN at a time.

    My plan was to make child 1 write to pipe pi, and child 2 receive, and write to pipe fi, for parent to receive. I think the error lies here:

    dup2(fi[1],1);

    In child 2, but I'm not quite sure how to fix it.

    Thank you.

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I think the error lies here:

    dup2(fi[1],1);
    Well, I wouldn't say so. Your pipe setup looks correct, as far as I can see. But you should close file descriptor before using them as the second argument of dup2. It won't hurt anyone, even if it may not be a problem in your case. Also, if you are on a 64-bit OS, you should definitely cast 0 to void*/char* for the last parameter to execlp/execl.

    On a side note, your calls to close() doesn't make a lots of sense... why are you closing some file descriptor while leaving some open ? I mean, close all of the useless file descriptor or close no one explicitly, but don't close only "some". This is just polluting your code (my opinion).

    I didn't test your code. Check the return value of every function call you make (if you want to do it fast, use the assert macro). It will at least give you some information.
    I hate real numbers.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your indentation sucks.

    Also, what's this doing?
    for(i=0; i<2 && (pid=fork()); i++);

    I also thought tee had more command line options than that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-02-2009, 06:13 PM
  2. fork() and pipe()
    By scioner in forum C Programming
    Replies: 3
    Last Post: 06-13-2008, 11:32 PM
  3. fork and pipe
    By tbarsness in forum C Programming
    Replies: 3
    Last Post: 10-22-2007, 12:40 PM
  4. Pipe(): Interprocess or Intraprocess comm?
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 07:27 PM
  5. Having trouble with a named pipe
    By crazeinc in forum C Programming
    Replies: 2
    Last Post: 05-13-2005, 01:00 AM