Linux ftok () function

Source: Internet
Author: User
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), 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.
To query the node number of a file index, use LS-I.

The following is a testProgram:
# Include <stdio. h>
# Include <sys/types. h>
# Include <sys/IPC. h>

# Define ipckey 0x11
Int main (void)
{
Int I = 0;
For (I = 1; I <256; ++ I)
Printf ("Key = % x \ n", ftok ("/tmp", I ));

Return 0;
}

After obtaining the key, you can use the key as the key value for inter-process communication in a certain method, such as shmget shared memory.

Shmget function prototype is

Int shmget (key_t, size_t, flag );

After the shared memory is created, the shared memory descriptor is returned. The key_t used in shmget is generated by ftok.

Instance:

# 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 an 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 );

// Remove the shared memory
If (shmctl (shmid, ipc_rmid, 0) <0 ))
{
Printf ("shmctl error: % s \ n", strerror (errno ));
Return-1;
}
}

Multi-process shared memory:

# 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 );
}

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 );
}
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 );
}

Output:

shmctl (shmid, ipc_rmid, 0) the purpose is to delete the congratulations bucket from the system. Because each shared storage segment has a connection count (shm_nattch in the shmid_ds structure), unless the last process in the segment is used to terminate the disconnection from the segment, otherwise, the bucket is not actually deleted

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.