?
Solved

Pipe Synchronization

Posted on 2003-11-02
8
Medium Priority
?
2,071 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

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

Accepted Solution

by:
mtmike earned 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 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