Chinaunix首页 | 论坛 | 博客
  • 博客访问: 52205
  • 博文数量: 17
  • 博客积分: 1471
  • 博客等级: 上尉
  • 技术积分: 155
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-27 11:44
文章分类

全部博文(17)

文章存档

2012年(1)

2011年(1)

2008年(15)

我的朋友

分类:

2008-05-27 11:56:48

*
 * 管道通信:有名管道
 * 无名管道只能用于具有亲缘关系的进程之间,而有名管道可以在互不相关的两个进程间
 * 实现彼此通信。要注意,FIFO严格按照先进先出的规则,对管道及FIFO的读总是从开始
 * 处返回数据,对它们的写则把数据添加到末尾,不支持lseek等文件定位操作。
 *
 * 有名管道的创建使用mkfifo()。创建成功后就可以使用open、read、write这些函数了。
 * 读管道部分
 */


##----------------------------------##
fifo_read.c
    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/*在这里设置打开管道文件的mode为只读形式*/
#define FIFOMODE (O_CREAT | O_RDWR | O_NONBLOCK)
#define OPENMODE (O_RDONLY | O_NONBLOCK)
#define FIFO_SERVER "myfifo"

int main(void)
{
        char buf[100];
        int fd;
        int readnum;

        /*创建有名管道,设置为可读写,无阻塞,如果不存在则按照指定权限创建*/
        if ((mkfifo(FIFO_SERVER, FIFOMODE) < 0) && (errno != EEXIST)) {
                printf("cannot create fifoserver\n");
                exit(1);
        }

        printf("Preparing for reading bytes... ...\n");

        /*打开有名管道,并设置非阻塞标志*/
        if ((fd = open(FIFO_SERVER, OPENMODE)) < 0) {
                perror("open");
                exit(1);
        }

        while (1) {
                /*初始化缓冲区*/
                bzero(buf, sizeof(buf));
                /*读取管道数据*/
                if ((readnum = read(fd, buf, sizeof(buf))) < 0) {
                        if (errno == EAGAIN) {
                                printf("no data yet\n");
                        }
                }
                /*如果读到数据则打印出来,如果没有数据,则忽略*/
                if (readnum != 0) {
                        buf[readnum] = '\0';
                        printf("read %s from FIFO_SERVER\n", buf);
                }
                sleep(1);
        }

        return 0;
}

fifo_write.c
   
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

/*特别注意写管道时,设置打开管道文件的格式必须为可写*/
#define FIFO_SERVER "myfifo"
#define OPENMODE (O_WRONLY | O_NONBLOCK)

int main(int argc, char **argv)
{
        int fd;
        int nwrite;

        /*打开管道文件,可写非阻塞*/
        if ((fd = open(FIFO_SERVER, OPENMODE)) < 0) {
                perror("open");
                exit(1);
        }

        /*如果没有在命令行中写入参数,那么要重新运行程序*/
        if (argc == 1) {
                printf("Please send something\n");
                exit(1);
        }

        /*向管道文件中写入数据,在这里要用strlen,如果用sizeof,则只是4个字节的指针长度*/
        if ((nwrite = write(fd, argv[1], strlen(argv[1]))) < 0) {
                if (errno == EAGAIN) {
                        printf("The FIFO has not been read yet.Please try later\n");
                }
        }
        else {
                printf("write %s to FIFO\n", argv[1]);
        }

        return 0;
}

Makefile
   
CC = gcc
OBJS = fifo_write fifo_read
CFLAGS = -Wall -O2 -g

all: fifo_write fifo_read

fifo_write: fifo_write.c
        $(CC) $(CFLAGS) $^ -o $@

fifo_read: fifo_read.c
        $(CC) $(CFLAGS) $^ -o $@

.PHONY: clean dist distclean
clean:
        rm -rf $(OBJS) myfifo

dist:
        tar zcvf fifo.tar.gz * && echo "OK"

distclean:
        rm -rf $(OBJS) fifo.tar.gz myfifo
转载:http://blog.chinaunix.net/u/21948/showart_204272.html
   
阅读(795) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~