linux C 命名管道

命名管道:解决不相关进程之间的通信问题。

函数:http://write.blog.csdn.net/postedit?ref=toolbar

#include

#include

int mkfifo(const char *filename,mode_t mode);

访问命名管道:

1、打开fifo文件

与打开其它文件一样,fifo文件也可以用open打开,mkfifo文件只是创建一个fifo文件,要使用命名管道,害的将其打开。

注意:

程序不能以O_RDWR模式打开FIFO文件进行读写操作,而其行为也未明确定义,因为如果一个管道以读/写方式打开,进程就会读回自己的输出。我们通常使用fifo只是为了单向的数据传递。

打开fifo文件的方式:

open(const char *path,O_RDONLY);           

open(const char *path,O_RDONLY | O_NONBLOCK);

open(const char *path,O_WRONLY);

open(const char *path,O_WRONLY | O_NONBLOCK);

O_NONBLOCK表示非阻塞,加上这个选项后,表示open调用是非阻塞的。如果没有这个选项,则表示open调用是阻塞的。

open调用的阻塞:对于以只读方式打开的FIFO文件,如果open调用是阻塞的,除非有一个进程以写方式打开同一个FIFO,否则它不会返回。如果open调用是非阻塞的,则即使没有其他进程以写方式打开同一个FIFO文件,open调用将成功并立即返回。

对于以只写方式打开的FIFO文件,如果open调用是阻塞的,open调用将被阻塞,直到有一个进程以只读方式打开同一个FIFO文件为止。如果open调用是非阻塞的,open总会立即返回,但如果没有其他进程以只读方式打开同一个FIFO文件,open调用将返回-1,并且FIFO也不会被打开。

代码:

fifowrite.c

#include
#include
#include
#include
#include
#include
#include
#include

int main()
{
    const char *fifo_name = "/tmp/my_fifo";
    int pipe_fd = -1;
    int data_fd = -1;
    int res = 0;
    const int open_mode = O_WRONLY;
    int bytes_sent = 0;
    char buffer[PIPE_BUF+1] = {0};
    if(access(fifo_name,F_OK) == -1)
    {
        res = mkfifo(fifo_name,0777);
        if(res != 0)
        {
            fprintf(stderr,"Could not create fifo %s\n",fifo_name);
            exit(EXIT_FAILURE);
        }
    }
    printf("Process %d opening FIFO O_WRONLY\n",getpid());
    pipe_fd = open(fifo_name,open_mode);
    data_fd = open("Data.txt",O_RDONLY);
    printf("Process %d result %d\n",getpid(),pipe_fd);
    if(pipe_fd != -1)
    {
        int bytes_read = 0;
        bytes_read = read(data_fd,buffer,PIPE_BUF);
        buffer[bytes_read] = '\0';
        while(bytes_read > 0)
        {
            res = write(pipe_fd,buffer,bytes_read);
            if(res == -1)
            {
                fprintf(stderr,"Write error on pipe\n");
                exit(EXIT_FAILURE);
            }
            bytes_sent += res;
            bytes_read = read(data_fd,buffer,PIPE_BUF);
            buffer[bytes_read] = '\0';        
        }
        close(pipe_fd);
        close(data_fd);
    }
    else
        exit(EXIT_FAILURE);
    printf("Process %d finished\n",getpid());
    exit(EXIT_SUCCESS);

}



fifiread.c

#include
#include
#include
#include
#include
#include
#include
#include


int main()
{
    const char *fifo_name = "/tmp/my_fifo";
    int pipe_fd = -1;
    int data_fd = -1;
    int res = 0;
    int open_mode = O_RDONLY;
    char buffer[PIPE_BUF+1] = {0};
    int bytes_read = 0;
    int bytes_write = 0;
    printf("Process %d opening FIFO O_RDONLY\n",getpid());
    pipe_fd = open(fifo_name,open_mode);
    data_fd = open("ReadData.txt",O_WRONLY | O_CREAT,0644);
    printf("Process %d result %d\n",getpid(),pipe_fd);
    if(pipe_fd != -1)
    {
        do
        {
            res = read(pipe_fd,buffer,PIPE_BUF);
            bytes_write = write(data_fd,buffer,res);
            bytes_read += res;
        }while(res > 0);
        close(pipe_fd);
        close(data_fd);
    }
    else
    {
        exit(EXIT_FAILURE);
    }
    printf("Process %d finished,%d byes read\n",getpid(),bytes_read);
    exit(EXIT_SUCCESS);
}






你可能感兴趣的:(linux)