Pipes used for Interprocess Communication

"Interprocess communication in MINIX uses pipes. When a user types

cat file1 file2 | sort

the shell creates a pipe and arranges for standard output of the first process to write to the pipe, so standard input of the second process can read from it. The PIPE system call creates a pipe and returns two file descriptors, one for writing and one for reading. The call is

pipe(&fd[0]);

where fd is an array of two integers and fd[0] is the file descriptor for reading and fd[1] is the one for writing. Typically, a FORK comes next, and the parent closes the file descriptor for reading and the child closes the file descriptor for writing(or vice versa), so when they are done, one process can read the pipe and the other can write on it." The implementation can be like this:

#define STD_INPUT 0     /*file descriptor for standard input*/
#define STD_OUTPUT 1 /*file descriptor for standard output*/

pipeline(process1, process2)
char *process1, *process2 /*Old fashioned parameters' declaration*/
{
    
int fd[2];

    pipe(
&fd[0]); /*create a pipe*/
    
if(fork() != 0)
    
{
        
/*The parent process executes these statements.*/
        close(fd[
0]);                       /*process1 does not need to read from pipe*/
        close(STD_OUTPUT);      
/*prepare for new standard output*/
        dup(fd[
1]);                         /*set standard output to fd[1]*/
        close(fd[
1]);                       /*this file descriptor not needed any more*/
        execl(process1, process1, 
0);
    }

    
else
    
{
        
/*The child process executes these statements.*/
        close(fd[
1]);                       /*process2 does not need to write to pipe*/
        close(STD_INPUT);      
/*prepare for new standard input*/
        dup(fd[
0]);                         /*set standard output to fd[0]*/
        close(fd[
0]);                       /*this file descriptor not needed any more*/
        execl(process2, process2, 
0);
    }

}

fork() will create a subprocess identical to the parent

Usually, we use the return value to decide whether some statements should be executed by the parent process or the child one. If it returns 0, that means it is the child process's turn; other values mean the parent one, and the specific value is the process id of the its current child process.

dup(fd) will assign the parameter file descriptor fd to the current available file descriptor with the smallest number

So, the two file descriptors point to the same file, and we can use them separately.

close(fd) will close the specific file descriptor, but not the file itself

We cannot use this file descriptor any more, but we are still able to operate on the file by other file descriptors which also point to that file, as long as the file itself is not closed.

execl(process, process, 0) will do the reading or the writing tasks, through the pipe.

 

Actually the codes are very clear, but I spent almost half a day to understand how exactly it works. The key to understand this code is to know that fork() create a subprocess identical to the parent, and they do not share the data, but have two copies. Thus, the operations executed by the parent will not affect the child, say change the child's data, vice versa. Then the codes above will be easy to understand.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章