Open command in the process to Open the file

Source: Internet
Author: User
Article Title: use the Open command to Open a file in the process. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

When developing script programs, Unix System Engineers often need to use Open functions in the process to Open a file. For example, when writing an installer, you may need to obtain relevant parameters from a configuration file. In this case, the file is opened during the installation process. At this time, system commands such as vi cannot be used. It must be implemented through system calls such as Open. In fact, when you use the vi command to open and compile a file, the background is also implemented through the open system call.

1. Basic functions of Open commands.

In the process, the format of the Open command is int open (const char * pash, int oflag ,.....). This open command can not only open files, but also create files. If the file exists, the Open command will Open the file. If the file does not exist, the Open command will create the file. We can see that there is an Int keyword in front of the Open command, which indicates that after the Open command is used, the system will return a file descriptor, which is an integer value and a minimum integer that can be allocated. It is like another format file name. This file descriptor is used when you Open a file using the Open command and Close the file. When writing a script program, the system engineer needs to know when to use this Open command to Open the file. If the script program only needs to use the attributes of the file, for example, writing a file backup program requires the attribute of the modification time of the file, you do not need to use the Open command at this time. Because this time attribute is not saved in the file, but saved in Inode. Therefore, System Engineers need to know what is saved in the file and what is not saved in the file. Generally, all attributes of a file, such as the owner, modification time, and permissions, are not stored in the file. When a process accesses this information, it does not need to use the Open command to Open the file. The Open command is required only for operations on the content of a file, such as read/write operations on a file. The most common example is to write the installation log to the relevant files during the installation process.

Ii. File Opening Method.

When you use the Open command to Open a file, unlike the Vi command, you need to display how to Open the file. For example, if you want to open a configuration file for an installer, you must specify whether to open the file in read-only mode or read/write mode. In the parameter types of Open commands, three symbolic constants are used to represent three different Open modes. The symbol constant O_RDONLY indicates that the file is opened only for read operations; the symbol constant O_WRONLY indicates that the file is opened only for write operations; the symbol constant O_RDWR indicates that the file is opened for read and write operations. To prevent accidental operations on files, you must open the file in an appropriate way. For example, if an installation process only needs to read parameters from the configuration file and does not write content to it, it is best to open it in O_RDONLY read-only mode. If you need to write the Installation Log to a file during installation, you can open it in O_WRONLY write-only mode. It can be seen that when you deploy related files, it is best to be specific to files. Each file is only responsible for a specific purpose, which is conducive to improving the reuse of these files.

These constants are defined in the fcntl. h file. Therefore, you often need to insert the # include statement at the beginning of the application code. Only in this way can the application recognize the meaning represented by these constants. I once again stressed that the type of name file opening that must be displayed when the Open command is used to Open the file. To prevent accidental damage to the configuration file, you must use the minimum permission to Open the file by using the Open command.

However, these file opening modes cannot meet the needs of System Engineers. For example, we have compiled a file backup program. After each file backup, we need to add the backup program to the log file. For example, the backup program starts and ends, and the files backed up. This information is conducive to future maintenance and performance adjustment. Therefore, we need this backup program to append the logs to the original log files. To complete these special functions, some common functions are pre-defined in Unix operating procedures, without the need for our system administrator to implement them through complicated coding. To meet different needs, the Unix operating Kernel provides the following functions. Like the open mode, constants are used to represent different functions. For example, the symbolic constant O_APPEND indicates that the file is opened in append mode (note that the file opening mode cannot be read-only). The symbolic constant O_EXCL indicates that if the file exists and is opened in O_CREAT mode, an error code is returned, which can prevent files from being forcibly overwritten. For example, the symbolic constant O_SYNC indicates that the read/Write operation is executed synchronously, that is, the Write command must be allowed to return the data after the data is written to the disk, this ensures that the modified content is saved to the hard disk, rather than in the memory. When System Engineers need to open a file to complete specific functions, they only need to call the relevant character constants. Instead of writing code. This improves the efficiency of System Engineers in developing programs. These constants are common in different Unix operating systems, so there is no need to worry about compatibility issues. For example, you need to open a file as an append to save the daily backup information. You can open the file as follows:

Open(javasbacklist.txt ", O_WRONLY | O_APPEND

This command is used to open backlist.txt In the write-only method, and append relevant log information to the file. At this time, the relevant information will be written from the end of the file. That is to say, after using this function, the system will automatically set the pointer to the end of the file when opening the file. The subsequent file write operations will not overwrite the original content, but will constantly append records to the end of the file, and the file size will also increase. Note that the file opening mode and function character constants are separated by the | symbol. In addition, the first parameter of the Open command is the path and name of the file to be opened. This path can be an absolute or relative path. However, it is best to use the relative path, so that the application location changes will not affect the path in the Open command.

3. Allow multiple processes to share one file.

When an Open command is used to Open a file with the O_CREAT character constant, the system creates a file if the file does not exist. If the file exists, the original file will be overwritten. Therefore, when using the O_CREAT function, System Engineers need to pay attention to whether the original file is unnecessary. Generally, the constant O_CREAT is used only in log management of program installation. Because the first installation log often plays no role in the second installation process. To facilitate the Administrator to view the installation log, the previous Installation Log is deleted.

However, in most cases, using the O_CREAT method is not a good choice. For example, when a process accesses a file at the same time, if one of the files uses the O_CREAT function to overwrite the entire file, access to other processes will be fatal. To improve the harmony of multiple processes sharing one file, System Engineers need to use the O_Excl function in the Open command. It can restrict other processes to use the O_CREAT function to overwrite files during access. This is a lock mechanism in which multiple processes share the same file.

For example, when a certain process is running the backlist.txt file, a lock will be applied to the file before the updated content is written to the file. After the write is completed, the lock will be removed. If the O_Excl function is used in the Open operation, only the first Open call succeeds, and other Open calls fail. That is to say, at the same time, only one process can open the file to prevent incorrect file reading. This is much better than permission control. At this time, multiple user processes can still share the file, as long as it is not operating on the file within the same time.

Using Open commands in the process to Open a file is a prerequisite for the program to operate on the file. You can only use the Open command to Open a file before the application can perform operations on the file, such as adding content, deleting content, and updating information. Therefore, in application programming, you must use appropriate methods and permissions to open relevant files. If multiple processes need to access the same file, it is best to use a lock mechanism to ensure that each process can share the file harmoniously.

 

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.