Meanings of open (/dev/ietctl, o_rdwr) Parameters

Source: Internet
Author: User

This is a common function of file I/O. OPEN function is used to open a device. It returns an integer variable. If the value is-1, an error occurs when the file is opened, if it is a value greater than 0, this value represents the file descriptor. The general syntax is if (FD = open ("/dev/ttys0", o_rdwr | o_noctty | o_ndelay) <0 ){

Perror ("open ");

}

FD is a device descriptor. When operating a hardware device, Linux shields the basic hardware details and only uses the hardware as a file for operations, all operations start with the open function, which is used to obtain the FD, and other later operations control the FD to complete the actual operations on the hardware device. The/dev/ttys0 you want to open represents the serial port 1, which is commonly referred to as COM1, followed by some control words. Int open (const char * pathname, int Oflag ,... /*, Mode_t mode */); this is the formula of the open function. There are many control words. I will list them for you now:

O_rdonly is read-only.

O_wronly only writes open.

O_rdwr reads and writes are enabled.

O_append is added to the end of the file each time it is written.

O_creat: if the file does not exist, create it. When using this option, you must specify the third parameter mode to indicate the access permission of the new file.

If o_excl is specified at the same time and the file already exists, an error occurs. This can test whether a file exists. If it does not exist, create the file as an atomic operation.

O_trunc if the file exists and is read-only or only successfully opened, the length of the file is truncated to 0.

O_noctty if pathname refers to a terminal device, the device is not allocated as the control terminal of the process.

O_nonblock if pathname refers to a FIFO, a block special file, or a character special file, this option sets the non-blocking mode for this file's current open operation and subsequent I/O operations.

O_sync enables each write operation to wait until the physical I/O operation is completed.

These control words are separated by the "or" symbol (|)

When the system calls open, the operating system installs file_operations in inode of the file system's corresponding device file into file_struct in task_struct of the user process, and then calls the OPEN function in file_operations of the specific file, the same applies to other read and write operations. Therefore, the open operation is actually a connection process. I personally think it is easy to mislead people to call it open. For the operations on underlying hardware in file_operations, simply put, they are actually reading and writing the control/status registers and data registers of specific hardware interfaces. hardware such as hard disks has their own controllers, therefore, he periodically processes the register commands and data internally to complete the corresponding operation request. After the request is completed, the CPU is interrupted.


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.