Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C linux internal difference between O_WRONLY and O_RDWR if only writting

Tags:

c

linux

file-io

I know open offers these mutually exclusive flags: O_RDONLY, O_WRONLY and O_RDWR.

I want to know: Are there any performance issues (even of it's just a fraction of a ms) or different ways of treating the file if the file is opened as O_RDWR and

  • I only write to the file. (Versus opening as O_WRONLY)
  • I only read data from the file. (Versus opening as O_RDONLY)
like image 429
brunoais Avatar asked Oct 15 '25 21:10

brunoais


1 Answers

First, you seem to have mistyped (inverted) in the two cases of your description the write/read tags. As to what you ask, the VFS, in its various structures, keeps track of desired access rights by flags. The read/write flag is typically a different bit in the same flag (multi) byte. When a process request access as read or write, the kernel checks if it has the requested access rights, and proceeds accordingly. As setting 2 bits doesn't increase your execution time, you should see no difference as later access is the same. Using the proper tag is simply good style and part of file protection.

like image 197
gnometorule Avatar answered Oct 18 '25 09:10

gnometorule