I'm trying to understand the usage of pipe.
A parent process will pipe and if the parent forks, the child process will inherit the pipe. So now we hae a direct link to the child process and they can communicate?
I get lost when we start to close pipes and redirect pipes. Does anyone have a good explanation on closing pipes and redirecting pipes?
Thank you in-advance.
Here's the deal.
Here is what it looks like:
P1=[WR FD]===========[RD FD]=[STDIN]=P2
P1=[RD FD]===========[WR FD]=[STDOUT]=P2
P1 and P2 are processes. And "===" depict the pipes.
Your question was specifically about closing and redirecting. That comes into play when you perform the switch I referred to earlier. Let's say you have obtained a pipe by using the pipe() system call.
int fd[2];
pipe(fd);
Now you create a child process, and in that process perform the switch, like so:
if (!fork()) // 0 return value indicates that you are in the child process
{
dup2(fd[0], 0); // dup2 first closes 0 in the child process, then duplicates
// fd[0] as 0; keep in mind that 0 is stdin and fd[0] is the
// read end of the pipe
exec(something); // start the new process, which when it reads stdin, will be
// reading the pipe instead
}
See Pipeline.
Simple example
ls -l | less
In this example, ls is the Unix directory lister, and less is an interactive text pager with searching capabilities. The pipeline lets the user scroll up and down a directory listing that may not fit on the screen.
Creating pipelines programmatically
Pipelines can be created under program control. The Unix
pipe()
system call asks the operating system to construct a new anonymous pipe object. This results in two new, opened file descriptors in the process: the read-only end of the pipe, and the write-only end. The pipe ends appear to be normal, anonymous file descriptors, except that they have no ability to seek.To avoid deadlock and exploit parallelism, the Unix process with one or more new pipes will then, generally, call
fork()
to create new processes. Each process will then close the end(s) of the pipe that it will not be using before producing or consuming any data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With