Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66589
  • 博文数量: 30
  • 博客积分: 1260
  • 博客等级: 中尉
  • 技术积分: 285
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-03 12:27
文章分类

全部博文(30)

文章存档

2010年(30)

我的朋友

分类: LINUX

2010-06-25 18:00:06

以实例的方式讲述两者的区别

1. 管道

#include
#include
#include
#include
#include
 
int main()
{
    pid_t childpid
    int fd[2];
    char string[] = "data from child process";
    char tmp[100];
 
    childpid = fork();
    switch(childpid)
    {
         case -1:
            printf("fork error.\n");
            break;
         case 0:
            printf("fork ok, I am the child.\n");
            close(fd[0]);//0->in 1->out 2->stderr
            write(fd[1], string, strlen(string));
            exit(0);
 
        default:
            printf("I am the father.\n");
            close(fd[1]);
            read(fd[0], tmp, sizeof(tmp));
            printf("now buf is %s\n", tmp);
    }
    return 0
}

2. 有名管道


#defined FIFO_TMP "/tmp/fifotest"
 
int main()
{
    int fd;
    char buf[] = "data packet.";
 
    if((mkfifo(FIFO_TMP, S_IRWXU) < 0) && error != EEXIST)
        printf("Error to creat fifo tmp\n");
 
    fd = fopen(FIFO_TMP, "w");
    
    ret = fwite(buf, 1, 100, fd);
}


阅读(557) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~