I am sure that somebody is struggling with understanding and pipe system call(Maybe Dankook students?)
Well.. Yes, it's hard and Shell must have at least three functions.
1. Redirection
2. Pipe
3. Background
This post will talk about how to implement pipe basically, but in order to understand this correctly
you might need to practice it a lot. For me it took almost 4~5 hours to understand each function of dup2() and pipe()
Before begin this, I would like to share really good lecture website for those functions.
http://web.eecs.utk.edu/~huangj/cs360/360/notes/Pipe/lecture.html
Unfortunately, the official website which describes system calls in Linux does not give us enough information
so that we implement those functions practically. so I would like to explain this a little bit more easily than it.
Well.
Firstly, we need to understand what the pipe is
What's pipe? yes, pipe is a kind of path between one process and process.
since a process(program in execution) is strictly prohibited to share a data. there is no way to communication with each other. That's why we use a pipe.
The prototype of pipe() system call is this
int pipe(int pipefd[2]);as long as we use pipe system call with fork(), the structures will be like this..
Make sure the parameter of pipe() system call is int pipefd[2]
fd means file description. Yes, pipe use a file description for each communication.
The official description is as below
pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe. For further details, see pipe(7).
However, what the fuck that mean? okay. let's make it simple.
Pipe is used for the communication tool by making two more file descriptions,
which is fd[0] and fd[1] fd[0] is used for reading(like STDIN) and fd[1] is used for
writing(like stdout)
pipe1(parent) file pipe1(child) |----------| desc- |----------| | code, | riptors | code, | | globals, | |---------| | globals, | | heap. | 0 <----- | | -----> 0 | heap. | | | 1 -----> |operating| <----- 1 | | | | 2 -----> | system | <----- 2 | | | | pipefd[0] <--- | | ---> pipefd[0] | | | | pipefd[1] ---> |---------| <--- pipefd[1] | | | | | | | stack | | stack | |----------| |----------|Let's look at the example.
from http://web.eecs.utk.edu/~huangj/cs360/360/notes/Pipe/lecture.html
well. before using pipe, we used only "STDIN, STDOUT, STDERR", however we have two more FDs
that are used for a kernal path between parent task and child task.
then we could use "dup2" function which duplicate a file description
We are gonna use dup2() function. The prototype is this.
int dup2(int fd1, int fd2)
what if we declare like "dup2(STDOUT,fd)"???????????????????
it means whenever STDOUT happens, the file description will be replaced instead of STDOUT,
DO NOT BE CONFUSED. We can use both file description STDOUT and FD however even if you use
STDOUT, all output datas will automatically be controlled by the file description.
that means you cannot see any STDOUT result, because all datas go to the file that is pointed by
FD!
int main(void) { int pfd[2]; // 0 for reading, 1 for writing. int pid; int pid2; char buf[10]; int readSize; pipe(pfd); // make a pipe; if( (pid = fork()) == 0) { close(pfd[0]); dup2(pfd[1],1); // close(pfd[1]); // open("a.txt",O_RDONLY, 0664); execlp("more","more","a.txt",(char*)0); // call more // child precessing // } else // parent processing { if((pid2=fork())==0) { close(pfd[1]); dup2(pfd[0], 0); // close(pfd[0]); printf("does it work?"); execlp("wc","wc",(char*)0); } else { wait(); printf("Succedded"); } } }
This program is the same as "more a.txt | wc"
See? using pipe and dup2 all stdout goes to the "wc" task by using fd[1](for writing)
and the wc program reads all data from more process by using "STDIN with fd[0]"
we might be confused because dup2 and pipe function are too complicated.
in order to understand this processing, I suggest you draw a status of task structure.
=)
'Programming > System programming' 카테고리의 다른 글
custom cp sheel command with the mode information (0) | 2011.10.15 |
---|---|
sys/types.h (2) | 2011.10.10 |