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. |