Solved

Pipe Synchronization

Posted on 2003-11-02
8
2,053 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now