• fcntl()


     

    fcntl()

    F_GETFL
    ---------------------------------------------
            将文件状态标志作为函数值返回。

            文件状态标志:
            O_RDONLY        O_WRONLY        O_RDWR
            O_APPEND        O_NONBLOCK      O_SYNC FASYNC(O_ASYNC)
            三个存取方式标志(O_RDONLY, O_WRONLY, O_RDWR)是互斥的,一个文件只能有这三种值的其中一个。首选需要用屏蔽字O_ACCMODE取得存取方式位,然后将结果与这三个标志相比较。

           
    F_SETFL
    ---------------------------------------------
            将文件状态标志设置为第三个参数的值,可以更改的几个标志是:O_APPEND, O_NONBLOCK, O_SYNC, FASYNC(O_ASYNC)

    注:
            当一个打开的文件FASYNC标志变化时(调用fcntl()函数,设置FASYNC文件标志时),该文件所对应的设备驱动的fasync()接口将被调用。











    ---------------------------------------------
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char **argv)
    {
            int fd, val, accmode;
           
            if (argc != 2) {
                    printf("usage: ./a.out filename ");
                    return -1;
            }

            if ((fd = open(argv[1], O_RDWR)) < 0) {
                    perror("open error");
                    return -1;
            }

            if ((val = fcntl(fd, F_GETFL)) < 0) {
                    perror("fcntl error");
                    return -1;
            }

            accmode = val & O_ACCMODE;
            if      (accmode == O_RDONLY)   printf("read only ");
            else if (accmode == O_WRONLY)   printf("write only ");
            else if (accmode == O_RDWR)     printf("read write ");
            else                            printf("unknown access mode ");
           

            if ((val = fcntl(fd, F_SETFL, val | FASYNC | O_NONBLOCK)) < 0) {
                    perror("fcntl error");
                    return -1;
            }

            if ((val = fcntl(fd, F_GETFL)) < 0) {
                    perror("fcntl error");
                    return -1;
            }
           
            //if (val & O_ASYNC)
            if (val & FASYNC)
                    printf("async ");
            if (val & O_NONBLOCK)
                    printf("nonblocking ");

    }

  • 相关阅读:
    30.3 Collections 集合体系的工具类
    30.2 案例:ArrayList本身数据可以重复,写出去重的方法
    30 HashSet
    29.3 ArrayList、List、LinkedList(链表)优缺点
    foreach-- for循环的增强型:for(类型 变量 : 数组对象) { 使用变量 }
    <E> 泛型
    29.2 Iterator 迭代器ConcurrentModificationException:并发修改异常处理
    29.2 Iterator 迭代器
    29.1 collection层次结构 中的根接口
    29 collection 集合体系结构
  • 原文地址:https://www.cnblogs.com/devil-91/p/3303489.html
Copyright © 2020-2023  润新知