Linux ftok () function

Source: Internet
Author: User


The linux ftok () function system must specify an ID value for 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) indicates the name of the fname you specify (this file must exist and be accessible), and id indicates the sub-serial number, although it is an int, only 8 bits are used (0-255 ). When the execution is successful, a key_t value will be returned, otherwise-1 will be returned. 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. The method for querying the file index node number is: ls-I www.2cto.com and below is the test program: # include <stdio. h> # include <sys/types. h> # include <sys/ipc. h> # define IPCKEY 0x11int main (void) {int I = 0; for (I = 1; I <256; ++ I) printf ("key = % x \ n", ftok ("/tmp", I); return 0;} after the key is obtained successfully, you can use this key as the key value for inter-process communication in a certain way, such as the shmget shared memory mode. The shmget function prototype is int shmget (key_t, size_t, flag). After successful creation, the shared memory descriptor is returned. The key_t used in shmget is an instance generated using ftok: www.2cto.com # include <sys/shm. h> # include <stdio. h> # include <errno. h> # include <string. h> # include <stdlib. h> # define SIZE 1024 extern int errno; int main () {int shmid; char * shmptr; // create shared memory if (shmid = shmget (IPC_PRIVATE, SIZE, (0600) <0) {printf ("shmget error: % s \ n", strerror (errno); return-1 ;} // connect the shared memory to the available address. if (shmptr = (char *) shmat (shmid, 0, 0) = (void *)-1) {printf ("shmat error: % s \ n", strerror (errno); return-1;} memcpy (shmptr, "hello world ", sizeof ("hello world"); printf ("share memory from % lx to % lx, content: % s \ n", (unsigned long) shmptr, (unsigned long) (shmptr + SIZE), shmptr); // disassemble shared memory if (shmctl (shmid, IPC_RMID, 0) <0) {printf ("shmctl error: % s \ n ", strerror (errno); return-1; www.2cto.com} shared memory among multiple processes: # include <sys/shm. h> # include <stdio. h> # include <errno. h> # include <string. h> # include <stdlib. h> # include <unistd. h> # include <sys/types. h> # include <sys/wait. h> # define SIZE 1024 extern int errno; int main () {int shmid; char * shmptr; key_t key; pid_t pid; if (pid = fork () <0) {printf ("fork error: % s \ n", strerror (errno); return-1;} else if (pid = 0) {sleep (2 ); if (key = ftok ("/dev/null", 1) <0) {printf ("ftok error: % s \ n", strerror (errno )); return-1;} if (shmid = shmget (key, SIZE, 0600) <0) {printf ("shmget error: % s \ n ", strerror (errno); exit (-1); www.2cto.com} if (shmptr = (char *) shmat (shmid, 0, 0) = (void *) -1) {printf ("shmat error: % s \ n", strerror (errno); exit (-1) ;}// memcpy (shmptr, "hello world ", sizeof ("hello world"); printf ("child: pid is % d, share memory from % lx to % lx, content: % s \ n", getpid (), (unsigned long) shmptr, (unsigned long) (shmptr + SIZE), shmptr); printf ("child process sleep 2 seconds \ n"); sleep (2 ); if (shmctl (shmid, IPC_RMID, 0) <0) {printf ("shmctl error: % s \ n", strerror (errno )); exit (-1);} exit (0);} // parent else {if (key = ftok ("/dev/null", 1) <0) {printf ("ftok error: % s \ n", strerror (errno); return-1;} if (shmid = shmget (key, SIZE, 0600 | IPC_CREAT | IPC_EXCL) <0) {printf ("shmget error: % s \ n", strerror (errno); exit (-1 );} if (shmptr = (char *) shmat (shmid, 0, 0) = (void *)-1) {printf ("shmat error: % s \ n ", strerror (errno); exit (-1); www.2cto.com} memcpy (shmptr, "hello world", sizeof ("hello world"); printf ("parent: pid is % d, share memory from % lx to % lx, content: % s \ n ", getpid (), (unsigned long) shmptr, (unsigned long) (shmptr + SIZE), shmptr); printf ("parent process sleep 2 seconds \ n"); sleep (2); if (shmctl (shmid, IPC_RMID, 0) <0) {printf ("shmctl error: % s \ n", strerror (errno); exit (-1) ;}} waitpid (pid, NULL, 0 ); exit (0);} author's Track 16

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.