search

Home  >  Q&A  >  body text

linux - I am looking at some C functions. What do these bitwise ORs mean?

I am learning C recently, and I saw that some functions use bitwise or parameter passing. I don’t know what it means, and it seems that bit operations are rarely used in daily work.

For example, the following piece of code

#define LOCKFILE "/var/run/gwyydaemon.pid"

#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
 fd = open(LOCKFILE,O_RDWR|O_CREAT,LOCKMODE);

What do the O_RDWR|O_CREAT and S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH here mean?

仅有的幸福仅有的幸福2754 days ago681

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-05-16 13:31:54

    You need to check the documentation yourself

    • O_RDWR: Readable and writable

    • O_CREAT : If the file does not exist, create it

    Bitwise OR means combined configuration, assuming (I don’t know the specific value):
    O_RDWR 等于二进制 00000001
    O_CREAT 等于二进制 00000010 is equal to binary 00000001

    O_CREAT is equal to binary 00000010

    Then the combined configuration is🎜
    00000001  可读写
    00000010  创建
    --------  位或
    00000011  可读写+创建

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 13:31:54

    It’s just bit operations
    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH and so on should be defined in a certain header file
    For example (the value is written casually by me, please check that header file for details, I’m being lazy)

    #define S_IROTH 0x01
    #define S_IRGRP 0x02
    #define S_IWUSR 0x04
    #define S_IRUSR 0x08

    Wait.

    reply
    0
  • Cancelreply