Flag of open function __ function

Source: Internet
Author: User

2.1. File read and Write permission
(1) Linux Chinese parts have read and write permission, we open the file in open can also be accompanied by a certain permission to explain (for example, O_rdonly is read-only open, o_wronly means open in a write-only way, o_rdwr means to open in a readable and writable way) When we have some kind of permission attached to the open file, the open file can only be manipulated by that permission.

2.2. Change the contents of the file
(1) When we open the existing and internal content of the file will appear in 4 cases (the original content disappeared (using the O_TRUNC flag), the new content is added after the original content (using the O_append flag), new content is added before the original content , the original contents of the file are not changed when the file is not read and written (the default is not to use the o_append and O_trunc flags), and if O_append and O_trunc are present at the same time, the O_TRUNC flag function is blocked by code validation o_append flag.

2.3. Exit process or procedure
(1) When the program (process) in the previous operation failed to carry out the subsequent operation is not possible, we should be in the previous error monitoring program to end the entire program (process), should not let the program (process) continue to run.
(2) in the main function of the return keyword, the general principle is the normal termination of the program return 0, the program abnormally terminated return-1, not commonly used.
(3) The formal termination of the process (program) should use Exit or _exit or _exit one, the general principle is that the process normally terminates exit (0), the program terminated exit (-1), often used.

2.4. Open files that do not exist
(1) When we open a file for the existence of the file there will be 2 scenarios (create and open files that do not exist, if the file exists, the file will be modified to reset (using the O_CREAT flag), create and open a file that does not exist, and if the file exists, it will have an error. Modifying the file (using both the O_EXCL flag and the O_CREAT flag) is not created.
(2) When you use the O_CREAT flag to create a file, the Open function can use the 3rd argument mode to specify the permissions of the file you want to create; mode uses 4 digits to specify permissions, with the following 3 important, which corresponds to the permission flags for the target file we are creating , for example, to create 1 readable writable files that are not executable by 0666.

2.5. Blocking and Non-blocking
(1) If a function is blocked, then the current process may be blocked when we call the function (in essence, the condition of the function to be finished is not available, cannot be done at present, wait for the condition to be ripe), the function is blocked and cannot be returned immediately; If a function is non-blocking, Then we must return immediately after calling this function, but the function does not necessarily complete the task.
(2) Blocking and non-blocking are two different design ideas, and there is no good or bad; In general, blocking results are guaranteed but time is not guaranteed; non-blocking time is guaranteed but results are not guaranteed.
(3) The API provided by the operating system and the library functions encapsulated by the API, many of which are designed to be blocked or non-blocking in themselves, so when our application calls these functions it must be clear whether the function is blocking or non-blocking.
(4) By default, we open a file is blocked (if there is a problem when reading and writing to the file when the file will cause blocking), if you want to open the file in a non-blocking way, then flag to add the O_NONBLOCK flag Blocking and non-blocking are used only for device files (Linux hardware devices such as serial ports, I2C communications devices, LCD) and not for normal files.

2.6. The underlying blocking and non-blocking  
(1) write by default simply writes content to the underlying buffer to return, and then the underlying (the code in the operating system that implements open, write these operations, It also contains the code for the underlying hardware, such as read and write hard drives in the OS, and automatically synchronizes the contents of the underlying buffer to the hard disk at the right time; the design mechanism is to improve the hardware operation performance and hardware life; but sometimes we want the hardware not to wait and write our content directly to the hard disk, then use the O_ The sync flag (write blocking waits for the underlying finish write before returning to the application tier).

<code class= "Hljs cpp has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " ><span class= "Hljs-number" style= "Color:rgb" (0, 102, 102); Box-sizing:border-box; " >2.</span>open_exit <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; "
 >/* * Company: XXXX * Author: Rston * Project: Open function Flag Detailed description * Function: File read/write permission, change file content, exit process or program, open nonexistent file. */</span> <span class= "hljs-preprocessor" style= "Color:rgb" (68, 68, 68); Box-sizing:border-box; " > #include <stdio.h></span> <span class= "hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " > #include <sys/types.h></span> <span class= "hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " > #include <sys/stat.h></span> <span class= "Hljs-preprocEssor "style=" Color:rgb (68, 68, 68); Box-sizing:border-box; " > #include <fcntl.h></span> <span class= "hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " > #include <unistd.h></span> <span class= "hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " > #include <string.h></span> <span class= "hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " > #include <stdlib.h></span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >int</span> Main (<span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); box-sizing:border-box; " >int</span> argc, <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >char</span> **argv) {<span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); box-sizing:border-box; " >int</span> fd =-<span class= "Hljs-number" style= "color:rGB (0, 102, 102); Box-sizing:border-box; "                                >1</span>; <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//FD is file descriptor, document descriptor </span> <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >int</span> ret =-<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; "                               >1</span>; <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//is used to receive read return values, to determine whether to read files successfully </span> <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >char</span> buf[<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); box-sizing:border-box;" >100</span>] = {<span class= "hljs-number" style= "Color:rgb" (0, 102, 102); box-sizing:border-box; "                        >0</span>}; <span class= "hljs-comment" style= "color:RGB (136, 0, 0); Box-sizing:border-box; " >//build buffer to store content read out from file </span> <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >char</span> bufwrite[<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); box-sizing:border-box;" >20</span>] = <span class= "hljs-string" style= "Color:rgb" (0, 136, 0); Box-sizing:border-box; "       > "Linux is great" </span>; <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " The >//build buffer holds the content to be written to the file </span> <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//Open Test.txt file, if the file does not exist to create (permission 666), if the file exists the error </span> FD = open (<span class= "hljs-string" style= "Color:rgb" (0, 136, 0); Box-sizing:border-box; " > "test.txt" &LT;/SPAN&GT; O_rdwr | O_creat | O_EXCL, <span class= "Hljs-number" style= "Color:rgb" (0, 102, 102); Box-sizing:border-box; "             
    >0666</span>); <span class= "Hljs-keyWord "style=" Color:rgb (0, 0, 136); Box-sizing:border-box; " >if</span> (-<span class= "Hljs-number" style= "Color:rgb" (0, 102, 102); box-sizing:border-box; " >1</span> = = FD) <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//to determine whether the file was opened successfully, it can also write if (FD < 0) </span> {<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >printf</span> (<span class= "hljs-string" style= "Color:rgb" (0, 136, 0); box-sizing:border-box; "
        > "Open File error.\n" </span>); <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; "                            >//return-1; Use return or exit to exit the process or program </span> <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >exit</span> (-<span class= "Hljs-number" style= "Color:rgb" (0, 102, 102); box-sizing:border-box; " >1</span>); } <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >else</span> {<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); box-sizing:border-box; ">printf</span> (<span class=" hljs-string "style=" Color:rgb (0, 136, 0); box-sizing:border-box; " > "Open File sucess.
    FD =%d.\n "&LT;/SPAN&GT; FD"; } <span class= "Hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " > #if 1 </span> ret = write (FD, bufwrite, <span class= "hljs-built_in" style= "Color:rgb" (102, 0, 102); box -sizing:border-box; "    >strlen</span> (Bufwrite)); <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//write content to the file </span> <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >if</span> (Ret < <span class= "Hljs-number" style= "Color:rgb" (0, 102, 102); box-sizing:border-box; "      >0</span>)                              <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//to determine whether the content was successfully written to the file </span> {<span class= "hljs-built_in style=" Color:rgb (102, 0, 102); Box-sizing: Border-box; " >printf</span> (<span class= "hljs-string" style= "Color:rgb" (0, 136, 0); box-sizing:border-box; "
        > "Write File errot.\n" </span>); <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//return-1;</span> <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >exit</span> (-<span class= "Hljs-number" style= "Color:rgb" (0, 102, 102); box-sizing:border-box; "
    >1</span>); } <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >else</span> {<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); box-sizing:border-box; ">printf</span> (<span class=) HLjs-string "style=" Color:rgb (0, 136, 0); Box-sizing:border-box; "
        > "Write%d bytes to file.\n" &LT;/SPAN&GT; ret); <span class= "hljs-built_in" style= "Color:rgb" (102, 0, 102); Box-sizing:border-box; " >printf</span> (<span class= "hljs-string" style= "Color:rgb" (0, 136, 0); box-sizing:border-box; "
    > "The content of write is [%s].\n" &LT;/SPAN&GT;, bufwrite); } <span class= "Hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " > #endif </span> <span class= "hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " > #if 1 </span> ret = read (FD, buf, <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing: Border-box; "   >sizeof</span> (BUF)); <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//reads file contents </span> <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >if</span> (Ret < <span class= "Hljs-number" style= "Color:rgb" (0, 102, 102); Box-sizing:border-box; " >0</span>) <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " >//to determine whether to read the file successfully </span> {<span class= "hljs-built_in style=" Color:rgb (102, 0, 102); Box-sizing:bo Rder-box; " >printf</span> (<span class= "hljs-string" style= "Color:rgb" (0, 136, 0); box-sizing:border-box; "
        > "Read file error.\n" </span>); <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//return-1;</span> <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >exit</span> (-<span class= "Hljs-number" style= "Color:rgb" (0, 102, 102); box-sizing:border-box; "
    >1</span>); } <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >else</span> {<span class= "hljs-built_in" style= "Color": RGB (102, 0, 102); Box-sizing:border-box; " >printf</span> (<span class= "hljs-string" style= "Color:rgb" (0, 136, 0); box-sizing:border-box; "
        > "read%d bytes actual from file.\n" &LT;/SPAN&GT;, ret); <span class= "hljs-built_in" style= "Color:rgb" (102, 0, 102); Box-sizing:border-box; " >printf</span> (<span class= "hljs-string" style= "Color:rgb" (0, 136, 0); box-sizing:border-box; "
    > "The content of Read is [%s].\n" &LT;/SPAN&GT;, buf); } <span class= "Hljs-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; "                          > #endif </span> Close (FD); <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//closes the file </span> <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//return 0;</span> <span class= "hljs-built_in" style= "Color:rgb" (102, 0, 102); Box-sizing:border-box; " >exit</span> (<span class= "Hljs-number")style= "Color:rgb (0, 102, 102); Box-sizing:border-box; "
>0</span>); }</code>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.