Linux-ftok Function

Source: Internet
Author: User

Why is this function used? This function is mainly used to generate several keys of IPC that do not conflict with each other?

An ID value must be specified for the system to establish IPC communication (such as message queue and shared memory. Generally, this ID value is obtained through the ftok function.
The ftok prototype is as follows:
Key_t ftok (char * fname, int ID)
Fname indicates the name of the file you specified (the file must exist and be accessible). Id indicates the subsequence number. Although it is an int, only 8 bits are used.

In the general UNIX implementation, the index node number of the file 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 file 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.
To query the node number of a file index, use LS-I.
After the reconstruction file is deleted, the index node number is allocated by the operating system according to the current usage of the file system. Therefore, the index node number is different from the original one.
To ensure that the key_t value remains unchanged, ensure that the ftok file is not deleted or that a fixed key_t value is specified without ftok. For example:

# Define ipckey 0x111

Char path [256];

Sprintf (path, "% S/etc/config. ini", (char *) getenv ("home "));
Msgid = ftok (path, ipckey); [/Code]

The same program is used to ensure that two groups of identical programs under two different users obtain the IPC key values that do not interfere with each other.
Because ETC/config. INI (hypothesis) is the key configuration file of the application system, so there is no problem of being easily deleted-even if it is deleted, it will soon be discovered and rebuilt (and the application system will be restarted ).
Ftok () is designed for this purpose.

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.