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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值