[LINUX] Basic IO (redirection)

Basic IO

System file IO

In addition to fopen, fwrite and other interfaces when we manipulate files, we can also use system interfaces to access files.

Interface introduction

OPEN

#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);
pathname: 要打开或创建的目标文件
flags: 打开文件时,可以传入多个参数选项,用下面的一个或者多个常量进行“或”运算,构成flags。
参数:
O_RDONLY: 只读打开
O_WRONLY: 只写打开
O_RDWR : 读,写打开
这三个常量,必须指定一个且只能指定一个
O_CREAT : 若文件不存在,则创建它。需要使用mode选项,来指明新文件的访问权限
O_APPEND: 追加写
返回值:
成功:新打开的文件描述符
失败:-1

Here, including pathname, flags are introduced, write, read, etc. are similar to those in the C library.

File descriptor

When we define

int fd=open(.........)

This form
Insert picture description here
and now know that the file descriptor is a small integer starting from 0. When we open a file, the operating system must create a corresponding data structure in the memory to describe the target file. So there is a file structure. Represents an opened file object. The process executes the open system call, so the process must be associated with the file. Each process has a pointer files, which points to a table files_struct. The most important part of the table is to contain an array of pointers. Each element is a pointer to an open file! So, in essence, the file descriptor is the subscript of the array. So, as long as you hold the file descriptor, you can find the corresponding file*

Therefore, the bottom layer of fp used in the C language is a structure. The structure must contain data describing fd as a descriptor.

0 & 1 & 2

By default, the Linux process will have 3 default open file descriptors, which are standard input 0, standard output 1, and standard error 2.
The physical devices corresponding to 0,1,2 are generally: keyboard, display, display

Redirect

fd distribution rules

When we close(1)//1 before the code is the standard output
and then write data to fd, we will find that the display will not be displayed, but will be written to the file. From then on, the distribution rules can be introduced.
1. The system will assign 012 to standard input, standard output, and standard error. (Because the bottom layer needs to interact with the hardware)
2. When we close one of them in advance, we will find that fd will choose a small subscript for allocation.
3. This also introduces redirection.
Insert picture description here
This picture is the essence of redirection.

Look through the code

int main()
{
    
    
close(1);
int fd = open("myfile", O_WRONLY|O_CREAT, 00644);
if(fd < 0){
    
    
perror("open");
return 1;
}
printf("fd: %d\n", fd);
fflush(stdout);
close(fd);
exit(0);
}

Close (1) first, then 1 is the smallest subscript. Open a file is described by the file descriptor fd. At this time, the value of fd is 1, and then input to the display will be redirected to the file.

dup2 function

Header file and its definition: #include <unistd.h> int dup2(int oldfd, int newfd);

dup2 can use the parameter newfd to specify the value of the new file descriptor. If the parameter newfd has been used by the program, the system will close the file pointed to by newfd. If newfd is equal to oldfd, it will return to newfd without closing the file pointed to by newfd. The file descriptor copied by dup2 shares various file states with the original file description. Share all locks, read and write positions and various permissions or flags, etc.
Return value:
If the dup2 call is successful, it will return a new file descriptor, and if it fails, it will return -1.

Guess you like

Origin blog.csdn.net/weixin_43762735/article/details/115309552