Linux environment Programming file I/O (i): File descriptor

Source: Internet
Author: User

A

First, for the kernel, it uses "file descriptors" to access files . The file descriptor is generally a non-negative integer. A file descriptor is returned when we open an existing file with open or create a new file with creat. With the file descriptor, we can use the file description to read and write the file, that is, the read, write system call requires the file descriptor FD (filename descriptor) as its parameter. As can be seen from the above description, when we want to use read, write and other system calls to read and write the file, you must use the open or creat system call to get the file descriptor.


In general Unix systems, the shell uses the file descriptor 0 associated with the process's callout input, and the file descriptor 1 is associated with the output of the process, and the file descriptor 2 is associated with the callout error . In applications, 0, 1, and 2 are generally used instead of constants Stdin_fileno, Stdout_fileno, and Stderr_fileno. These substitutions are defined in the </usr/include/unistd.h> header file:

/*standard file descriptors*/

#define STDIN_FILENO 0/*input*/

#define Stdout_fileno 1/*output*/

#define Stderr_fileno 2/*error output*/

The sample program is as follows:

/*demo.c*/#include <stdio.h> #include <fcntl.h> #include <unistd.h>intmain (void) {int fd;        /* Open file test.c, because it does not exist, so open failed, return -1*/FD = open ("test.c", o_rdonly);p rintf ("FD =%d.\n", FD);        /* Create the file because descriptors 0, 1, 2 are default, so the descriptor created is 3*/FD = creat ("test.c", O_RDWR);p rintf ("FD =%d.\n", fd); return 0;}

Compile execution: gcc demo.c./a.out results show: FD = -1.FD = 3.


Two

Each process has a task_struct struct in the Linux kernel to maintain information about the process, called the process descriptor. The process descriptor task_struct has a pointer member (struct FILES_STYRUCT *files) that points to the files_struct struct, called the File descriptor table, where each of its entries contains a pointer to an open file.

Instead of directly accessing the file descriptor list in the kernel, the user can use only the index of the file descriptor, which is the file descriptor.


Three

File descriptors, file pointers, file paths There are also differences between the three, it is difficult to start learning to clarify the relationship between the three.

First, the file descriptor FD is generally a concept in Unix or Unix-like systems, and it is actually an integer that can be used to get an open file in a File descriptor table.

Document pointer file is a concept in C library, which is not only in Linux systems, but also in Windows, where the file pointer points to a data structure called a file structure in the user area of the process, which includes a buffer and a file descriptor. file contains fd information and IO buffering, so file is more suitable for cross-platform than FD. they can be converted to each other using the Fdopen function and the Fileno function.

FILE *fdopen (int fd, const char *mode);

int Fileno (FILE *stream);

File path is the path of the file, usually, we get the file path to the specific file, so that the file descriptor, the file pointer.


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.