"linux&unix--file descriptors and Permissions"

Source: Internet
Author: User

personal Learning and collation, if there are deficiencies, please feel free. Reprint Please specify:@CSU-max


File Descriptor

Each UNIX process has a file descriptor range with a size of 0 to the maximum value of the N,N flag file descriptor, and the size of n depends on the UNIX version and System configuration. In fact, it is an index value that points to the record table in which the kernel opens a file for each process maintained by the process. When a program opens an existing file or creates a new file, the kernel returns a file descriptor to the process. In the program design of Linux and Unix operating systems, some of the underlying programming tends to be expanded around the file descriptor.

standard File Descriptor

Typically, the first three file descriptors are opened when the process is running.

file descriptor 0: standard input

file descriptor 1: Standard output

file descriptor 2: standard error output

In addition to using numbers to represent, you can also use symbolic constants, corresponding to: Stdin_fileno, Stdout_fileno, Stderr_fileno.

Any one of these standard file descriptors can open files, pipes, FIFO, devices, or even sockets, and these three standard file descriptors can be used immediately when calling the read and write system calls. Other file descriptors for files, pipelines, and so on can be obtained through the process itself.

The file descriptor is simply an index to each process table . Each record entry in the process table points to a system-wide open File description (that is, a file table record entry), and the file descriptor points to the file data.

The above procedure can be expressed as:





as shown, multiple file descriptors, even file descriptors from different processes, can point to the same file description .

each open or pipe system call creates a new opening file description and a new file descriptor. As shown, process a two times has opened the same file, got the file descriptor 5 and 6, and created open file Description 1 and 2, after the file descriptor copy mechanism, process a got a copy of the file descriptor 5-File Descriptor 7. Of course, the file descriptor 7 points to the open file description with the file descriptor 5 pointing to the same, that is, pointing to the same open file description, instead of creating a new one as previously said. Process B is a child process of process A, and file descriptor 3 agrees to be a copy of the file descriptor 5.

file permissions and representations

The information node for each file has an owner user ID (the owner) and an owner group ID (group), and the information node also contains three sets of permission bits, three bits per group, each containing a read permission bit, a write permission bit, and a execute permission bit. A value of 1 means permission is allowed, and 0 indicates a deny permission. Three groups of permissions, one for owners, one for groups, and one for other users (i.e. not in the previous two categories).

The permission bits are typically represented in the order of the owner, group, and other user three groups, usually in octal numbers. The specific representation method is as follows:



For example, the octal number 755 indicates that the owner has read, write, and execute permissions, while the group and other users have only read and execute permissions. The LS command shows the combination of its permissions as Rwxr-xr-x, which is represented as 111101101 in binary notation.

file permission bit symbol

If you think that file permissions must be used in the above method: 9 file permission bits should be put together and in a certain order (owner, group, other user), it is wrong. This is not actually the case, as long as there are 9 permission bits.

a permission bit symbol is another way of representing file permissions that can be used instead of the traditional octal number representation. These symbols are in the form s_ipwww, where p represents access rights (R,W or X), and www represents who operates (USR, GRP, or OTH), which represents all 9 symbols.

For example, as mentioned earlier, without octal 755, the available permission bit symbols are shown as follows:

s_irusr | S_IWUSR | S_ixsur | S_irgrp | S_ixgrp | S_iroth | S_ixoth

when USR, GRP, or oth has all three access rights, it is possible to use a separate symbol, such as: S_irwxw. Here W represents the user type, which can be u, g, or O. Therefore, the above file access rights can also be expressed as follows:

S_irwxu | S_irgrp | S_ixgrp | S_iroth | S_ixoth

Although this description is less readable and prone to error, the operator is free to arrange the position of the permission bit, and it is also more convenient to read programming, generally we will define the required macro at once, instead of using a long sequence of symbols such as s_irusr in all required locations.

define Perm_directory S_irwxu

define Perm_file (S_irsur | S_iwsur | S_irgrp | S_iroth)

This allows you to change the entire application's access policy by changing the macro only once.

***************************************************************************

                                        & nbsp;            *   Reprint Please specify source:   @CSU-max    http ://blog.csdn.net/csu_max      *

***************************************************************************






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.