Linux命名管道及函数
命名管道,也称FIFO,实质是一种文件类型,通过FIFO可以用于任何两个进程间的通信。
1 命名管道的创建
1.1 命令方式
在shell中可以使用mkfifo命令创建一个命名管道,格式为:
mkfifo [option] name
其中option选项用于选择创建FIFO的模式,使用形式为-m mode,mode为八进制模式,创建示例:
mkfifo -m 666 myfifo
创建之后可以在当前文件间看到新建的文件。
1.2 函数方式
FIFO管道可通过mkfifo()函数创建,函数原型为:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
创建成功返回0,出错返回1。函数第一个参数为普通的路径名,即创建后FIFO文件的名字,第二个参数与打开普通文件的open函数中的mode参数相同。
如果要创建的FIFO文件已经存在,则会返回EEXIST错误,因此在创建前应先检查是否创建成功,若文件已存在,只要调用打开FIFO的函数即可。
1.3 编程示例
创建一个命名管道,create_FIFO.c:
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    mode_t mode = 0666;
    if(argc != 2)
    {
        printf("USEMSG:create_FIFO{FIFO name}\n");
        exit(1);
    }
    if((mkfifo(argv[1], mode))<0)
    {
        perror("failed to mkfifo!\n");
        exit(1);
    }
    else
    {
        printf("you successfully create a FIFO name is: %s\n", argv[1]);
    }
    return 0;
}
编译执行:
$ ./create_FIFO testFIFO
you successfully create a FIFO name is: testFIFO
上述程序使用mkfifo函数创建了一个名为“testFIFO”的命名管道。
此时再次执行:
C$ ./create_FIFO testFIFO
failed to mkfifo!
: File exists
由于要创建的FIFO已经存在,再次创建会提示创建失败。
2 命名管道的读写
一般的文件I/O函数均可用于FIFO操作,如open、close、read、write等,若要删除一个命名管道,则使用系统调用unlink。
2.1 编程示例
2.1.1 FIFO写入
向FIFO写入数据,write_fifo.c:
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#define BUFES PIPE_BUF
int main(void)
{
   int fd;
   int n,i;
   char buf[BUFES];
   time_t tp;
   char fifo_name[]="fifo1";
   printf("My process ID: %d\n", getpid());
   // create a fifo
   if(mkfifo(fifo_name, 0666) < 0)
   {
       printf("mkfifo failed, maybe there exist a same fifo file\n");
       //exit(1);
   }
   else
       printf("mkfifo ok\n");
   printf("...\n");
   // open the fifo, write only mode
   if((fd=open(fifo_name, O_WRONLY))<0)
   {
       printf("Open failed!\n");
       exit(1);
   }
   else
       printf("Open fifo ok!\n");
   for(i=0;i<5;i++)
   {
       time(&tp);// get current time
       n=sprintf(buf,"write_fifo %d seconds %s", getpid(), ctime(&tp));
       printf("Send msg: %s", buf);
       // write to fifo
       if((write(fd, buf, n+1)) < 0)
       {
           printf("Write failed!\n");
           close(fd);
           exit(1);
       }
       sleep(2);
   }
   // close the fifo
   close(fd);
   // delete the fifo
   unlink(fifo_name);
   exit(0);
}
该程序首先创建一个名为fifo1的FIFO,如果已存在则直接调用open()函数打开,之后通过write()函数写入当前的时间内容到FIFO,最后使用close()函数关闭FIFO,并用unlink(函数删除FIFO。
2.1.2 FIFO读取
从FIFO读取数据 ,read_fifo.c:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFES PIPE_BUF
int main(void)
{
    int fd;
    int len;
    char buf[BUFES];
    mode_t mode = 0666;
    printf("My process ID:%d\n", getpid());
    // open a fifo, read only mode
    if((fd=open("fifo1", O_RDONLY))<0)
    {
        printf("Open failed!\n");
        exit(1);
    }
    // read the fifo
    while((len=read(fd, buf, BUFES))>0)
        printf("Read_fifo read:%s", buf);
    // close the fifo
    close(fd);
    exit(0);
}
2.1.3 测试
分别编译上述两个程序。测试之前先使用mkfifo命令创建一个名为fifo1的命名管道:
mkfifo -m 666 fifo1
然后打开两个shell窗口,依次运行write_fifo和read_fifo两个程序。
其中一个shell中运行写入程序:
$ ./write_fifo
My process ID: 2278
mkfifo ok
...
Open fifo ok!
Send msg: write_fifo 2278 seconds Tue Nov 26 09:49:09 2019
Send msg: write_fifo 2278 seconds Tue Nov 26 09:49:11 2019
Send msg: write_fifo 2278 seconds Tue Nov 26 09:49:13 2019
Send msg: write_fifo 2278 seconds Tue Nov 26 09:49:15 2019
Send msg: write_fifo 2278 seconds Tue Nov 26 09:49:17 2019
另一个shell中运行读出程序:
$ ./read_fifo
My process ID:2282
Read_fifo read:write_fifo 2278 seconds Tue Nov 26 09:49:09 2019
Read_fifo read:write_fifo 2278 seconds Tue Nov 26 09:49:11 2019
Read_fifo read:write_fifo 2278 seconds Tue Nov 26 09:49:13 2019
Read_fifo read:write_fifo 2278 seconds Tue Nov 26 09:49:15 2019
Read_fifo read:write_fifo 2278 seconds Tue Nov 26 09:49:17 2019
- 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)