How function Ftok in Linux produces key values

Source: Internet
Author: User
Tags file info function prototype

When we do the Linux interprocess communication development, we often use the Ftok function to produce the unique key value of the text, then how this key value is generated.

Function prototype:key_t ftok (const char * fname, int id); Application:
key_t Key=ftok (".", ' A ');


FName is a file name that already exists, this article is "." Represents the current directory, the ID is a sub-ordinal, and the value range is only 8bits (0-255).



Here we give an example of how to generate the key value, the code ftok_test.c as follows:

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <string.h>
    4. #include <sys/stat.h>
    5. int main ()
    6. {
    7. Char filename[50];
    8. struct stat buf;
    9. int ret;
    10. strcpy (FileName, ".");
    11. RET = stat (filename, &buf);
    12. if (ret)
    13. {
    14. printf ("Stat error\n");
    15. return-1;
    16. }
    17. printf ("The file Info:ftok (filename, ' a ') =%x, St_ino =%x, St_dev =%x\n", ftok (filename, ' a '), Buf.st_ino, Buf.st_ DEV);
    18. return 0;
    19. }
Copy Code

The results of the operation are as follows:
$./a.out
The file Info:ftok (filename, ' A ') = 41015bb5, St_ino = c5bb5, St_dev = Ca01

The 16 process value of the capital letter ' a ' is 0x41.
You can see that the key value is a sub-ordinal value is the capital letter ' a ' 16 process value "41", plus st_dev two bits "01", plus st_info four bits "5BB5" composition "5bb5".

Note: The stat structure is as follows:
struct STAT {
unsigned long st_dev;//device number of the file
unsigned long St_ino;//Node
unsigned short st_mode;//file type and access permissions
unsigned short st_nlink;//The number of hard connections to the file, the newly created file value is 1
unsigned short st_uid;//user ID
unsigned short st_gid;//group ID
unsigned long st_rdev;
unsigned long st_size;
unsigned long st_blksize;
unsigned long st_blocks;
unsigned long st_atime;
unsigned long st_atime_nsec;
unsigned long st_mtime;
unsigned long st_mtime_nsec;
unsigned long st_ctime;
unsigned long st_ctime_nsec;
unsigned long __unused4;
unsigned long __unused5;
};

How function Ftok in Linux generates key values

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.