Use of the Ftok () function

Source: Internet
Author: User

In the previous article,Mayuyu described the principle of shared memory and how to use it. Before you create shared memory, you must specify an ID value, which is usually obtained by the Ftok () function that is now spoken. The ftok () function is prototyped as follows

Where the parameter fname is the specified file name, the file must be present and accessible. ID is a sub-ordinal, which is an integer of 8bit . That is, the range is 0~255.

when the function executes successfully, the KEY_T key value is returned, or 1 is returned. In general Unix, the index node of the file is usually taken out and then the key_t value is obtained by adding a sub-ordinal to the front face. The way to view the file index node number is to use the command:ls-i. For example, the specified file is shareand the index number is 787706, which corresponds to the 0xc04faof the binary,

The sub-sequence number is set to 0, then the key value is 0x000204fa, as

In the previous article, the parameter key for theshmget () function was generated in this way. In addition, the keyin the shmget () function can also be used with ipc_private or 0, which creates a new shared memory object.

A trap for the Ftok () function

When using the ftok () function, there are two parameters, that is , fname and ID,fname is the specified file name, and the ID is a sub-sequence number, the return value of this function is key, which is related to the index node number and the sub-serial number ID of the specified file, which gives us a misconception that the resulting key value will never change as long as the file path, name and sub-sequence number are not changed.

In fact, this understanding is wrong, think about, if there is a situation: in Access to the same shared memory of multiple processes call Ftok () time period, if the fname point to the file or directory is deleted and re-created, Then the file system will give the same name to the new I node information, so the process calls Ftok () will return to normal, but the key value key is not necessarily the same. The possible consequence is that these processes originally intended to access an identical shared memory object, but because of their respective key values, the shared memory that the process points to is no longer consistent, and if the shared memory is created, no errors will be reported on the surface during the entire application run. However, the purpose of data transfer through a shared memory object cannot be achieved.

This is a very important question, I hope to remember!!!

So make sure the key value is not changed, either make sure that the ftok () file is not deleted or not Ftok (), specifying a fixed key value.

Use of the Ftok () function

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.