Sponsored Content
Full Discussion: Problems understanding pipes
Top Forums Programming Problems understanding pipes Post 302562623 by Corona688 on Friday 7th of October 2011 12:52:35 PM
Old 10-07-2011
Some of us might be in a different timezone than you, and this forum is populated by volunteers, we are not "on call".

You also agreed to not bump posts when you registered.

I can't tell if your pseudocode's wrong or not since you didn't mention fork() at all in there.

Writing up some pseudocode for you.

---------- Post updated at 10:52 AM ---------- Previous update was at 10:41 AM ----------

The read-end won't EOF until all of the write-ends are closed, and the write-end won't die with SIGPIPE until all of the read-ends are closed, which is usually why this hangs: Forgetting to close all ends of the pipe you weren't using.

Each process you fork() gets independent copies of any pipe FD's that were open when you fork()ed, any unused ones need to be closed separately.

Also, you need to close the write-end too, once you're done with it, for the reader to hit EOF. Or the writer quitting works, too.
Code:
# parent gets writing-end of pipe, child gets reading-end of pipe.
int pipefd[2];

pid_t pid;
pipe(pipefd);

pid=fork();

if(pid < 0)
{
        perror("couldn't fork");
        exit(1);
}
else if(pid == 0) // in child
{
        dup2(pipefd[0], 0); // Overwrite STDIN with read end of pipe
        // Close writing end of pipe!  ESSENTIAL!
        close(pipefd[1]);
        execlp("/bin/cat", NULL); // exec REPLACES the current process
        perror("Couldn't exec"); exit(1);
}

// If we reach here, we must be the parent
const char *str="the owls are not what they seem\n";
int status;
dup2(pipefd[1], 1); // overwrite STDOUT with write end of pipe
close(pipefd[0]); // close the read-end!  ESSENTIAL!
write(1, str, strlen(str));  // send data to cat
close(1); // close FD so the child will get EOF.  Also essential!  wait() would wait forever otherwise.
wait(&status);
fprintf(stderr, "Returned status %d\n", WEXITSTATUS(status));

This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies

2. Shell Programming and Scripting

cd using pipes

Hi, Can the cd command be invoked using pipes??? My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Discussion started by: Sinbad
2 Replies

3. UNIX for Advanced & Expert Users

FIFO Pipes

Hi...Can anyone please guide me on FIFO Pipes in UNIX.I have lerant things like creating fifo pipes,using them for reads and writes etc.I want to know what is the maximum amount of memory that such a pipe may have? Also can anyone guide me on where to get info on this topic from? (4 Replies)
Discussion started by: tej.buch
4 Replies

4. UNIX for Advanced & Expert Users

Consolidating Pipes

This is something I've given a lot of thought to and come up with no answer. Say you have a data stream passing from a file, through process A, into process B. Process A only modifies a few bytes of the stream, then prints the rest of the stream unmodified. Is there any way to stream the file... (4 Replies)
Discussion started by: Corona688
4 Replies

5. Shell Programming and Scripting

named pipes

How to have a conversation between 2 processes using named pipes? (5 Replies)
Discussion started by: kanchan_agr
5 Replies

6. UNIX for Dummies Questions & Answers

learning about pipes!

im trying to figure out how to do the following: using pipes to combine grep and find commands to print all lines in files that start with the letter f in the current directory that contain the word "test" for example? again using pipes to combine grep and find command, how can I print all... (1 Reply)
Discussion started by: ez45
1 Replies

7. Shell Programming and Scripting

Problems understanding example code

I am really new to UNIX and programming in general and so apologies if this thread is a bit simple. I have searched and found a piece of sample code for a training program I am currently undertaking, but seeing as I am relatively new, I dont completely understand how it works. Here is the... (1 Reply)
Discussion started by: Makaer
1 Replies

8. UNIX for Dummies Questions & Answers

Problems understanding example code

I am really new to UNIX and programming in general and so apologies if this thread is a bit simple. I have searched and found a piece of sample code for a training program I am currently undertaking, but seeing as I am relatively new, I dont completely understand how it works. Here is the... (6 Replies)
Discussion started by: Makaer
6 Replies

9. Programming

Pipes in C

Hello all, I am trying to learn more about programming Unix pipes in C. I have created a pipe that does od -bc < myfile | head Now, I am trying to create od -bc < myfile | head | wc Here is my code, and I know I might be off, thats why I am here so I can get some clarification. #include... (1 Reply)
Discussion started by: petrca
1 Replies

10. Homework & Coursework Questions

Using Pipes and Redirection

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a pipe to show the number of people who are logged into the system right now. Create a pipe to show... (2 Replies)
Discussion started by: lakers34kb
2 Replies
POPEN(3)						     Library Functions Manual							  POPEN(3)

NAME
popen, pclose - initiate I/O to/from a process SYNOPSIS
#include <stdio.h> FILE *popen(command, type) char *command, *type; pclose(stream) FILE *stream; DESCRIPTION
The arguments to popen are pointers to null-terminated strings containing respectively a shell command line and an I/O mode, either "r" for reading or "w" for writing. It creates a pipe between the calling process and the command to be executed. The value returned is a stream pointer that can be used (as appropriate) to write to the standard input of the command or read from its standard output. A stream opened by popen should be closed by pclose, which waits for the associated process to terminate and returns the exit status of the command. Because open files are shared, a type "r" command may be used as an input filter, and a type "w" as an output filter. SEE ALSO
pipe(2), fopen(3S), fclose(3S), system(3), wait(2), sh(1) DIAGNOSTICS
Popen returns a null pointer if files or processes cannot be created, or the shell cannot be accessed. Pclose returns -1 if stream is not associated with a `popened' command. BUGS
Buffered reading before opening an input filter may leave the standard input of that filter mispositioned. Similar problems with an output filter may be forestalled by careful buffer flushing, for instance, with fflush, see fclose(3S). Popen always calls sh, never calls csh. 7th Edition May 15, 1985 POPEN(3)
All times are GMT -4. The time now is 07:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy