Link to home
Start Free TrialLog in
Avatar of Maarten Bruins
Maarten Bruins

asked on

What is the difference between "mode" and "flag"?

In short, I would say:

A flag is a predefined bit or bit sequence that holds a binary value.

A mode is a distinct setting.

So it's not always possible to replace the term "mode" by "flag". This is only possible if it's about a binary value. I got confused by these terms when reading:

http://man7.org/linux/man-pages/man2/open.2.html

The argument flags must include one of the following access modes:
O_RDONLY, O_WRONLY, or O_RDWR.

The file creation flags are O_CLOEXEC,
O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TMPFILE, and
O_TRUNC.

The file status flags are all of the remaining flags listed
below.

Why they just don't say:

The file access flags are O_RDONLY, O_WRONLY, and O_RDWR.

Why they suddenly use a different term when it's about "access"? Probably behind the scenes it's also just about a binary value, right? Probably all O_VARIABLE's above are 0 or 1.

And see: https://en.wikipedia.org/wiki/File_descriptor

This table records the mode with which the file (or other resource) has been opened: for reading, writing, appending, and possibly other modes.

So Wikipedia uses the term "mode" only (and not flag). At least they are consistent, because they call them all "modes".

Is there a specific reason why sometimes mode is used and something flag (while it's about the same thing)? I would stay, be at least consistent to avoid confusion.
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

I think the difference is nothing more than an English language construct.  "Flag" is a noun and "Mode" is a verb.

The "security flag" is set.
The program is running in "Secure mode".
From the Oxford dictionary:
mode   A way or manner in which something occurs or is experienced, expressed, or done.
flag   A variable used to indicate a particular property of the data in a record.

eg.  You set various flags (properties) to open a file (mode) to read from that file.


They are not the same thing at all.
Avatar of noci
noci

In ICT A flag commonly is a yes/no condition, where a mode is some enumerable condition. (finite list of options).
Avatar of Maarten Bruins

ASKER

Okay, two different answers ;), but actually I'm thinking more in the direction of Kent.

You can open a file, but that's not a mode. However, you can open a file with certain settings. In other words, you can open a file in a certain mode. So it's not a verb, but they are kind of similar in my opinion because the settings represent the mode, so basically it comes down to the same thing.

But then I don't understand why they are not using just one of the terms.

P.S. I didn't see noci's answer yet when I was typing this, so my reaction on that will still come.
In ICT A flag commonly is a yes/no condition, where a mode is some enumerable condition. (finite list of options).

About the "flag" you're correct I think, but I think a "mode" refers to just a certain setting.

See: http://man7.org/linux/man-pages/man2/open.2.html

The argument flags must include one of the following access modes:
O_RDONLY, O_WRONLY, or O_RDWR.

They are saying: "one of the following", so they call O_RDONLY a mode. But O_RDONLY is yes/no (1/0), right? O_RDONLY is not a list of options?
O_RDONLY = 0, O_WRONLY = 1, O_RDWR = 2, ..... so.... an enumeration. (names are completely meaningless after compilation.).

See:   /usr/include/asm-generic/fcntl.h
>>You can open a file, but that's not a mode

Wrong.  The file is opened in a mode.  eg. readonly, create if not existing...
The flag you specify determine what mode the file is opened in.
@andy:
The mode is: "for read", "for write" or "for read and/or write".. supplemental to these 3 modes there are flags.. create if non existing etc.
the latter are all binary values which can be combined together or with mode.
You cannot combine "For read" + "for write" to get "for read and/or write"...
Some combination may be useless:   O_RDONLY | O_CREATE | O_TRUNC   creats a new file if needed, removes all content if it exists  and opens it for reading ==> first read will be EOF, but those are valid options...
@andy:

>>You can open a file, but that's not a mode

Wrong.  The file is opened in a mode.  eg. readonly, create if not existing...
The flag you specify determine what mode the file is opened in.

I know that, but then it's not wrong what I'm saying. Opening a file is not a mode. They way you open it can be seen as a mode somehow.

@noci:

O_RDONLY = 0, O_WRONLY = 1, O_RDWR = 2, ..... so.... an enumeration. (names are completely meaningless after compilation.).

See:   /usr/include/asm-generic/fcntl.h

Till now, this sounds the most logical. So Wikipedia uses the term "mode" incorrectly? And see: https://en.wikipedia.org/wiki/File_descriptor

This table records the mode with which the file (or other resource) has been opened: for reading, writing, appending, and possibly other modes.

"appending" is a flag (O_APPEND), so yes/node. How they can call "appending" a mode? Or is this just incorrect?
different call open in different "modes"....

man 2 open    Mode is singular defined  only access mode...
man 3 open    Mode is dual defined....   one the access mode, the other the "mode" means the protection "mode" bits. (see example)

My advice: don't try to define EVERYTHING according to dictionaries...
IMHO:  ICT industry is  a pretty sloppy engineering discipline w.r.t. definitions & usage of names etc.  many names have ambivalent meanings...
My advice: don't try to define EVERYTHING according to dictionaries...

That's easy to say. And probably you're saying that, because if you would do that, then many definitions will be wrong. Or people are using it differently. That's the whole problem.

See for example: https://www.experts-exchange.com/questions/29122175/The-columns-of-the-open-file-table.html?anchorAnswerId=42708354#a42708354

They really store the file permissions in the open file table?

You're not really answering that question there. It's about:

file mode (permissions),

So I have to dive into all the structures. I see different terms like "flags", "modes" et cetera then at least I have to start at the basic definitions. Now at least I know the terms "flags" and "modes" are just used inconsistently. First you came up with a definition, but then I came up with the Wikipedia example and then suddenly the defintiion also changed to:

different call open in different "modes"

That doesn't matter, but then I know at least that people are just using it inconsistently and that there are mulitple interpretations/definitions.
@noci
...You cannot combine "For read" + "for write" to get "for read and/or write"......

That is (to me) perfectly clear but also getting off the point of the question as I understand it.  The question is, to me, mode and flag seem to be the same so why isn't the documentation just using one of them rather than both and confusing things.

Unfortunately they are not the same at all.

From my earlier comment (Look up a dictionary online if it is still unclear)
mode   A way or manner in which something occurs or is experienced, expressed, or done.
flag   A variable used to indicate a particular property of the data in a record.

Understanding that difference will make understanding the documentation much clearer.
Hi everyone, :)

To make it more funny (because English is not my first language). For example, the mode as name of the parameter for opening a file says what mode the opened file structure should have -- symbolically like:

f = open(fname, mode='wb')

Open in new window


However the w and b are actually the flags that describe the wanted mode. In other words, the mode of the file says how it can be used, the flags are separate attributes that can be used for defining/describing the mode.
@pepr, that should be fopen()... the c-runtime buffered IO used "w", "r", "a" , optionaly added with "b"/"a"... (binary/ascii flag).
The UNIX file systems uses an    open(filename, ACCESS_MODE)
ACCESS_MODE = O_RDONLY, O_WRONLY  or O_RDWR   (O_RDONLY is default as it's value is 0).
augmented with flags: O_CREATE | O_NONBLOCK | O_TRUNC ....
see man 2 open
@noci: Yes. (I wrote "symbolically" in the text.) Anyway, the basic idea remains the same. I understand that O_RDWR is not a single bit while the O_CREATE like ones are. But this is more the way how it was originally implemented. Basically, adding the flag like O_CREATE modifies the mode to another one.

Yes, it is difficult to argue using bits and a human language. The original authors solved a problem. Maybe, they did not focus on the linguistic part. ;)
@Andy:

From my earlier comment (Look up a dictionary online if it is still unclear)
mode   A way or manner in which something occurs or is experienced, expressed, or done.
flag   A variable used to indicate a particular property of the data in a record.

So:

Access flag: O_RDONLY (0), O_WRONLY (1), or O_RDWR (2)
O_CLOEXEC flag: yes or no (1 or 0)
et cetera

So actually O_RDONLY can not be called a flag, because it doesn't indicate a particular property of the data in a record. It's always just 0. But if you zoom out, then the "access flag" can be for example O_RDONLY (0). So in summary:

A flag can be: O_RDONLY

but:

O_RDONLY is not a flag

because:

O_RDONLY = an access MODE

This explains: http://man7.org/linux/man-pages/man2/open.2.html

The argument flags must include one of the following access modes:
O_RDONLY, O_WRONLY, or O_RDWR.

The file creation flags are O_CLOEXEC,
O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TMPFILE, and
O_TRUNC.

That explains, why they don't call O_RDONLY a flag.

And this: https://en.wikipedia.org/wiki/File_descriptor

This table records the mode with which the file (or other resource) has been opened: for reading, writing, appending, and possibly other modes.

Reading, writing, appending et cetera are ways to open a file. And some "ways" can be combined. So the definitions are still correct.

So I think you're right @Andy, this explains both example of me, right?
@pepr:

flags are separate attributes that can be used for defining/describing the mode

I don't think you must see it like that.

There is an "access flag" that is: O_RDONLY (0), O_WRONLY (1), or O_RDWR (2). That are three modes, so in such a case, one mode is used to define/describe the access flag. The best way to see it is as:

Access flag:               O_RDONLY (0, access mode), O_WRONLY (1, access mode), or O_RDWR (2, access mode)
O_CLOEXEC flag:     yes or no (1 or 0)

And O_CLOEXEC can also be called a mode, because it can be used for opening a file in a certain way. However, e.g. O_RDONLY can not be called a flag. O_RDONLY can be the value of a flag, but it's not the flag.
A flag can be: O_RDONLY

but:

O_RDONLY is not a flag

because:

O_RDONLY = an access MODE   ---- Note it does not say it is a mode, it says it is an access mode.

The value O_RDONLY is a flag.  When it is used to open a file then the file is opened in readonly mode, it is how the file will be accessed.
A flag is commonly a secondary indicator that an action was taken.
Mode, type
.

let see fif the following might clear this up.

Four people are sitting at a table, they can not talk out of turn.
The turn is determined by who holds a specific item at the table, talking stick, some kind of single tool.
The mode here is random(order of people talking), the item being used to differentiate who has the right to talk is the flag.

I think I got this right.
@Andy: Yeah in your last post you're saying the same thing as I'm saying in the two post before. But meanwhile I have another example/situation.

Usually file permissions are also called "mode". The permissions have nothing to do with what purpose the file is opened (for example O_RDONLY), but the permissions are a different thing. You're not really opening a file with "permissions", the file has permissions. So how I can see the permissions as a "mode" with the definitions we have so far?
@arnold:

A flag is commonly a secondary indicator that an action was taken.

I don't think that's the way to look at it. Flags are saying something about how an action CAN BE taken, so in what way the action can be taken. So I can do shopping, but I can do it with a black|white shirt, furthermore I can do it with yellow|red shoes. So then you have two flags:

shirt-flag: black or white
Shoes-flag: yellow or red

If you're going to the shop with black shoes, then this can be seen as the mode. The mode says something about how the action IS taken and not how it CAN BE taken. So the mode can be:

black shirt, yellow shoes
black shirt

et cetera. But the mode can not be:

black shirt, white shirt
shoes color does not facilitate anything, they merely convey information.
i.e. using your example. a person wearing a yellow, red, white or black shoe does not convey how the person got there.
The mode of transport can be anything the person wants, by car, bus, train, taxi, bike, walking.

The type of shoes worn does not affect the transport mode. it only is sorted/differentiated once arrived at scene.

you could use the shoe color to set which mode of transport will be free to the user.

mode, flag
bus, red
taxi, yellow
walking, all
I think you are getting there, lets keep to files otherwise things just get more complicated.
Maybe this will help.  You can have some flags:  Readonly, Read, Writeonly, Write, Exclusive...
Now you can open a file with one or more flags say
Readonly AND Exclusive - Means the file is opened in a mode where you can read but not write and no-one else can open the file when you have it opened
or Read AND Write - Means you can both read and write to the file but so can anyone else at the same time

So you can use multiple flags but the file is only opened in one mode.  The mode the file is opened can vary depending on which selection of flags you specify.
if not mistaken, read only, read/write and write only are modes not flags.
create, exclusive, are the flags
i.e. conveys info that the file is being opened as  read/write with create, exclusive, tmpfile flag means the file is being created as a temporary file with exclusive access.


Unfortunately, the issue with terms often get messed up by different vendors tech write-ups using their own .....
@arnold: But it doesn't really matter which metaphor exactly we're using. In the end, the conclusion is that the terms "mode" and "flags" are used in all kind of different ways. It's not really possible to make one general definition of it.

Just try to make a definition and explain this: http://man7.org/linux/man-pages/man2/open.2.html

The argument flags must include one of the following access modes:
O_RDONLY, O_WRONLY, or O_RDWR.

The file creation flags are O_CLOEXEC,
O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TMPFILE, and
O_TRUNC.

And this: https://en.wikipedia.org/wiki/File_descriptor

This table records the mode with which the file (or other resource) has been opened: for reading, writing, appending, and possibly other modes.

And this:

the file mode (permissions),

With permissions being:
owner rwx
group rwx
other rwx

For example, what is the "action" in when it's about permissions? Just try to find a definition that stays correct for all these examples.
For me a flag is something you use to specify a course of action and a mode is the result after the action.
@Andy:

So you can use multiple flags but the file is only opened in one mode.  The mode the file is opened can vary depending on which selection of flags you specify.

Then you will run into problems with "file permissions". This is also called mode, but a file is not opened with a certain file permission. The file permissions are part of the file itself.

@arnold:

read only, read/write and write only are modes not flags.

But Andy and me are not saying that they are also flags. However, a flag can have read_only as value, so a flag can be read_only but a flag is not the same thing as read_only.
File permissions (maybe I misunderstand you) are restrictions/allowances that the file system put on you with the actions you can take on a file.  Not the same as opening a file.  (Your attempt to open a file might fail if you do not have the necessary permissions but if it succeeds then the file is opened in the mode you requested.)
My advice: don't try to define EVERYTHING according to dictionaries...

why? searching for proved definitions seems the most rational approach for me. naming generally is quite an individual thing, especially for programmers who tend to have a special technical view to their names.

i looked for synonyms and found modus and way as the most used alternatives to mode.

'modus' as in the latin 'modus operandi' was used for the defined methods and processes some action or process is taken.
'way' or 'manner' would mean the 'how to' of some doing as well . no single synonym of mode could be substituted by 'flag'.

for 'flag' there are synonyms like 'marker', 'pin', sign', or 'label'. the origin meaning of flag as a 'banner' also tells that 'flag' was a visible 'property' which an object may or may not possess or which you would 'stick' to something.

in my opinion none of those synonyms would be used as a synonym for 'mode'.

so, for me mode and flag cannot be exchanged while the first refers to an process or action and the second is a status or property of an object.

Sara
File permissions (maybe I misunderstand you) are restrictions/allowances that the file system put on you with the actions you can take on a file.  Not the same as opening a file.  (Your attempt to open a file might fail if you do not have the necessary permissions but if it succeeds then the file is opened in the mode you requested.)

But the one access mode (O_RDONLY, O_WRONLY, or O_RDWR) is used to open a file in a certain way. This is the system call open(). It's not that this action doesn't take place, because the file goes into the "open file table". However, if the reading starts it will fail if there are no permissions. So you're pretty right what you're saying. However, the permissions are also called the mode. According to your definition, permissions are not a mode. So that's the problem with that definition.

@Sara:

so, for me mode and flag cannot be exchanged while the first refers to an process or action and the second is a status or property of an object.

We know, but the problem is that it's not always used like that in reality (by respected/official websites/manuals et cetera).
>>However, the permissions are also called the mode.

I'd qualify that to the mode of opening, not just the mode.  (Note after the action is performed.)
Mode of opening? The mode of opening is for example reading, but that is what O_RDONLY is about. Let's say there are only write permissions, then the mode of opening is still "read"? It's also used as argument flag in the open() system call. Or you mean it differently?
@andy: please read man 3 open (Posix manual)  go to  examples. 3rd argument. (or O_CREAT flag description).
You mean this, right? See: https://linux.die.net/man/3/open

and the access permission bits (see <sys/stat.h>) of the file mode shall be set to the value of the third argument taken as type mode_t modified as follows
yes.
But that is still using a set of flags and, for my understanding, the file mode is the mode of the file when opened.  (Flag is not the same as mode).


ps.  If one defined a macro such that cat_t is identical to mode_t could one not replace all lines in the examples with
cat_t cat =  ....   instead of mode_t mode = .... and then pass the variable cat into the fopen call instead of the variable mode.  Things would still work and I doubt anyone would be asking why are animals being used in opening a file.
Yeah I understand you and I think you're right. Actually a file can be opened for reading, but the same file is opened with some specific file permissions. So if you're seeing it like you're saying:

mode   A way or manner in which something occurs or is experienced, expressed, or done.

Then actually you're right, because it's a way or manner to open a file. You can open a file with file permissions "A", but you can also open a file with file permissions "B". This doesn't have to affact the "action". That's why the examples of @arnold are wrong. I can just wear a black|white shirt to go somewhere. It doesn't really affact something (immediately), but it's a way to do something.

Actually if you see a flag as something that is yes/no, then that yes and no can be seen as modes. So actually I think you gave the right answer ;), but I'll leave the question still open for a while, so people can comment on it.

Only about:

But that is still using a set of flags and, for my understanding, the file mode is the mode of the file when opened.

It's not THE file mode, because actually you have:
- Permissions mode
- Access mode (O_RDONLY, O_WRONLY, or O_RDWR)
- Other modes (yes/no from flags such as O_CREAT)
it's not always used like that in reality (by respected/official websites/manuals et cetera).

that is not a good point. websites or docs must describe what them was given from the designers and developers who seldom were highly gifted name givers.

the access 'modes' are defined in fcntl.h

#define O_ACCMODE	00000003
#define O_RDONLY	00000000
#define O_WRONLY	00000001
#define O_RDWR	00000002

Open in new window


O_RDONLY is 0 what means the statement

The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, O_RDWR
is wrong. open will apply the O_RDONLY anyway. more precisely, the O_RDONLY is not a bit constant as it has no bit set. but, beside of  the wrong statement, you can specifiy access mode read-only by not or'ing O_WRONLY and O_RDWR. but if the docs are not accurate in a point where they could have known better, how should they be blamed for using the wrong names where they have no choice?

why is the 'flags' argument called 'flags' and not 'modes'? obviously the programmers wanted to spare arguments. with 'flags' they can pass any combination of up to 32 bit constants (actually it is only 20 different ones but still a huge amount of different combinations) . even if there is only a rather small set of valid combinations, it would not be reasonable to make modes out of those valid combinations. such a list would spoil the high flexibility and expandibility the current solution has.

moreover, you should see that an argument named 'flags' is not the same as an argument named 'flag'. while the latter indeed should be a flag and not a mode, i would not necessarily demand that a 32-bit container named flags contains flags and nothing else. in my opinion it is not so much a wrong naming if some of the bit constants which can be passed with 'flags' are modes. would it be better if the argument was named 'mode_and_flags'? moreover, we could reason that the access modes are deliberately designed as 'flags' just to be able to pass them together with real flags with one 32-bit argument. so, simplicity beats linguistical accuracy.

Sara
As an addition, after the file has been created, the file permissions can not be called a mode anymore. This is only when a file is opened in combination with creat (O_CREAT). But then the definition is still correct.
@sara:

The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, O_RDWR
is wrong. open will apply the O_RDONLY anyway.

What do you mean? It will apply O_RDONLY OR O_WRONLY OR O_RDWR? Or do I understand you wrong? I would say, this can be seen as the access-flag and the access-flag can have the following values/modes:

O_RDONLY
O_WRONLY
O_RDWR

I don't see why this would be wrong?
O_RDONLY is defined with 0 what means that no bit is set.

if you assign it to flags like with

unsigned flags = O_RDONLY | O_NONBLOCK;

Open in new window


it has no impact and the statement is equalivent to

unsigned flags = O_NONBLOCK;

Open in new window


the open flags are a special case of being used in a bit combination anyhow. if you read the doc of those flags, you won't see a flag which really is essential for a normal file open of an existing file. see the following table which compares the fopen modes with open flags:

                  ┌─────────────────┬───────────────────────────┐
                  │  fopen() Mode   │       open() Flags        │
                  ├─────────────────┼───────────────────────────┤
                  │r or rb          │ O_RDONLY                  │
                  │w or wb          │ O_WRONLY|O_CREAT|O_TRUNC  │
                  │a or ab          │ O_WRONLY|O_CREAT|O_APPEND │
                  │r+ or rb+ or r+b │ O_RDWR                    │
                  │w+ or wb+ or w+b │ O_RDWR|O_CREAT|O_TRUNC    │
                  │a+ or ab+ or a+b │ O_RDWR|O_CREAT|O_APPEND   │
                  └─────────────────┴───────────────────────────┘

Open in new window


in my opinion open flags is a bad sample for to discuss the difference of flag and mode as it seems to me that the developers simply used the word flags as a name for the used bit set technique where each bit value represents one flag.

Sara
I don't follow you ;). Let's do it step by step for me.

See: https://whatis.techtarget.com/definition/flag

In programming, a flag is a predefined bit or bit sequence that holds a binary value. Typically, a program uses a flag to remember something or to leave a sign for another program.

Do you agree with a definition like that? And if no, what would you change? Then we have:

unsigned flags = O_RDONLY | O_WRONLY | O_RDWR;

Then the flag is 0,1 or 2. Why with "0" no bit is set? A bit is 0 or 1, so 0 can also be seen as a bit?
yes, i do. i would expand the definition such that a flag may have more than 2 states, e. g. red/green/blue but it is good.

unsigned flags = O_RDONLY | O_WRONLY | O_RDWR;

you were mixing up bits and values. each bit may have value 0 or value 1.

the unsigned flags has 32 bits indexed from 0 to 31. if now bit 0 of flags stores whether O_RDONLY is set or not, you can test this by a bit mask which has the bit 0 set to 1. then, if do 'AND' with flags and mask, the result is 1 if the bit 0 is set and 0 if the bit was not set.

const unsigned int  BIT_0_IS_SET = 0x1; 
if ((flags & BIT_0_IS_SET) != 0)
{
       std::cout << "bit 0 of flags is set." << std::endl;
}
else
{
        std::cout << "bit 0 of flags is not set." << std::endl;
}

Open in new window



obviously the difference between BIT_0_IS_SET and O_RDONLY is that the first is 1 and the second is 0.

so using BIT_0_IS_SET you can test if the flag is set in flags.

if you do the same with O_RDONLY, the result is always 0, because (flags & 0) == 0. so if you have initialized flags with 0 (what is the only initialization that makes sense) what means that no bit is set, you will not get any difference to this initial state if you assign O_RDONLY to flags.

there would be a way to use O_RDONLY for a bit combination nevertheless. you would not use O_RDONLY as a value but as a bit position.

flags = 1<<O_RDONLY;   // shifts the bit 0 of 1 (which is set) by 0 positions to the left

if ((flags & (1<<O_RDONLY)) != 0)
{
       std::cout << "O_RDONLY bit is set." << std::endl;
}
else
{
        std::cout << "O_RDONLY bit is not set." << std::endl;
}

Open in new window


but, you see from examples that they use the options as values and not as bit positions.

that means  O_RDONLY is not well defined. it actually doesn't make a difference to the initial value and you can omit it as it has no impact. the only thing it does, it defines a default for flags and might improve the readability. but, it would have been much better if O_RDONLY = 1, O_WRONLY = 2, O_RDWR = 4, .... because the flags then could be tested whether the O_RDONLY was set by using the standard bit test with AND operator. also, the default could be changed to any other flag with out changing the values.

by the way, in many standard c headers they have both. they have an enumeration with values 0,1,2,3,...  and a corresponding enumeration for the bit position.

enum 
{
        eReadOnly = 0,
        eWriteOnly = 1,
        eReadWrite = 2,
        MAX_ACCESS_MODES    // value = 3 automatically set from compiler
};

enum 
{
        eBitReadOnly = (1<<eReadOnly),    // 1
        eBitWriteOnly = (1<<eWriteOnly),   // 2
        eBitReadWrite = (1<<eReadWrite ),// 4
};

Open in new window


the first enumeration was used for indexing in an array while the second was used for bit combinations.

Sara
you were mixing up bits and values.

Where exactly?

But before we're going too much into the details, first let's clarify/summarize what you're trying to make clear. That's still not clear for me, because you're already going too much into the details. Again let's start with:

In programming, a flag is a predefined bit or bit sequence that holds a binary value.

We're saying:
O_RDONLY = 0
O_WRONLY= 1
O_RDWR = 2

So the associated flag can be 0,1 or 2. Now you're saying that this flag is 0 by default, so this is incorrect. It's only correct if it would be:
O_RDONLY = 1
O_WRONLY= 2
O_RDWR = 3

Is that what you're trying to say? Or what can not be called a mode or a flag according to you? And what's your definition of a flag/mode?


Because:

In programming, a flag is a predefined bit or bit sequence that holds a binary value.

0 (O_RDONLY) is a value, 1 (O_WRONLY) is a value, 2 (O_RDWR) is a value, so the flag holds one of these values, so what's wrong about it? And 0,1 and 2 are not binary values but the binary values (0,1,10) can represent 0,1,2.
we started from the definition  "a flag is a predefined bit or bit sequence that holds a binary value".

if we ignore the "bit sequence" for the moment, we still have that "a flag is a predefined bit that holds a binary value.

i hope we do agree that this implies that if the predefined bit is 1, the flag is set and if the predefined bit is 0, the flag is not set.

if so, you can't set a flag with the value 0. assigning a zero flag does nothing.

now to the bit sequences and bit containers:

flags is a bit container with 32 bits. that means you may have 32 flag bits where each bit has a different meaning. bit  sequences are 2 or more consecutive bits which have a meaning only if you look at all of them. for example if you want to store a flag with either uncolored/red/green/blue you need a bit sequence of 2 bits in the bit container. in case of flags we have only single bits (not sequences) for each flag.

for example

O_CREAT = hex 00000100 == binary 1 00000000 00000000           has bit 16 set and all other bits are  zero
O_SYNC  = hex 00002000 == binary 10 00000000 00000000 00000000 has bit 25 set and all other bits are zero. 

Open in new window


so, if you do

flags = O_CREAT | O_SYNC;

Open in new window


the result is hex 00002100 == binary 10 00000001 00000000 00000000 and bit 25 and bit 16 are set.

if you now do

flags = O_RDONLY | O_CREAT | O_SYNC;

Open in new window


you will see no difference. the result still is hex 00002100 == binary 10 00000001 00000000 00000000 and bit 25 and bit 16 are set.

that is because O_RDONLY is 0 and adds nothing to a bit container or bit sequence.

flags = O_WRONLY | O_CREAT | O_SYNC;

Open in new window


would make hex 00002101 == binary 10 00000001 00000000 00000001 and bit 25, bit 16 and bit 0 are set.

do you now see that O_RDONLY isn't a bit flag but on the best a default. to know whether O_RDONLY flag applies in flags you have to test flag O_WRONLY and O_RDWR. if one of them is set, the O_RDONLY is not set. if neither O_WRONLY nor O_RDWR is set, we fall back into the default case and can say O_RDONLY is set. (note, if we would say that bit 0 and bit 1 of flags is a bit sequence of length 2 which means the access mode, we have that interpreting the two bits as an 2 bit integer the read-only is 0, the write-only is 1, and the read-write is 2. but anyway, you don't have a chance to say that none of the 3 modes applies what could make sense if you only want to check whether the file exists but neither want to read or write).

Note, the open function and the flags have been part of the very early C language compilers. at this times the C compilers run on 16-bit operation systems and memory was counted in kilobytes. so, sparing a bit by defining O_RDONLY as the default allowed to use an 8bit or 16bit variable for the bit container.  later, when the bit containers grow to 32 bit or even 64-bit, they didn't change the old flag values because that would have made millions of programs invalid as the system functions run in shared libraries (dll's) which mostly were linked at runtime and the usage of the right bit flags in the interface necessarily was crucial.

Sara
But again you're going immediately into details. Take it easy ;). You're saying:

if we ignore the "bit sequence" for the moment, we still have that "a flag is a predefined bit that holds a binary value.

Why you're ignoring the "bit sequence". Just keep it there, because it's also in the definition.

Furthermore:

i hope we do agree that this implies that if the predefined bit is 1, the flag is set and if the predefined bit is 0, the flag is not set.

The flag holds a value, so it can hold 0 or it can hold 1. In both cases, the flag can be seen as set.

And:

in case of flags we have only single bits (not sequences) for each flag.

Definition:

a flag is a predefined bit or bit sequence that holds a binary value
Why you're ignoring the "bit sequence". Just keep it there, because it's also in the definition.
because none of the flags that were used for open are bit sequences. they are only bit flags (beside of O_RDONLY which is nothing).

The flag holds a value, so it can hold 0 or it can hold 1. In both cases, the flag can be seen as set.

the bit flag only has value 0 or 1 if you isolate the single bit from a container. if defined as a constant like it is in fcntl.h it has binary values 1, 2, 4, 8, 16, 32, 64, ....   (alternatively hex 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, ....). all this constants have exactly 1 bit set and all others not set. hence you can add each bit flag to flags without spoiling any other bit already stored in the container and you can test whether the flag is set in the container by checking if the bit value at the corresponding bit position is 1 or 0.

a flag is a predefined bit or bit sequence that holds a binary value
look at the sys/fcntl.h. none of the flag constants from O_WRONLY to O_TEMPFIL has a value which would set more than 1 bit. the only execption is O_ACCMODE which has a value of 3 what is binary 11 and covers exactly the two bit flags O_WRONLY, and O_RDWR.

so, with O_ACCMODE you could test

if ((flags & O_ACCMODE) != 0)
{
     // either bit 0 or bit 1 of flags (or both) were set, what means that either O_WRONLY or O_RDONLY flag was stored.

Open in new window


i ignored it because O_ACCMODE was not discussed yet (interesting: because bit sequences often can store modes or options or enumerations, while flags are either true or false).

Sara
because none of the flags that were used for open are bit sequences. they are only bit flags (beside of O_RDONLY which is nothing).

We have this flag:
O_RDONLY = 0
O_WRONLY = 1
O_RDWR   = 2

This flag can hold three different values. A bit is only 0 or 1, so it can only hold two values. You need a sequence of two bits to hold three values. So in our situation there is a sequence involved.

the bit flag only has value 0 or 1 if you isolate the single bit from a container

No, not if it's about a sequence of bits.
But wait, just summarize (in short, two/three sentences) what you're trying to make clear? You don't have to explain it yet, but what's your point? According to you, what exactly can not be called a flag or a mode? I think we're talking about different things now.
Often when more than one bit is involved in a flag it is just a shortcut to help readability / reduce typing required.

eg.
Bit_ONE = 1
Bit_TWO = 2
Bit_THREE = 3  which is the same as Bit_ONE |Bit_TWO

so one can use
Bit_ONE | Bit_TWO
or alternatively just
Bit_THREE
See: https://whatis.techtarget.com/definition/flag

In programming, a flag is a predefined bit or bit sequence that holds a binary value. Typically, a program uses a flag to remember something or to leave a sign for another program.

I'm thinking a bit more about this part:

a flag is a predefined bit or bit sequence that holds a binary value

What exactly is a "binary value"? Because a value (in computing) always consist of one or more bits (bit or bit sequence). So then a flag is just a predefined value. So it's just a value that doesn't change.

But that's why I'll first wait for what @sara actually tries to say:

But wait, just summarize (in short, two/three sentences) what you're trying to make clear? You don't have to explain it yet, but what's your point? According to you, what exactly can not be called a flag or a mode? I think we're talking about different things now.

@noci said:

In ICT A flag commonly is a yes/no condition, where a mode is some enumerable condition. (finite list of options).

But this is not true according to the definition above. See for example: https://elixir.bootlin.com/linux/latest/source/include/uapi/asm-generic/fcntl.h

#define O_ACCMODE      00000003
#define O_RDONLY      00000000
#define O_WRONLY      00000001
#define O_RDWR            00000002
#ifndef O_CREAT
#define O_CREAT            00000100      /* not fcntl */
#endif
#ifndef O_EXCL
#define O_EXCL            00000200      /* not fcntl */
#endif
#ifndef O_NOCTTY
#define O_NOCTTY      00000400      /* not fcntl */
#endif
#ifndef O_TRUNC
#define O_TRUNC            00001000      /* not fcntl */
#endif
#ifndef O_APPEND
#define O_APPEND      00002000

So these values are all predifined, but they are not yes/no 1/0, it's just a predefined value.
Numbers starting with 0 in C are ocatal, starting with 0x = Hex, starting with 1..9 = decimal

O_TRUNC = 01000  = 01000 octal!!!   (yes truncate)  
                     =  001000000000   (binary)
O_NOTRUNC = 00000000 = 0...    (no truncate)

So O_TRUNC is the YES variant
O_APPEND = 02000 (also octal )
                      =  010000000000   (binary)
O_NOAPPEND = 0...

So irrespective of how you want to call those numbers they represent 1 bit (yes, no).

To use TRUNCATE & APPEND:
O_TRUNCATE | O_APPEND  = 00003000   (octal)
                                              =  011000000000   (binary)
| = bitwise or

Please if you need to read some language try to learn some aspects of a language....
Almost all computers work in binary arithmatic, with some notable exceptions:
- Differential Engine Mechanical,  (Decimal, Charles Babbage)  Design 1820's, https://en.wikipedia.org/wiki/Charles_Babbage#Difference_engine 
ultimately built in 1990's
- Eniac  (Decimal, John von Neuman), electornic
- MIX ( (Model 1009, Arbitrary number system, Donald Knuth, https://www.i-programmer.info/history/people/98-donald-ervin-knuth.html?start=1 )
 
Again are you sure you want to pursue you investigations in computers..., i think it will become a steep learning curve.
Good luck again.
Again are you sure you want to pursue you investigations in computers..., i think it will become a steep learning curve.
Good luck again.

I'm just asking a basic question. This is just a question about definitions. If I see how many people have different thoughts about it, then it's not that weird that I'm trying to get some things clear. And as I already said to you, I don't need to know everything so don't push me in that direction again. Just stop talking like that to me, because this is not the first time you're saying something like that to me. If you're here to demotivate people and to look down on people, then please just leave. If you want to talk about the question itself, fine! Then just keep on-topic. Try to work on your social skills I would say (and I don't mean this bad). Maybe you're not even doing it on purpose, but then at least you're aware of it now.

But a computer are just bits, so in the end an octal number also consist of a sequence of bits. So again I don't understand why you're starting about hex, octal, decimal et cetera.

O_TRUNC = 01000  = 01000 octal!!!   (yes truncate)  
                     =  001000000000   (binary)
O_NOTRUNC = 00000000 = 0...    (no truncate)

The thing is that O_TRUNC is called a flag. This has just 1 value, so it's not yes or no or something like that. If you see O_TRUNC and O_NOTRUNC together as the flag, then you would be right. Now a flag is just a predefined value. So you're just wrong and acting like I'm saying really weird things? I'm just trying to follow the definition first and get that clear.

Probably you're that long in this business (and you already know that much) that you're not even aware of all the bad/incorrect definitions that are used when it's about some subjects. You just accept it, because you're used to it and probably you don't even care about it or you are just not aware of it. I think that's a bad thing (especially for new people) and sometimes it's good to redefine things or to look at it some longer/better. I have the feeling from your lasts posts that that frustrates you, but sometimes it's good to look at something with a fresh eye or from a different point of view (for example open file table instead of structures). And that doesn't mean I want/need to know everything or every detail. I'm just trying to get an answer on some basic questions (in this case flag/mode). And yes, I just don't easily accept an answer, because I really want to understand the answer and I know that can be frustrating sometimes. But yeah what are my alternatives? Just leave and acting like I understand it?
My last comment: i don't try to demotivate people.., don't intend to  but if get the impression sometimes people are drowning.. (and i am also a lifeguard , swimming instructor.. so i don't like to see people drowning) then people need to get ashore and either get the advice to not enter the water again or the advice to start swimming in a regular swimming instruction.
From the way you summarize the answers and doubt some given things my impression is you are in over your head.  
I can be wrong, but i will honour your request to leave it with you.
BTW (a question like "what is a binary value" gives me that impression as it's name literary mean 2 way value... customary we give that value 0, 1 )
two wil become 1+ 1,in binary causing a carry  (just like 9 + 1 in decimal does)   + 10 is the result etc. etc.

1 like 0 are "just values". Yes or no are both also just values.   a, b,c are also just values.  (the ASCII number they got are 61-63 Hexadecimal, or 97-99 decimal).    
It gets meaning when people assign a meaning to it.
Computers care about 00001 or 00010   people are better with symbols. and use names for this O_WRONLY or O_RDWR if this is concerning an open() function call.   0001 might also mean x in the protection mask, where 0010 means w .  etc. etc.
Binary strings are tedious and long (and slightly alien to our decimal thinking)... In computers until the 1980's octal was the customary representation after that hexadecimal became the norm for compact number representation.
Hence the old stuff specifies things in octal... and a 0400 represents 1 bit of information only shifted left by 8,,, otherwise written as 1<<8 or
1 * 2^8  
Octal is also near enough in decimal to be slightly easier to calculate with then hex is.  just remember 0400+ 0400  =  1000 in octal and 800 in  decimal.
My last comment: i don't try to demotivate people.., don't intend to  but if get the impression sometimes people are drowning.. (and i am also a lifeguard , swimming instructor.. so i don't like to see people drowning) then people need to get ashore and either get the advice to not enter the water again or the advice to start swimming in a regular swimming instruction.
From the way you summarize the answers and doubt some given things my impression is you are in over your head.  
I can be wrong, but i will honour your request to leave it with you.

My request it to leave IF you're here to demotivate people and to look down on people. So if you're seeing it as honoring my request, then that says something about the second part of the sentence. Then that's your own conclusion/choise, not mine. But the thing is that I can swim perfectly fine, but I'm swimming in dirty watter, so the instuctor need to take care of the water. He must not just send the swimmers away. And:

From the way you summarize the answers and doubt some given things my impression is you are in over your head.

Usually if I don't understand something, it has a good reason. Then I've read somewhere something (for example a definition) and then later on that doesn't seem to be correct. For example: https://www.experts-exchange.com/questions/29121598/Why-less-0-test-txt-doesn't-output-Missing-filename-output-redirection.html
So then you have no idea what I'm talking about, but with the information I saw, it's not that unlogical. With the "open file table" was also a good example. In the end it turned out that you had a wrong idea about it in your head (not how it works in the end because you know, but how "open file table" was used in that context).

And:

a question like "what is a binary value" gives me that impression

If we're trying to understand a definition and "binary value" is in it, then it's good to check if everyone has the same idea about it. Probably you just think, oh he doesn't even know that. But the reason why I'm asking it, is to check if people have the same idea about it, because I already faced a lot of terms that in the end are used differently by people on the internet. And I'm glad I'm doing that, because otherwise we would be still talking about the "open file table" right now. That's also why I'm trying to find out how I have to see flag/mode.

You're saying:

"what is a binary value" gives me that impression as it's name literary mean 2 way value... customary we give that value 0, 1

But other people see it also as a value expressed in the base-2 numeral system. So that doesn't need to be 0 or 1. For example the number 15 can also be expressed in a base-2 numeral system. So again this is a good example that you're thinking that I'm asking a "stupid" or unnecesarry question, but in the end it's not that unnecesarry to discuss it.

So if you still want to join this question, you're more than welcome. I think you can contribute positively with your knowledge. However, it's your own choise ...it's up to you and you have to decide if you can handle my way of questioning. And trust me, if you would see it a bit more from my point of view, then you would understand why I'm asking some things. And with for example that question about i/o redirections, then in the end it becomes clear why I was asking some things and what the "problem" was or what was behind my questions.

So if you want to still join the discussion, then we have to start from here:

The thing is that O_TRUNC is called a flag. This has just 1 value, so it's not yes or no or something like that. If you see O_TRUNC and O_NOTRUNC together as the flag, then you would be right. Now a flag is just a predefined value. So you're just wrong and acting like I'm saying really weird things? I'm just trying to follow the definition first and get that clear.

Because there you and the definition are going two separate ways. So then there are three options:

1. Or the definition is wrong (then just say it's wrong and tell why it's wrong).
2. Or maybe you have to see it differently.
3. Or I am wrong (but then you have to say exactly what's incorrect about it in that quote above).

P.S. In the definition a flag is a PREDEFINED value, so it's not a 0 or 1, it's a 0 and stays a 0 or it's 1 and stays 1. Or it's for example 15 (in base-2 numeral system) and it stays 15.That's a difference. For example the flag O_RDONLY is just 0, not 0 or 1.

And other people are just not "drowning" (with for example mode/flag), because they just think, ah skip it (or they think they understand it but they didn't think of some cases). Otherwise I would just get 1 simple answer and everyone would agree with it and we would be done. However, that's not the case. So the "water" is just dirty and I'm trying to make the water a bit clear. Then first I have to ask stupid/basic questions to get some things clear first. And usually then most of the people still think: too much time/energy/frustration -> just skip it. And then in the end, everything stays unclear, and different people see things differently.
a flag is a boolean property

in this context, a mode is a way more generic term that may refer to any number of properties which are not necessarily boolean...

--

in this context, the flags are low-level settings that determine whether you can read or not, write or ,not, create the file automatically, fail if it does not exist, fail if it already exist, place the pointer at the end of the file rather than offset 0... note that some flag combinations may not work.

still in this context, modes are preset set of flags so users do not need to bother setting 3 or 4 different flags from about a dozen every time they need to open a file. they are merely comprehensive preselections that cover most use cases.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.