zoukankan      html  css  js  c++  java
  • 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 ");

    }

  • 相关阅读:
    GDB Practice
    GCC常用命令
    使用VS2010 C#编写ActiveX控件
    [.NET] 使用 .NET Framework 開發 ActiveX Control
    VC2005开发MFC ActiveX控件
    Register DLL and OCX
    COM组件开发实践
    Java Invoke C and C++ Using JNI
    Unable to cast object of type 'System.Int32' to type 'System.String'.
    SharePoint wiki 分类获取所有的
  • 原文地址:https://www.cnblogs.com/devil-91/p/3303489.html
Copyright © 2011-2022 走看看