Mknod commands and low-level file operation functions

Source: Internet
Author: User
Mknod command: used to create a special file prototype: # mknod [options] device file name {bcu} device No. Times device No. www.2cto.com option [-mmode] [-- modemode] [-- help] [-- version] option:-m, -- mode...
Mknod command: used to create a special file prototype: # mknod [options] device file name {bcu} device No. Times device No. www.2cto.com option [-m mode] [-- mode = mode] [-- help] [-- version] option: -m, -- mode specifies the file generation mode option. As a mode, it includes the mark or digit form used in chmod -- help display help content -- version display version description: mknod command is used to create FIFO, character device files, and block device files. The initial value of the file creation mode is 0666. Specify the special status of the file after writing the file name to the device. Here you can use the following settings: p FIFO file B block device file c or U file device file when creating a block or character special file, the Master and secondary device numbers of the device file must be specified. Open () function: open the device file prototype: # include # Include # Include Int open (const char * pathname, int flag); note: use the property specified by flags to open the device file that represents the specified character on pathname. Generally, the specified location on the pathname is the device file in the "/dev/" directory. Variable: pathname: specify the character address of the device file. flags indicates the property close to the device file: _ O_RDONLY open the file _ O_WRONLY in read-only mode _ O_RDWR open the file in readable and writable mode _ O_NOCTY if the file to be opened is a terminal device, the terminal will not be treated as a process control terminal _ O_NONBLOCK to open the file in an unblocking manner, that is, whether there is data read or waiting, _ O_NDELAY in the process will be immediately returned to open the file in an unblocking manner _ O_SYNC to open the file in synchronous mode, the content written on the device is recorded before the hardware, the call process is in the blocking status www.2cto.com. return value: if the file is successfully opened, the file descriptor is returned. if the file fails, the value-1 is returned. If all the permissions to be verified have passed the check, a value of 0 is returned, indicating that the operation is successful. if one permission is disabled, the system returns-1. When you get the-1 value, see errno to determine the value returned by the actual device driver. Error Code:-The ENXIO file is a device file, but no corresponding device-ENODEV does not exist. the device driver related to the device file or the hardware-ENOMEM core memory is insufficient. the Function of closing () is as follows: disable the device file prototype: # include Int close (int fd); description: to open the device file, disable the file descriptor fd corresponding to the device file returned by the open () function. Variable: file descriptor return value returned by fd open () function running Result: 0 is returned if the function is closed successfully, and-1 is returned if the function fails. The read () function reads data from device files. prototype: # include Ssize_t read (int fd, void * buf, size_t count); description: read () the function transfers the device file pointed to by the parameter fd to the memory indicated by the buf pointer in count bytes. At this time, the count www.2cto.com value should be less than SSIZE_MAX. When the open () function is not specified as O_NONBLOCK or O_NDELAY, the corresponding count value can be read. When the device driver of the device file does not reflect O_NONBLOCK or O_NDELAY, the corresponding options are not specified and may be blocked. In principle, this is the wrong device driver. When creating a program, you must compare the above conditions. Therefore, you must confirm the returned results. In addition, the file read/write location will move with the size of the read byte. Variable: fd stores the space location of the data to be read by the descriptor buf returned by the open () function running result. The storage space should be larger than the data size read from the count byte count device file. The value must be smaller than SSIZE_MAX. The return value is 0, and the operation is terminated immediately. The return value is the number of bytes read after the device file reads data normally. It is not an error even if the value is smaller than the corresponding number of bytes. Several www.2cto.com does not have the actual number of bytes available or the signal is interrupted. If it fails,-1 is returned. if the value-1 is returned, see errno. you can confirm the return value in the actual device driver. Error Code:-EINTR this call is interrupted by the signal-EAGAIN when the file is opened with an unblocking (O_NONBLOCK, read calls unreadable data-an input/output error occurs when reading data from an EIO device file-the EBADF parameter fd is not a valid file descriptor, or the file has disabled-EINVAL fd to connect to the object that is not properly read-the buf parameter of EFAULT is an invalid pointer and points to the function of the memory write () function that cannot exist: write data to www.2cto.com in the device file. prototype: # include Ssize_t write (int fd, const void * buf, size_t count); description: write () the function writes the count bytes in the memory referred to by the parameter buf to the file referred to by the parameter fd. In this case, the count value should be less than SSIZE_MAX. When the open () function is not specified as O_NONBLOCK or O_NDELAY, the corresponding count value can be read. When the device driver of the device file does not reflect O_NONBLOCK or O_NDELAY, the corresponding options are not specified and may be blocked. In principle, this is the wrong device driver. When creating a program, you must compare the above conditions. Therefore, you must confirm the returned results. The position of the file pointer will move the corresponding number of bytes. Variable: fd stores the space location of the written data in the descriptor buf returned by the open () function running result. The storage space referred to by this address should be larger than the size of data to be written in the count byte count device file. The value must be smaller than SSIZE_MAX. If the return value is 0, the system immediately interrupts the return value. after a device file writes data normally, the number of bytes is returned. It is not an error even if the value is smaller than the required number of bytes. There may be no actual number of bytes written, or some signal is interrupted. If the request fails,-1 is returned. if the value of-1 is returned, see errno. you can confirm that the error code www.2cto.com returned by the actual device driver-invalid file descriptor of the EBADF parameter fd, or the file is not in writable state-EINVAL fd is connected to the object not suitable for writing-the EFAULT parameter buf is an invalid pointer and points to an unusable space-although EAGAIN is not blocked (O_NONBLOCK) open the file, but it is not in the status that can be directly processed after the read Call-before EINTR writes data, this call is interrupted by signal-the ENOSPC device that contains the fd file does not have the corresponding data space-the input/output error occurs when the EIO device file is written to the data function: prototype of the read/write location of a mobile File: # include # Include Off_t lseek (int fd, off_t offset, int whence); Description: The lseek () function is used to control the read/write location of the file. Move the device file specified by file descriptor fd to the file pointer up to www.2cto.com and move the file pointer to the offset value specified by whence. The location of the file pointer varies with the processing method of the device driver connected to the device file. For example, device files that manage memory can use the memory location. However, most character device drivers do not use this function. Variable: the offset value returned by the open () function running result is in bytes, specifying the position of the pointer to the file to be moved. This value is interpreted as the actual moving position along with the whence. whence specifies the condition used to interpret the offset. the SEEK_SET parameter offset is the new read/write location.-SEEK_CUR increases the offset displacement after the current read/write location.-SEEK_END will after the read/write position points to the end of the file, the offset displacement is increased. Most devices have a vague definition of the end of the file. Generally, this value is not used to return the value: when the call is successful, the current read/write location is returned, that is, the number of bytes from the beginning of the file. If an error occurs, return (off_t)-1, and errno stores the error code. After obtaining the normal moving position of the file pointer, the actual moving position is returned. If it fails,-1 is returned. When the-1 value is obtained, refer to the global variable error value to determine the value returned by the actual device driver. the error code www.2cto.com-EINVAL whence is not suitable for the ------------------------------------------------------------------------------------------------- ioctl () function: control Device file prototype: # include Int ioctl (int fd, int request ,...); note: The ioctl () function implements the read () and write () functions on the device file corresponding to the file descriptor fd for hard-to-complete input/output processing. Except fd, each variable in this function has no standard value. Knowledge specifies the criteria defined by several macro values. Different device files have different values. Variable of ioctl () function. Although it can be expressed in syntax, it can contain up to three. The third factor represents char * argp; variable: fd the request descriptor returned by the open () function running result defines the command that the device driver connecting to the device file should call. The macro determines whether the input or output commands are used. when the value specified by argp www.2cto.com is used as the storage address, the transmission factor is displayed in bytes... the third variable is called argp. it is an ignored variable. it is related to the request. it is the auxiliary information returned for processing the request command. when the call is successful, 0 is returned and-1 is returned. When the value-1 is obtained, refer to the global variable error value to determine the value returned by the actual device driver. the error code-the EFAULT parameter argp is invalid, point to memory space that cannot exist-ENOTTY fd has nothing to do with the character device file-the device driver connecting the device file to the EINVAL cannot process the request or the argp functions www.2cto.com fsync () function: write the buffer data back to the disk prototype: # include Int fsync (int fd); description: fsync () is responsible for writing the file data referred to by the parameter fd back to the disk from the system buffer to ensure that the data synchronization variable: fd is composed of open () return value of the descriptor returned by the function running result: 0 is returned for success. if-1 is returned for failure, the returned value is determined by referring to the global variable error value. Error code-EROFS, EINVAL device file does not support the processing of this function-an error occurred during EIO synchronization
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.