文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>正确使用pipe()函数

正确使用pipe()函数

时间:2010-07-06  来源:ctosgh

说明:

  1. 该代码最初的设想是“父进程给子进程发送一个信息,子进程在收到父进程发送的信息后给父进程一个回复信息”
  2. 该代码没有考虑出错处理,仅仅是演示目的

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
int main()
{
    pid_t pid = 0;
    int fds[2], nwr = 0;
    char buf[128];

    pipe(fds);

    pid = fork();
    if(pid < 0)
    {
        printf("Fork error.\n");
        return -1;
    }else if(pid == 0)
    { 
        printf("This is child process, pid = %d\n", getpid());
#if 0

        //part A
        printf("Child:waiting for message...\n");
        nwr = read(fds[0], buf, sizeof(buf))
        printf("Child:received\"%s\"\n", buf);
#endif
#if 0

        //part B
        printf("Child:send reply\n");
        strcpy(buf, "Reply from child");
        nwr = write(fds[1], buf, sizeof(buf));
        printf("Child:send %d bytes to parent.\n", nwr);
#endif
    }else{
        printf("This is parent process, pid = %d\n", getpid());

        printf("Parent:sending message...\n");
        strcpy(buf, "Message from parent");
        nwr = write(fds[1], buf, sizeof(buf));
        printf("Parent:send %d bytes to child.\n", nwr);
#if 1

        //part C
        printf("Parent:waiting for reply from child...\n");
        nwr = read(fds[0], buf, sizeof(buf));
        printf("Parent:received \"%s\" from child\n", buf);
#endif
    }
    return 0;
}


《1》如果part A、part B和part C部分全部打开,子进程无法收到父进程发送的信息;

《2》如果part A、part B关闭,part C打开,那么父进程发送的信息被自己接收;

《3》如果part A打开,part B和part C关闭,那么父进程发送的信息被子进程接收;

总结性的东西就不写了,从试验中自己体会吧!我觉得pipe是一个父子进程间单向通信的工具,如果要用pipe实现父子进程间双向通信,那么必须调用两次pipe。所以pipe比较常见的使用方法如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
int main()
{
    pid_t pid = 0;
    int fds[2];
    char buf[128];
    int nwr = 0;

    pipe(fds);//should before fork()

    pid = fork();
    if(pid < 0)
    {
        printf("Fork error.\n");
        return -1;
    }else if(pid == 0)
    { 
        printf("This is child process, pid = %d\n", getpid());
        printf("Child:waiting for message...\n");
        close(fds[1]);
        nwr = read(fds[0], buf, sizeof(buf));
        printf("Child:received\"%s\"\n", buf);
    }else{
        printf("This is parent process, pid = %d\n", getpid());
        printf("Parent:sending message...\n");
        close(fds[0]);
        strcpy(buf, "Message from parent");
        nwr = write(fds[1], buf, sizeof(buf));
        printf("Parent:send %d bytes to child.\n", nwr);
    } 
    return 0;
}

不对之处,欢迎指正!

相关阅读 更多 +
排行榜 更多 +
<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />

<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />

飞行射击 下载
<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />

<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />

飞行射击 下载
<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />

<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />

飞行射击 下载