Thread: Reading stdout from pipe

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Reading stdout from pipe

    Hi there,

    I am trying to read stdout from a child process to parent using pipe. I am unable to do so. I would appreciate it if someone can point me in the right direction.

    Here's my sample program for basic input output:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char buffer[10];
    	printf("first:\n");
    	fscanf(stdin, "%s", buffer);
    	FILE *fwriter;
    	
    	fwriter = fopen("aaa.txt", "w");
    	fprintf(fwriter, "%s\n", buffer);
    	fclose(fwriter);
    	
    	printf("b=%s\n", buffer);
    	
    	exit(0);
    }
    Code:
    gcc simple.c -o sss
    The program that calls fork():
    Code:
    #include <signal.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    	char buffer[50];
    	int fd[2], fd2[2];
    	pipe(fd);
    	pipe(fd2);
    	pid_t pid = fork();
    	if(pid == 0) {
    		dup2(fd[0], STDIN_FILENO);
    		close(fd[0]);
    		close(fd[1]);
    		
    		dup2(fd2[1], STDOUT_FILENO);
    		close(fd2[0]);
    		close(fd2[1]);
    		
    		execlp("./sss", "./sss", 0);
    	}
    	else {
    		close(fd2[1]);
    		read(fd2[0], buffer, 50);
    		printf(buffer);
    		fgets(buffer, 50, stdin);
    		/*sprintf(buffer, "%s\n", "doesNotMatter");*/
    		close(fd[0]);
    		write(fd[1], buffer, 50);
    		close(fd[1]);
    		
    		printf("%s", buffer);
    		close(fd2[0]);
    		waitpid(pid, NULL, 0);
    	}
    	
    	return 0;
    }
    Code:
    gcc tp.c -o tp
    The output of tp:
    Code:
    user1@xmac:~/pipe_test2$ ./tp
    123qwe
    
    ^C
    user1@xmac:~/pipe_test2$
    What I would like : I want ./tp to print out "first:" before the prompt for input. Basically, it should emulate ./sss.

    Thanks in advance.
    Last edited by CashCow01; 05-12-2010 at 11:57 PM. Reason: Miss out a line.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    There are lots of things wrong with the posted code. The child duplicates stdin to fd[0] and stdout to fd2[1] followed by immediately closing 'em. The first printf() in parent is wrong and the scheduler decides as to which process (parent or child) gets put on the cpu first. Though the scheduler's decision can be influenced by sleep()'ing in the appropriate process.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirect stdout
    By GoLuM83 in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 04:17 PM
  2. stdout with JNI
    By GoLuM83 in forum C Programming
    Replies: 4
    Last Post: 12-14-2006, 01:27 PM
  3. forcing stdout of external program to be line-buffered
    By FreakCERS in forum C Programming
    Replies: 4
    Last Post: 09-17-2006, 12:46 PM
  4. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 12:55 AM
  5. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 11:41 AM