“This is the 17th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”.

1.1C Difference between standard function and system function

! [image – 20211117200854436] (/ Users/shenjunwei/Documents/Doc/vegetable/source / _note/programming language to c + + course / _v_images/Andro Android programmer Id C++ series: Linux IO operation /image-20211117200854436.png)

1.1.1 I/O Buffer

Each FILE stream has a buffer with a default size of 8192 bytes.

1.1.2 efficiency

1.1.3 Cross-platform of the program

In fact, the term Unbuffered I/O is somewhat misleading. Although the write system call is located at the bottom of the C library’S I/O buffer, a kernel I/O buffer can be allocated at the bottom of the write, so the write does not have to be written directly to a file. It can also be written to a kernel I/O buffer. It makes no difference to A process whether it writes to A file or A kernel buffer. If process A and process B open the same file, the amount of data that process A writes to the kernel I/O buffer can be read from process B, while the C library I/O buffer does not (think about why).

1.2 PCB concept

1.2.1 task_struct struct

    /usr/src/linux-headers/include/linux/sched.h
Copy the code

1.2.2 files_struct

1.3 the open/close

! [image – 20211117201012910] (/ Users/shenjunwei/Documents/Doc/vegetable/source / _note/programming language to c + + course / _v_images/Andro Android programmer Id C++ series: Linux IO operation /image-20211117201012910.png)

1.3.1 File descriptors

A process opens three file descriptors by default

STDIN_FILENO 0 STDOUT_FILENO 1 STDERR_FILENO 2
Copy the code

Newly opened file returns the minimum file descriptor that is not used in the file descriptor table. The open function opens or creates a file.

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); Return value: The newly assigned file descriptor is returned on success, -1 and errno is set on errorCopy the code

In Man Page, the open function has two forms, one takes two arguments, one takes three arguments, in C code, the open function is declared like this:

int open(const char *pathname, int flags, ...) ;Copy the code

The final variable argument can be 0 or 1, depending on the flag bit in the Flags argument, as detailed below.

The pathName argument is the file name to open or create. Like fopen, pathName can be either a relative path or an absolute path. The flags argument has a series of constant values to choose from. Multiple constants can be selected at the same time by concatenating them with bitbits or operators, so the macro definitions for these constants all start with O_, indicating or.

Mandatory: Only one of the following three constants must be specified.Copy the code

* O_RDONLY read-only open * O_WRONLY write only open * O_RDWR read write open

The following options can specify 0 or more at the same time, and the required options can be bited or raised as flags arguments. There are a number of options available. Here are only some of them. For other options refer to the Man Page of Open (2) :

  • * O_APPEND: append. If the file already has content, the data written to the file is appended to the end of the file without overwriting the original content.

  • * O_CREAT Creates the file if it does not exist. To use this option, you need to provide a third parameter, mode, which indicates the access permission to the file.

  • * O_EXCL An error is returned if O_CREAT is also specified and the file already exists.

  • * O_TRUNC Truncates the length of a file if it already exists and is opened in write-only or read-write mode (Trun-

    Cate) is 0 bytes.

  • * O_NONBLOCK For device files, open in O_NONBLOCK mode to do non-blocking I/O(Nonblock I/

    O), non-blocking I/O is covered in more detail in the next section.

    Note that the open function is slightly different from the C standard I/O library’s fopen function: fopen a file in writable form is automatically created if the file does not exist, whereas opening a file must be

    Specify O_CREAT explicitly to create the file, otherwise return an error if the file does not exist. Fopen a file with w or W + truncates the file to 0 bytes if it already exists, whereas opening a file must

    O_TRUNC must be explicitly specified to truncate the file, otherwise overwrite the original data directly. The third parameter, mode, specifies file permissions, which can be expressed as an octal number, such as -rw-r-r – for 0644, or as an octal number

    Use S_IRUSR, S_IWUSR and other macro definitions in bitwise or together, see Man Page of Open (2). Note that file permissions are determined by both the mode parameter of open and the umask mask of the current process.

    A note about the Shell umask command. The umask mask of a Shell process can be viewed using the umask command:

    $ umask 
    0002
    Copy the code

    When creating a file using the touch command, the create permission is 0666. The touch process inherits the Shell process umask mask, so the final file permission is 0666&~ 022=0644.

    $touch file123 $ls-l file123-rw-rw-r -- 1 qingkouwei qingkouwei 0 9月 11 23:48 file123Copy the code

    Similarly, when compiling an executable with GCC, the create permission is 0777, and the final file permission is

    0777&~ 022 = 0755.

    Ubuntu :~$umask 0002 Ubuntu :~$GCC main.c Ubuntu :~$ls -l a.ut -rwxrwxr-x 1 qingkouwei qingkouwei 7158 9月 11 23:51 a.outCopy the code

    All we see are permissions modified by the umask mask, so how can we prove that touch or GCC should have created files with limits 0666 and 0777? We can change the Shell process umask to 0 and repeat the above experiment:

    $rm file123 a.out $umask 0 $touch file123 $ls -l file123-rw-rw-rw-1 qingkouwei qingkouwei $GCC main.c 0 9月 11 23:52 file123 $ls-l a.ut -rwxrwxr-x 1 qingkouwei qingkouwei 7158 September 11 23:52 a.utCopy the code

    Now we write a program in which the call open (” somefile O_WRONLY | O_CREAT, 0664); Create the file, then run it in the Shell and view the result:

    The close function closes an open file:

    #include <unistd.h> int close(int fd); Return value: 0 on success, -1 on error and set errnoCopy the code

    The fd argument is the file descriptor to close. It should be noted that when a process terminates, the kernel calls close to close all open file descriptors for that process, so even if the user program does not call close, the kernel will automatically close all open files at termination. However, for a long-running program (such as a network server), it is important to close the open file descriptors. Otherwise, as more and more files are opened, a large number of file descriptors and system resources will be consumed.

    The file descriptor returned by open must be the smallest descriptor not yet used by the process. Since file descriptors 0, 1, and 2 are opened automatically when the program starts, the first call to open a file usually returns descriptor 3, and the next call to open returns descriptor 4. You can use this to open a new file on standard input, standard output, or standard error output for redirection. For example, if close is called to close file descriptor 1 and open is called to open a regular file, file descriptor 1 must be returned, and the standard output is no longer a terminal, but a regular file, and the call to printf is not printed to the screen, but written to the file. The dup2 function, described later, provides another way to open a file on a specified file descriptor.

1.3.2 Maximum number of open files

View the maximum number of files that can be opened in the current system

 cat /proc/sys/fs/file-max
Copy the code

The default maximum number of open files is 1024

 ulimit -a
Copy the code

Example Change the default maximum number of open files to 4096

ulimit -n 4096
Copy the code

1.4 summarize

This document describes file operation commands, system calls, and APIS in Linux. It also introduces the difference between C standard function and system function, and the concept of PCB.