Solved

Pipe Synchronization

Posted on 2003-11-02
8
2,057 Views
Last Modified: 2012-06-21
I have a pipe program which interprets every > as | in Linux.
It reads in command from user and does shell execution based on the user input.
For example if the user types in
    ls >  wc
it will work like ls | wc in command line.

However if I do this
    cat /usr/include/time.h > wc
it will not work. From what I know, the pipe is overfilled. How to make it synchronized so that it will not overflow? Or other than making it synchronized, are there any methods that will solve the overflow problem?


hongjun
0
Comment
Question by:hongjun
  • 4
  • 4
8 Comments
 
LVL 5

Expert Comment

by:mtmike
ID: 9665999
Pipe overflow should not be the problem.

Two concurrent processes that are communicating using a pipe always work in lockstep. The writing process (ls or cat in your examples) fills up the pipe until it is full and then blocks. The reading process (wc) reads out the pipe until it is empty and then blocks.

In case the pipe is full, the writing process has to wait until the reading process reads out the data and, in case the pipe is empty, the reading process has to wait until the writing process fills it up.

A pipe can only overflow when the reading process does not read out the data.

Why do you think the pipe is overfilled?
0
 
LVL 33

Author Comment

by:hongjun
ID: 9666013
I can only think the problem is either due to my pipe coding not being concurrent or is overfilled.
Any example coding to make the pipe concurrent?

hongjun
0
 
LVL 5

Expert Comment

by:mtmike
ID: 9666122
Here's a small example C program:

#include <sys/types.h>
#include <unistd.h>

int main()
{
      int fds[2];
      pid_t pid;

      if (pipe(fds)) {
            return 1;
      }

      pid = fork();
      if (pid == 0) {

            /* Child (writing) process */
            dup2(fds[1], 1);
            close(fds[0]);
            execlp("ls", "ls", 0);
      } else if (pid > 0) {

            /* Parent (reading) process */
            dup2(fds[0], 0);
            close(fds[1]);
            execlp("wc", "wc", 0);
      }
      return 1;
}

It's important to close the end of the pipe a process does not use.
0
 
LVL 33

Author Comment

by:hongjun
ID: 9666159
i basically have similar coding.
the only problem lies when i try to pipe an output which is very large like cat /usr/include/time.h to an input.

hongjun
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Accepted Solution

by:
mtmike earned 500 total points
ID: 9666192
When I change
  execlp("ls", "ls", 0);

into
  execlp("cat", "cat", "/usr/include/time.h", 0);

It still works (even with a much larger file).

Can you show some of your code?
0
 
LVL 33

Author Comment

by:hongjun
ID: 9669460
I want to ask.
Any example of using named and unnamed pipes?
I just realised that there two types of pipes.
What are the use of them?

hongjun
0
 
LVL 5

Expert Comment

by:mtmike
ID: 9670163
A named pipe is similar to an unnamed pipe, except that it is created in a different way.

Named pipes can be opened by filename using mkfifo(). Two processes that want to communicate using a pipe can call mkfifo() on the same filename to open a pipe.

An unnamed pipe must be created in a single process using pipe(). The pipe file descriptors can be passed on to children by using fork().

Named pipes are seldom used and have been superseded by the more general unix domain socket mechanism.
0
 
LVL 33

Author Comment

by:hongjun
ID: 9705956
i solved that in another approach.
But anyway thanks for your reply and they are very relevant.

hongjun
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now