Reading stdout from pipe


 
Thread Tools Search this Thread
Top Forums Programming Reading stdout from pipe
# 1  
Old 05-13-2010
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 Parker_; 05-13-2010 at 02:57 AM..
# 2  
Old 05-14-2010
try popen as a start

---------- Post updated at 02:07 PM ---------- Previous update was at 01:09 PM ----------

try this, you only need ONE pipe.

Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

#define LEN 512
int main()
{
        char buffer[LEN];
        int fd[2];
        pipe(fd);

        pid_t pid = fork();
        if(pid == 0) {
                dup2(fd[1], STDOUT_FILENO);
                close(fd[0]);

                execlp("./sss", "./sss", NULL);
        }
        else {
                dup2(fd[0], STDIN_FILENO);
                close(fd[1]);

                while (fgets(buffer, LEN, stdin)) {
                    printf("%s", buffer);
                }
                waitpid(pid, NULL, 0);
        }

        return 0;
}


Last edited by bigearsbilly; 05-14-2010 at 09:10 AM.. Reason: haven't checked properly
# 3  
Old 05-17-2010
Hi bigearsbilly,

Thanks for your reply. I need a bi directional pipe or rather two pipes where each pipe represents a flow of information.
# 4  
Old 05-17-2010
Even if you need a bidirectional pipe there is no point in setting up 2 pipes. From what you have posted just 1 pipe is enough. In the child process you are making a basic mistake by closing fd2[0] after redirecting standard output to it...
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);  /* just 1 pipe is enough...no need for 2 pipes */
	pid_t pid = fork();
	if(pid == 0) {
		dup2(fd[0], STDIN_FILENO);
		close(fd[0]);  /* no need redirecting stdin because the child isnt getting any external input */
		close(fd[1]);
		
		dup2(fd2[1], STDOUT_FILENO);
		close(fd2[0]);  /* this also doesnt make sense but you dont need 2 pipes */
		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;
}

If you need "./tp" to print out "first:" then you need to flush std output in the "./sss" program otherwise it wont appear in the reading end of the pipe until the output block buffer gets filled up...
Code:
#include <stdio.h>

int main()
{
	char buffer[10];
        printf("first:\n");
        fflush(stdout);
	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);
}

You have too much "not needed" stuff going on in your "./tp" program which should all really be...
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
        char buffer[50];
        int status;
        int fd[2];
        pipe(fd);
        pid_t pid = fork();

        if (pid == 0) {
                dup2(fd[1], STDOUT_FILENO);
                execlp("./sss", "./sss", 0);
        }
        else {
                close(fd[1]);
                dup2(fd[0], STDIN_FILENO);

                read(fd[0], buffer, 8);
                printf("%s", buffer);

                read(fd[0], buffer, 50);
                printf("%s", buffer);

                waitpid(pid, NULL, 0);
                close(fd[0]);
        }

        return 0;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

2. UNIX for Dummies Questions & Answers

STDout

Hi, I have a program set to read in a text file, change certain characters and then print the altered version to the screen but does anyone know how to save the new version as another text file? And, if possible, how to specify the file name, and perhaps location? Thanks! (2 Replies)
Discussion started by: PerlNutt
2 Replies

3. UNIX for Dummies Questions & Answers

Reading a pipe delimited file

Hi Guys, i am reading a pipe delimited file using awk command. I have tested the gawk separately. it was fine. But when i execute the script. i am getting the following error saying command not found. Can somebody point out as what i am doing wrong. Cheers!!! (3 Replies)
Discussion started by: mac4rfree
3 Replies

4. Shell Programming and Scripting

awk reading from named pipe (fifo)

I'm trying to read a fifo using awk and comming across some problems. I'm writing to the fifo from multiple processes invoked by GNU Parallel: mkfifo my_fifo awk '{ a = a + $2 } END { for (i in a) print i, a }' my_fifo | sort -nk1 > sorted_output grep -v '^@' massive_file | parallel... (3 Replies)
Discussion started by: nathanhaigh
3 Replies

5. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

6. Shell Programming and Scripting

Reading a pipe delimited file and loading teradata

Hi, I am new to UNIX shell scripting (KSH). I have a file in UNIX server which is delimited by |. I require the basic syntax to: read the file move it to a local variables load a database - Teradata Continue until end of file. Note: if the field is have no value inbetween two... (6 Replies)
Discussion started by: shivacbz
6 Replies

7. Programming

C++ How to use pipe() & fork() with stdin and stdout to another program

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question:... (2 Replies)
Discussion started by: vvaidyan
2 Replies

8. Programming

reading reading data from webpage

hi iam reading data from web page using request socket and curl socket. now my problem is some the web page containg data as a image so how can i read the data from a image. thank,inadvance. sree (3 Replies)
Discussion started by: phani_sree
3 Replies

9. Shell Programming and Scripting

Reading from blocking fifo pipe in shell script

Hi!! I have a problem reading from a fifo pipe in shell script. The idea is simple, I have a C program with two pipe files: An input pipe I use to send commands in shell script to the C program (echo "command" > input.pipe) An output pipe that I read the result of the command also in... (4 Replies)
Discussion started by: victorin
4 Replies

10. UNIX for Dummies Questions & Answers

pipe and stdout

Hi there I know that this isn't the place to put a question like this i supose, but i'm geting desperated. I have searched in interbase forums and nothing helped. If anyone can help me i would apreciated. I'm using HP-UX with a version 3 of interbase wich means i can't split the backup file into... (3 Replies)
Discussion started by: vascobrito
3 Replies
Login or Register to Ask a Question