Ftok () function Introduction

Source: Internet
Author: User

When the system establishes IPC communication (such as message queue and shared memory), an ID value must be specified.
. Generally, this ID value is obtained through the ftok function.
.
The ftok prototype is as follows:
Key_t ftok (char * fname, int ID)
Parameter description:

Fname indicates the document name you specified.

ID is a subserial number.
Return Value:

In the general UNIX implementation, the index node number of the document is taken out, and the return value of key_t is obtained by adding the sub-number before it.

For example, if the index node number of a specified document is 65538, it is converted into a hexadecimal value of 0x010002, And the id value you specified is 38, it is converted into a hexadecimal value of 0x26, then the final key_t return value is 0x26010002.
The method for querying the document index node number is: ls-I
After the document reconstruction is deleted, the index node number is allocated by the operating system according to the usage of the document system at that time. Therefore, the index node number obtained is different from the original one.
If you want to ensure that the key_t value remains unchanged, make sure that the ftok document is not deleted.

, Or you do not need to use ftok to specify a fixed key_t value.
In addition, there are multiple file systems on AIX and other operating systems, and two documents distributed on different file systems will have the same index node number, in this case, use ftok to perform operations on the two documents. As long as the ID parameter remains the same, the obtained key_t value is the same, causing Message Queue creation failure. However, this situation is rare.

Because multiple system platforms are involved in development, ftok () functions are found to be different under different platforms during system migration. Of course, the root cause is not the ftok () itself, but the differences in the file system management of the operating system.

The test code is as follows:
[Code] # include <stdio. h>;
# Include <sys/IPC. h>;

Main ()
{
Sprintf ("Key = % 0x/N", ftok ("aaa.txt", 1000 ));
}
[/Code]

Operating systems involved in the test: RedHat as3u3, SCO openserver 5.0.6, unixware 7.1.1, and Solaris 9x86 u7.

In the Rh as3environment, the output value of the program changes after the aaa.txt is modified, indicating that the storage location of the file has changed after the modification.

The same program is executed under SCO openserver 5.0.6, unixware 7.1.1, and Solaris 9x86 u7. no modification to the file content of aaa.txt has no effect on program output.

Therefore, if ftok () is used in Linux to generate an IPC key value and ftok () is related to the configuration file, the application must be restarted after the configuration file is changed. Otherwise, unexpected consequences will occur!

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.