Sponsored Content
Full Discussion: Reading stdout from pipe
Top Forums Programming Reading stdout from pipe Post 302422100 by shamrock on Monday 17th of May 2010 12:37:32 PM
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;
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PIPE(2) 						     Linux Programmer's Manual							   PIPE(2)

NAME
pipe, pipe2 - create pipe SYNOPSIS
#include <unistd.h> int pipe(int pipefd[2]); #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <unistd.h> int pipe2(int pipefd[2], int flags); DESCRIPTION
pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe. For fur- ther details, see pipe(7). If flags is 0, then pipe2() is the same as pipe(). The following values can be bitwise ORed in flags to obtain different behavior: O_NONBLOCK Set the O_NONBLOCK file status flag on the two new open file descriptions. Using this flag saves extra calls to fcntl(2) to achieve the same result. O_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the two new file descriptors. See the description of the same flag in open(2) for reasons why this may be useful. RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately. ERRORS
EFAULT pipefd is not valid. EINVAL (pipe2()) Invalid value in flags. EMFILE Too many file descriptors are in use by the process. ENFILE The system limit on the total number of open files has been reached. VERSIONS
pipe2() was added to Linux in version 2.6.27; glibc support is available starting with version 2.9. CONFORMING TO
pipe(): POSIX.1-2001. pipe2() is Linux-specific. EXAMPLE
The following program creates a pipe, and then fork(2)s to create a child process; the child inherits a duplicate set of file descriptors that refer to the same pipe. After the fork(2), each process closes the descriptors that it doesn't need for the pipe (see pipe(7)). The parent then writes the string contained in the program's command-line argument to the pipe, and the child reads this string a byte at a time from the pipe and echoes it on standard output. #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (argc != 2) { fprintf(stderr, "Usage: %s <string> ", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]); /* Close unused write end */ while (read(pipefd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, " ", 1); close(pipefd[0]); _exit(EXIT_SUCCESS); } else { /* Parent writes argv[1] to pipe */ close(pipefd[0]); /* Close unused read end */ write(pipefd[1], argv[1], strlen(argv[1])); close(pipefd[1]); /* Reader will see EOF */ wait(NULL); /* Wait for child */ exit(EXIT_SUCCESS); } } SEE ALSO
fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2010-09-10 PIPE(2)
All times are GMT -4. The time now is 08:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy