Sponsored Content
Full Discussion: Problems understanding pipes
Top Forums Programming Problems understanding pipes Post 302562482 by ab_tall on Friday 7th of October 2011 12:14:13 PM
Old 10-07-2011
Problems understanding pipes

I am trying to create a csh clone, but am having problems implementing piped commands. Specifically, the below code simply hangs after input of ls | grep <text>
It does however filter the output and display it correctly, but it appears that grep hasn't exited and my shell never comes back to the waiting parent.
I am having doubts if I am using the pipe correctly.
Following is an extract of the relevant code(pseudo):
Code:
save stdin stdout and stderr using dup.
For every command
{
    int fd[2];
    if(not last command in pipe)
    {
        if(count ==0)
        {
            if(pipe(fd_pipe) < 0)
            {
                perror("pipe");
            }
 
        }
 
 
        if(dup2(fd_pipe[1], STDOUT_FILENO) <0)
        {
            perror("pipe");
        }
        if(dup2(fd_pipe[0], STDIN_FILENO) <0)
        {
            perror("pipe");
        }
        ++count;
    }
}
else if(last command)
{
    char line[100];
 
    if(dup2(fd_pipe[0], STDIN_FILENO) <0)
    {
        perror("pipe");
    }
    if(dup2(bstdout, 1) <0)
    {
        perror("pipe");
    }
}
Execute the command in subshell(fork and exec)
}
restore stdin out and stderr.

---------- Post updated at 03:37 AM ---------- Previous update was at 03:35 AM ----------

AArgh...Have cracked my head till 4: 30 am on this, but can't figure out what's wrong.
i tried using a separate file as an intermediate storage for the pipe output.
But then grep fails to filter the input from the file.

Moderator's Comments:
Mod Comment Moved to a more fitting forum


---------- Post updated at 12:14 PM ---------- Previous update was at 04:37 AM ----------

Anyone there?

To the moderator who moved the post...seems like moving it to a more "fitting" forum killed the chances of a reply Smilie

Last edited by pludi; 10-07-2011 at 11:24 AM..
 

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
PIPE(2) 						      BSD System Calls Manual							   PIPE(2)

NAME
pipe -- create descriptor pair for interprocess communication SYNOPSIS
#include <unistd.h> int pipe(int fildes[2]); DESCRIPTION
The pipe() function creates a pipe (an object that allows unidirectional data flow) and allocates a pair of file descriptors. The first descriptor connects to the read end of the pipe; the second connects to the write end. Data written to fildes[1] appears on (i.e., can be read from) fildes[0]. This allows the output of one program to be sent to another pro- gram: the source's standard output is set up to be the write end of the pipe; the sink's standard input is set up to be the read end of the pipe. The pipe itself persists until all of its associated descriptors are closed. A pipe whose read or write end has been closed is considered widowed. Writing on such a pipe causes the writing process to receive a SIGPIPE signal. Widowing a pipe is the only way to deliver end-of-file to a reader: after the reader consumes any buffered data, reading a widowed pipe returns a zero count. The generation of the SIGPIPE signal can be suppressed using the F_SETNOSIGPIPE fcntl command. RETURN VALUES
On successful creation of the pipe, zero is returned. Otherwise, a value of -1 is returned and the variable errno set to indicate the error. ERRORS
The pipe() call will fail if: [EFAULT] The fildes buffer is in an invalid area of the process's address space. [EMFILE] Too many descriptors are active. [ENFILE] The system file table is full. SEE ALSO
sh(1), fork(2), read(2), socketpair(2), fcntl(2), write(2) HISTORY
A pipe() function call appeared in Version 6 AT&T UNIX. 4th Berkeley Distribution February 17, 2011 4th Berkeley Distribution
All times are GMT -4. The time now is 08:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy