mkfifo函数使用

mkfifo(建立实名管道)
相关函数
    pipe,popen,open,umask
表头文件
    #include<sys/types.h>
    #include<sys/stat.h>
定义函数
    int mkfifo(const char * pathname,mode_t mode);
函数说明
    mkfifo()会依参数pathname建立特殊的FIFO文件,该文件必须不存在,而参数mode为该文件的权限(mode%~umask),因此 umask值也会影响到FIFO文件的权限。Mkfifo()建立的FIFO文件其他进程都可以用读写一般文件的方式存取。当使用open()来打开 FIFO文件时,O_NONBLOCK旗标会有影响
    1、当使用O_NONBLOCK 旗标时,打开FIFO 文件来读取的操作会立刻返回,但是若还没有其他进程打开FIFO 文件来读取,则写入的操作会返回ENXIO 错误代码。
    2、没有使用O_NONBLOCK 旗标时,打开FIFO 来读取的操作会等到其他进程打开FIFO文件来写入才正常返回。同样地,打开FIFO文件来写入的操作会等到其他进程打开FIFO 文件来读取后才正常返回。
返回值
    若成功则返回0,否则返回-1,错误原因存于errno中。
错误代码
    EACCESS 参数pathname所指定的目录路径无可执行的权限
    EEXIST 参数pathname所指定的文件已存在。
    ENAMETOOLONG 参数pathname的路径名称太长。
    ENOENT 参数pathname包含的目录不存在
    ENOSPC 文件系统的剩余空间不足
    ENOTDIR 参数pathname路径中的目录存在但却非真正的目录。
    EROFS 参数pathname指定的文件存在于只读文件系统内。
    
示例1:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main(void)
{
    char buf[80];
    int fd;
    unlink( "zieckey_fifo" );
    mkfifo( "zieckey_fifo", 0777 );
    
    if ( fork() > 0 )
    {
        char s[] = "Hello!\n";
        fd = open( "zieckey_fifo", O_WRONLY );
        write( fd, s, sizeof(s) );
        //close( fd );
    }
    else
    {
        fd = open( "zieckey_fifo", O_RDONLY );
        read( fd, buf, sizeof(buf) );
        printf("The message from the pipe is:%s\n", buf );
        //close( fd );
    }
    
    return 0;
}
执行
    hello!
    
示例2:    
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

int main( int argc, char **argv )
{
    mode_t mode = 0666;
    if ( argc !=2 )
    {
        printf( "Usage:[%s] fifo_filename\n", argv[0] );
        return -1;
    }
    
    if (mkfifo( argv[1], mode)<0 )
    {
        perror( "mkfifo");
        return -1;
    }
    
    return 0;
}    
  • 8
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在 Linux 环境中,`mkfifo()` 函数用于创建一个命名管道。命名管道是一种特殊的文件,用于实现进程间通信。它与普通文件的区别在于,它的数据只能被读取一次,读取后就被删除,不能被重复读取。 `mkfifo()` 函数的原型如下: ```c #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode); ``` 其中,`pathname` 参数是要创建的命名管道的路径名;`mode` 参数是文件权限,它指定了文件的访问权限和所有权等信息。 `mkfifo()` 函数成功返回 0,失败返回 -1,并设置相应的错误码。创建成功后,可以使用 `open()` 函数打开该命名管道进行读写操作,使用 `unlink()` 函数删除该文件。 下面是一个简单的示例程序,演示如何使用 `mkfifo()` 函数创建一个命名管道: ```c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main(int argc, char *argv[]) { const char *fifo_file = "myfifo"; int ret; // 创建命名管道 ret = mkfifo(fifo_file, 0666); if (ret < 0) { perror("mkfifo"); exit(1); } // 打开命名管道进行读写操作 int fd = open(fifo_file, O_RDWR); if (fd < 0) { perror("open"); exit(1); } // 写入一条消息 const char *msg = "Hello, named pipe!\n"; write(fd, msg, strlen(msg)); // 关闭文件描述符 close(fd); // 删除文件 unlink(fifo_file); return 0; } ``` 在该示例程序中,我们首先使用 `mkfifo()` 函数创建了一个名为 `myfifo` 的命名管道,然后使用 `open()` 函数打开该文件进行写操作,写入一条消息后关闭文件描述符,最后使用 `unlink()` 函数删除该文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值