Objectives of this section:
1. File Sharing
- Open the file kernel data structure
- A process opens the same file twice
- Two processes open the same file
2. copy the file descriptor (dup, dup2, fcntl)
1. File Sharing
1. A process opens two file kernel data structures
: Each process has one table, which is independent of each other. Each file descriptor table item points to a file table. The file descriptor 0 (STDIN_FILENO), 1 (STDOUT_FILENO), and 2 (STDERR_FILENO ), it is enabled by default, indicating standard input, standard output, and standard error devices.
: Each time a file is opened, it corresponds to a file table, which can be shared. When multiple file descriptors point to the same file table
The refcnt field changes accordingly. File status identifier: the file opening mode (R, W, RW, APPEND, NOBLOCK, etc.), current file offset, refcnt: Number of referenced files,
V node pointer: points to a v node table.
: Each file corresponds to one. No matter how many processes open, there is only one file. It includes v node information (mainly information in the stat struct) And I node information.
By default, only 1024 file descriptors can be opened for each process. When a process opens a file, the unused descriptors are searched from 0 by default. Because 0, 1 and 2 are occupied by default, generally, it starts from 3.
2. A process opens the same file twice
<Sys/stat. h> <sys/types. h> <sys/stat. h> <fcntl. h> <stdlib. h> <stdio. h> <errno. h> <. h> ERR_EXIT (m) \ (main (argc, * buf1 [] = {buf2 [] = {= open (fd1 =-= open (fd2 = -,,
Running result:
1, dup
2, dup2
# Include <unistd. h>
Int dup (int oldfd );
Int dup2 (int oldfd, int newfd );
DESCRIPTION
These system callcreate a copy of the file descriptor oldfd.
Dup () uses the lowest-numbered unused descriptor for the new descriptor.
Dup2 () makes newfd be the copy of oldfd, closing newfd first if necessary, but note
The following:
* If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.
* If oldfd is a valid file descriptor, and newfd has the same value
Oldfd, then dup2 () does nothing, and returns newfd.
After a successful return from one of these system cals, the old and new file descriptors may be used interchangeably. they refer to the same open file description (see open (2) and thus share file offset and file status flags; for example, if the file offset is modified by using lseek (2) on one of the descriptors, the offset is also changed for the other.
RETURN VALUE
On success, these system callreturn the new descriptor. On error,-1 is returned, and errno is set appropriately.
Example program:
#include <stdio.h><unistd.h><stdlib.h><fcntl.h> main(= open(( fd == -=(fd2 == -); fd3 =(fd3 == -
Running result:
<Unistd. h> <stdlib. h> <fcntl. h> main (= open (fd =-= dup2 (fd, (fd2 =-
Running result:
Or equal to arg and make it be a copy of fd. This is different
From dup2 (2), which uses exactly the descriptor specified.
On success, the new descriptor is returned.
Example program:
#include <unistd.h><sys/stat.h><sys/types.h><sys/stat.h><fcntl.h><stdlib.h><stdio.h><errno.h><.h> ERR_EXIT(m) \ ( main( argc, *= open( (fd == - (fcntl(fd, F_DUPFD, ) < );
Running result: