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)
The name of the file you specified when fname is used. ID is the sub-serial number.
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.
The following is a specific code example:
Key_t keysharemem;
If (keysharemem = ftok (afc_1__memory_name.c_str (), 0) =-1 ){
Cerr <"error:" <m_nthisthreadtype <"cbasemessagedeal () keysharemem ftok:" <errno <":" <strerror (errno) <Endl;
Throw new afcinitafcresourceexception ("cbasemessagedeal: cbasemessagedeal ftok keysharemem ");
}
If (m_shmid = shmget (keysharemem, 0, afc_shm_rw) <0 ){
Cerr <"error:" <m_nthisthreadtype <"cbasemessagedeal () shmget exist:" <errno <":" <strerror (errno) <Endl;
Throw new afcinitafcresourceexception ("cbasemessagedeal: cbasemessagedeal () shmget exist ");
}
If (m_afcsharememorybegin = (char *) shmat (m_shmid, null, 0) = (void *)-1 ){
Cerr <"error:" <m_nthisthreadtype <"cbasemessagedeal () shmat:" <errno <":" <strerror (errno) <Endl;
Throw new afcinitafcresourceexception ("cbasemessagedeal: cbasemessagedeal shmat ");
}
Trackback: http://tb.donews.net/TrackBack.aspx? Postid = 893843