利用pipe实现兄弟进程之间的通信

来源:互联网 发布:qq mac版 编辑:程序博客网 时间:2024/05/20 11:49

利用pipe实现兄弟进程之间的通信

进程A创建2个子进程B,C。然后B C执行的程序利用pipe来通信:ls的输出通过管道到达另一个进程用wc统计单词数,字节数,行数。

  • execve系统调用用于执行指定的程序,其他的exec系列函数都是基于它。APUE-188

    int execve(const char *filename, char *const argv[],char *const envp[]);
    execve() executes the program pointed to by filename. filename must be either a binary executable, or a script starting with a line of the form:
    #! interpreter [optional-arg]
    argv is an array of argument strings passed to the new program. By convention, the first of these strings should contain the filename associated with the file being executed. envp is an array of strings, conventionally of the form key=value, which are passed as environment to the new program. Both argv and envp must be terminated by a NULL pointer.

  • dup2复制现有的描述符,实现重定向,底层就是改变文件号和struct file的绑定。

    int dup2(int oldfd, int newfd);
    dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note the following:

    • If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.
    • If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd.
      After a successful return from one of these system calls, the old and new file descriptors may be used interchangeably. They refer to the same open file description and thus share file offset and file status flags; for example, if the file offset is modified by using lseek(2) on one of the descriptors, the offset is also changed for the other.
      The two descriptors do not share file descriptor flags (the close-on-exec flag). The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the duplicate descriptor is off.也就是APUE60所说,新描述符的close-on-exec标志总被dup函数清楚
  • ls命令下面俩选项要注意:

    -1 list one file per line
    -l use a long listing format

代码:

#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/time.h>#include <sys/resource.h>#include <sys/wait.h>int main(){    int childB, childC;    int pipefds[2];    char *args1[] = {"//usr/bin/wc", NULL};    char *args2[] = {"/bin/ls", "-l", NULL};    //=======Process A======    pipe(pipefds);    if(!(childB = fork())){        //======== Process B ========        close(pipefds[1]);// close write pipe end        // redirect stdin        close(0);        dup2(pipefds[0], 0);        close(pipefds[0]);        // execute the target        execve("/usr/bin/wc", args1, NULL);        printf("pid %d: I got here, sth is WRONG!\n", getpid());    }    // Process A continue    close(pipefds[0]);    if(!(childC = fork())){        //======== Process C ========        //redirect stdout        close(1);        dup2(pipefds[1], 1);        close(pipefds[1]);        // execute the target        execve("/bin/ls", args2, NULL);        //ERROR: If run to here         printf("pid %d: I got here, sth is WRONG!\n", getpid());    }    close(pipefds[1]);    wait4(childB, NULL, 0, NULL);    printf("%s\n", "OVER!");    return 0;}

运行效果:
这里写图片描述

参考:
《Linux内核源码情景分析 P702》

0 0
原创粉丝点击