Linux files, file descriptors and dup() and dup2()

  
 

A.Linux files

can be divided into four types: ordinary files, directory files, link files, and device files.


1. Ordinary files


are the most used files for users, including text files, shell scripts, binary executables, and various Type of data.


ls -lh to view the properties of a file, you can see something like -rw-r--r--, it is worth noting that the first symbol is -, so The file is a normal file in Linux. These files are typically created with related applications such as image tools, document tools, archiving tools... or cp tools. The way to delete such files is to use the rm command;


2, directory files


In linux, directories are also files, they are included File name and subdirectory name and pointers to those files and subdirectories


When we execute in a directory, we see something like drwxr-xr-x, which is the directory. The directory is a special file in Linux. Note that its first character is d. The command to create a directory can use the mkdir command, or the cp command, to copy a directory to another directory. Remove the command with rm or rmdir.


3. Linked files


Linked files are similar to "shortcuts" in Windows
.


is created by ln -s source file name new file name.


4, device files


includes two kinds of block device files, and the other is character device files

< Br>

Block device files refer to data read and write, they are devices in block units, such as hard disk drive


Character devices mainly refer to serial port interface devices Such as network cards.

Second, the file descriptor

1, the file descriptor and its role


Kernel (Kernel) using a file described in File descriptor to access the file. The file descriptor is a non-negative integer. When opening an existing file or creating a new file, the kernel will return a file descriptor. Reading and writing files also requires the use of file descriptors to specify which files to read and write. For Linux, all operations on devices and files are done using file descriptors. A file descriptor is a non-negative integer that is an index value and points to a record table in which each process in the kernel opens a file. When opening an existing file or creating a new file, the kernel returns a file descriptor to the process; when it needs to read and write files,


also needs to pass the file descriptor as a parameter Give the corresponding function.


Normally, when a process starts, it opens 3 files: standard input, standard output, and standard error handling. These three files correspond to file descriptors 0, 1, and 2, that is, macros replace STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO, and readers are encouraged to use these macros instead.)


View the default file descriptors for Linux, a total of 1024, which is sufficient for most cases:

# ulimit -n


View process id

#ps aux


Get a process file descriptor

cd /proc/[pid] /fd[pid] is the pid of the corresponding process.


#cd /proc/1473/fd




#sysctl -a

Copyright © Windows knowledge All Rights Reserved