Embedded linux / Hongmeng development board (IMX6ULL) application development basic knowledge file IO

Posted by mgason on Tue, 14 Dec 2021 14:56:48 +0100

4. File IO

Reference books:

The contents of these two books are similar. The first one has a more detailed description of knowledge points, which is suitable for beginners; The second book is more direct. It is an introduction to various functions. It is suitable for use as a dictionary. If you don't understand it, go and have a look.
To get started with pure Linux applications, just read these two books. You don't need to learn our videos. Our focus is on "embedded Linux".

In Linux system, everything is "file": ordinary file, driver, network communication and so on. All operations are performed through "file IO". Therefore, it is necessary to master the common interfaces for file operation.

4.1 where do the documents come from?

4.2 how to access files?

4.2. 1. General IO model: open/read/write/lseek/close

After downloading all the source code using GIT, the source code of this section is located in the following directory:
01_all_series_quickstart
04_ Basic knowledge of embedded Linux application development \ source_fileio\copy.c

copy.c source code is as follows:

01
02 #include <sys/types.h>
03 #include <sys/stat.h>
04 #include <fcntl.h>
05 #include <unistd.h>
06 #include <stdio.h>
07
08 /*
09  * ./copy 1.txt 2.txt
10  * argc    = 3
11  * argv[0] = "./copy"
12  * argv[1] = "1.txt"
13  * argv[2] = "2.txt"
14  */
15 int main(int argc, char **argv)
16 {
17      int fd_old, fd_new;
18      char buf[1024];
19      int len;
20
21      /* 1. Judgment parameters */
22      if (argc != 3)
23      {
24              printf("Usage: %s <old-file> <new-file>\n", argv[0]);
25              return -1;
26      }
27
28      /* 2. Open old file */
29      fd_old = open(argv[1], O_RDONLY);
30      if (fd_old == -1)
31      {
32              printf("can not open file %s\n", argv[1]);
33              return -1;
34      }
35
36      /* 3. create a new file */
37      fd_new = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
38      if (fd_new == -1)
39      {
40              printf("can not creat file %s\n", argv[2]);
41              return -1;
42      }
43
44      /* 4. Loop: read old file - write new file */
45      while ((len = read(fd_old, buf, 1024)) > 0)
46      {
47              if (write(fd_new, buf, len) != len)
48              {
49                      printf("can not write %s\n", argv[2]);
50                      return -1;
51              }
52      }
53
54      /* 5. Close file */
55      close(fd_old);
56      close(fd_new);
57
58      return 0;
59 }
60

The source code of this section can be tested on Ubuntu, which is no different from that on ARM board.
Execute the following commands to compile and run:

$ gcc -o copy copy.c
$ ./copy  copy.c new.c

4.2. 2 is not a general function: ioctl/mmap

After downloading all the source code using GIT, the source code of this section is located in the following directory:
01_all_series_quickstart
04_ Basic knowledge of embedded Linux application development \ source_fileio\copy_mmap.c

In Linux, you can also map all the contents of a file to memory, and then directly read and write the memory to read and write the file.

copy_mmap.c source code is as follows:

01
02 #include <sys/types.h>
03 #include <sys/stat.h>
04 #include <fcntl.h>
05 #include <unistd.h>
06 #include <stdio.h>
07 #include <sys/mman.h>
08
09 /*
10  * ./copy 1.txt 2.txt
11  * argc    = 3
12  * argv[0] = "./copy"
13  * argv[1] = "1.txt"
14  * argv[2] = "2.txt"
15  */
16 int main(int argc, char **argv)
17 {
18      int fd_old, fd_new;
19      struct stat stat;
20      char *buf;
21
22      /* 1. Judgment parameters */
23      if (argc != 3)
24      {
25              printf("Usage: %s <old-file> <new-file>\n", argv[0]);
26              return -1;
27      }
28
29      /* 2. Open old file */
30      fd_old = open(argv[1], O_RDONLY);
31      if (fd_old == -1)
32      {
33              printf("can not open file %s\n", argv[1]);
34              return -1;
35      }
36
37      /* 3. Determines the size of the old file */
38      if (fstat(fd_old, &stat) == -1)
39      {
40              printf("can not get stat of file %s\n", argv[1]);
41              return -1;
42      }
43
44      /* 4. Mapping old files */
45      buf = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd_old, 0);
46      if (buf == MAP_FAILED)
47      {
48              printf("can not mmap file %s\n", argv[1]);
49              return -1;
50      }
51
52      /* 5. create a new file */
53      fd_new = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
54      if (fd_new == -1)
55      {
56              printf("can not creat file %s\n", argv[2]);
57              return -1;
58      }
59
60      /* 6. Write new file */
61      if (write(fd_new, buf, stat.st_size) != stat.st_size)
62      {
63              printf("can not write %s\n", argv[2]);
64              return -1;
65      }
66
67      /* 5. Close file */
68      close(fd_old);
69      close(fd_new);
70
71      return 0;
72 }
73

The source code of this section can be tested on Ubuntu, which is no different from that on ARM board.
Execute the following commands to compile and run:

$ gcc -o copy_mmap copy_mmap.c
$ ./copy_mmap  copy_mmap.c  new2.c

4.3 how do you know the usage of these functions?

There are three help methods under Linux: help, man and info.
To view the usage of a command, such as the ls command, execute:

ls  --help

help can only be used to view the usage of a command, while the man manual can view the usage of commands, detailed descriptions of functions, etc. It contains nine categories as follows:

1   Executable programs or shell commands       // command
2   System calls (functions provided by the kernel)  // System call, such as man 2 open
3   Library calls (functions within program libraries)  // Function library call
4   Special files (usually found in /dev)             // Special files, such as man 4 tty 
5   File formats and conventions eg /etc/passwd  // File formats and conventions, such as man 5 passwd
6   Games  // game
7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) //miscellaneous
8   System administration commands (usually only for root) // System management commands
9   Kernel routines [Non standard]  // kernel subroutines 

For example, if you want to check the usage of the open function, you can directly execute "man open". If you find that this is not what you want, execute "man 2 open".
In the man command, you can timely press "h" to view help information and understand shortcut keys. Common shortcut keys are:

f  Turn a page forward
b  Turn back one page
/patten Search ahead
?patten Search back

In terms of content, the info manual is more comprehensive than the man manual, but the man manual is easier to use.
To describe the info manual and man manual in terms of books, the info manual is equivalent to a chapter, which contains several sections. You need to master if you jump from this section to the next section when reading; The man manual is only one section, which is easier to read.
Personally, I rarely use the info command.
You can directly execute the "info" command and enter "H" to view its shortcut keys. In the Info manual, a section is called "node". The common shortcut keys are as follows:

Up          Move up one line.
Down        Move down one line.
PgUp        Scroll backward one screenful.
PgDn        Scroll forward one screenful.
Home        Go to the beginning of this node.
End         Go to the end of this node.

TAB         Skip to the next hypertext link.
RET         Follow the hypertext link under the cursor.
l           Go back to the last node seen in this window.

[           Go to the previous node in the document.
]           Go to the next node in the document.
p           Go to the previous node on this level.
n           Go to the next node on this level.
u           Go up one level.
t           Go to the top node of this document.
d           Go to the main 'directory' node.

4.4 how does the system call function enter the kernel?

4.5 sys of kernel_ open,sys_ What will read do?

Topics: Linux Operation & Maintenance harmonyos