Solved

Pipe Synchronization

Posted on 2003-11-02
8
2,062 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux  : how to view CPU info and  its usage details 7 260
Linux , how can I inspect a tar GZ file 3 149
can't run or update 'yum' on CentOS 7 7 69
KVM vs  Qemu 4 116
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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