Problems understanding pipes


 
Thread Tools Search this Thread
Top Forums Programming Problems understanding pipes
# 1  
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..
# 2  
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:
# 3  
Old 10-07-2011
@Corona Apologise if the bumping was excessive..
The fork is done as a part of the line, run the command in a subshell.
Thanks for the explanation though, let me ensure if I am correctly closing the ends of the pipe.

EDIT: Followup question though, is it necessary to use different pipes if there are more than 2 commands in my pipe?
Or can i reuse the same pipe?
say if I want to an ls | grep <pattern> | more.
Once ls is done with its task and grep done with the reading, the pipe would no longer be used right? so can the same be used between grep and more? [I think not..as grep would need access to 2 pipes simultaneously...one to read from and other to write to..in that case I need to address the correct file descriptors in my 2D array]

Last edited by ab_tall; 10-07-2011 at 02:25 PM..
# 4  
Old 10-07-2011
Quote:
Originally Posted by ab_tall
EDIT: Followup question though, is it necessary to use different pipes if there are more than 2 commands in my pipe?
Yes, make n-1 pipes. Writing an example...

---------- Post updated at 01:27 PM ---------- Previous update was at 01:11 PM ----------

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

int main(void)
{
        const char *cmd[]={"ls", "tac", "less", NULL};
        int curpipe[2]={-1, -1}, lastpipe=-1, n;
        pid_t pids[64];

        for(n=0; cmd[n] != NULL; n++)
        {
                // If this isn't the last in the chain,
                // make a pipe for cmd[n] to write into.
                if(cmd[n+1] != NULL) pipe(curpipe);

                switch(pids[n]=fork())
                {
                case -1:
                        perror("couldn't fork");
                        break;
                default:// parent code

                        // Our latest child has a copy of the writing
                        // end, we don't need it anymore.
                        if(curpipe[1] >= 0) close(curpipe[1]);

                        // ...but we'll need the reading end next time.
                        // and we should close the last loop's reading end.
                        if(lastpipe >= 0) close(lastpipe);
                        lastpipe=curpipe[0];

                        // don't use those FD's again.
                        curpipe[1]=-1;  curpipe[0]=-1;
                        break;

                case 0: // child code

                        // If we have a reading end, use it.
                        if(lastpipe >= 0)
                        {
                                dup2(lastpipe, 0);
                                close(lastpipe);
                        }

                        // if we have a writing end, use it.
                        if(curpipe[1] >= 0)
                        {
                                // make a copy.
                                dup2(curpipe[1], 1);
                                // close both ends.
                                close(curpipe[1]);
                                close(curpipe[0]);
                        }

                        execlp(cmd[n], cmd[n], NULL);
                        perror("couldn't exec");
                        exit(1);
                        break;
                }
        }

        for(n=0; cmd[n] != NULL; n++)
        {
                int status;
                wait(&status);
        }
}

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-07-2011
@Corona

That is one elegant piece of code. Initially, I was going to bug you with added clarifications on how the code was working, but then decided that after so much effort on your part, the least I could do was trace the code.

I did and it resolved those doubts Smilie (Big surprise!)

I also went to your projects page burningsmell, and it appears that I have been talking to one awesome programmer.
Keep up the good work! And Thanks!
P.S - Additional somewhat unanswered question:
Instead of creating n-1 pipes for n commands, would it be possible to use the same single pipe by suitable adjusting the Fds?
My initial attempt tried to do that, but I got confused as to what's to be closed and what not to be.
From your code I surmised, we need to close anything in the parent that we don't use. But a close() call => that the FD no longer refers to any file. So when the parent closes the write end of the pipe with say curpipe{5,6} , how is the child allowed to use 6 to refer to the write end of the pipe?{is it because, in its address space, 6 is not closed? }
Sorry if these questions sound too basic, but i am unable to clearly visualize the address spaces like that. I think we can debug the child process using gdb, but I am not very familiar debugging multiple threads.
# 6  
Old 10-07-2011
Note: In what follows, a "file description" and a "file descriptor" are not synonymous.

When you open() a file or use the pipe() system call, the kernel will create what's called a file description. This file description is a data structure that keeps track of the file offset, permissions, access mode, etc, associated with the opened resource. Aside from creating that file description, an entry is added to the process file descriptor table and you are given an integer index which points to that new entry; this is the file descriptor.

Both the file description and file descriptor tables are inside the kernel's address space. A file description is a system-wide entity. File descriptor tables are a per-process data structure. Each process has its own descriptor table. There can be multiple file descriptors pointing to the same underlying file description.

When you fork, the newly-created process is provided with its own copy of the parent's descriptor table. Initially, each entry in the child's descriptor table points to the same underlying open file description as its counterpart in the parent's table. The same is true when you exec() a new executable image, except that file descriptors which have had their close-on-exec flag set are closed.

An open file description is not closed until all file descriptors in all processes which point to that file description are closed.

Since different file descriptors in different processes can manipulate the same underlying file description, it can be considered a mode of interprocess communication.

That's probably a lot of jargon to digest at once, but I believe it covers the essentials.

Regards,
Alister

Last edited by alister; 10-07-2011 at 07:18 PM..
This User Gave Thanks to alister For This Post:
# 7  
Old 10-07-2011
@Thanks alister.

The clarification was very helpful indeed.
While we are on the subject, (and I know there are scattered resources available on google for this), how is the pipe underlying structure implemented?
If it is just another file, is it possible to see the contents of the pipe {so as to know what's being passed on between the read and write ends}.
Finally,
Is there a way to find out which FDs point to the same underlying description?
If it's hidden somewhere in lsof, i'll dig deeper, but if not do let me know.

Thanks again. The community here on this site is much more forgiving towards the beginners. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question