Linux environment Programming-ftok () function detailed

Source: Internet
Author: User
Tags exit function prototype printf sleep

The system must specify an ID value when it establishes an IPC communication, such as Message Queuing, shared memory. Typically, the ID value is obtained through the Ftok function.

The Ftok prototype is as follows:

key_t Ftok (char * fname, int id)

FName The file name you specify (the file must be present and accessible), the ID is a substring, although it is int, but only 8 bits are used (0-255).

When executed successfully, a key_t value is returned, otherwise-1 is returned.

In a common UNIX implementation, the index node number of the file is taken out, preceded by a substring to obtain the return value of the key_t. If the specified file has an index node number of 65538, converted to 16 to 0x010002, and you specify an ID value of 38, converted to 16 to 0x26, the final key_t return value is 0x26010002.

The method for querying the file index node number is: Ls-i

The following is a test program:

#include

#include

#include

#define Ipckey 0x11

int main (void)

{

int i=0;

for (i = 1; i < 256; + i)

printf ("key =%xn", Ftok ("/tmp", i));

return 0;

}

After the key is successfully acquired, it can be used as a key value for interprocess communication of some method, such as the way Shmget shared memory.

Shmget's function prototype is

int Shmget (key_t, size_t, flag);

After the creation is successful, the descriptor for the shared memory is returned. The key_t used in the Shmget is generated by the Ftok method.

Instance:

#include

#include

#include

#include

#include

#define SIZE 1024

extern int errno;

int main ()

{

int shmid;

Char *shmptr;

Creating Shared Memory

if ((Shmid = Shmget (Ipc_private, SIZE, 0600)) < 0)

{

printf ("Shmget error:%sn", Strerror (errno));

return-1;

}

Connect shared memory to an available address

if ((Shmptr = (char*) shmat (shmid, 0, 0)) = = (void*)-1)

{

printf ("Shmat error:%sn", Strerror (errno));

return-1;

}

memcpy (shmptr, "Hello World"), sizeof ("Hello World");

printf ("Share memory from%LX to%LX, content:%sn", (unsigned long) shmptr, (unsigned long) (shmptr + SIZE), shmptr);

Remove shared memory

if ((Shmctl (Shmid, Ipc_rmid, 0) < 0))

{

printf ("Shmctl error:%sn", Strerror (errno));

return-1;

}

}

Shared memory between multiple processes:

#include

#include

#include

#include

#include

#include

#include

#include

#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:%sn", Strerror (errno));

return-1;

}

else if (PID = = 0)

{

Sleep (2);

if (key = Ftok ("/dev/null", 1)) < 0)

{

printf ("Ftok error:%sn", Strerror (errno));

return-1;

}

if ((Shmid = Shmget (key, SIZE, 0600)) < 0)

{

printf ("Shmget error:%sn", Strerror (errno));

Exit (-1);

}

if ((Shmptr = (char*) shmat (shmid, 0, 0)) = = (void*)-1)

{

printf ("Shmat error:%sn", Strerror (errno));

Exit (-1);

}

memcpy (shmptr, "Hello World"), sizeof ("Hello World");

printf ("Child:pid is%d,share memory from%LX to%LX, Content:%sn", Getpid (), (unsigned long) shmptr, (unsigned long) (shmptr + SIZE

), shmptr);

printf ("Child process sleep 2 Secondsn");

Sleep (2);

if ((Shmctl (Shmid, Ipc_rmid, 0) < 0))

{

printf ("Shmctl error:%sn", Strerror (errno));

Exit (-1);

}

Exit (0);

}

Parent

Else

{

if (key = Ftok ("/dev/null", 1)) < 0)

{

printf ("Ftok error:%sn", Strerror (errno));

return-1;

}

if (Shmid = Shmget (key, SIZE, 0600| Ipc_creat| IPC_EXCL)) < 0)

{

printf ("Shmget error:%sn", Strerror (errno));

Exit (-1);

}

if ((Shmptr = (char*) shmat (shmid, 0, 0)) = = (void*)-1)

{

printf ("Shmat error:%sn", Strerror (errno));

Exit (-1);

}

memcpy (shmptr, "Hello World"), sizeof ("Hello World");

printf ("Parent:pid is%d,share memory from%LX to%LX, Content:%sn", Getpid (), (unsigned long) shmptr, (unsigned long) (shmpt R + SIZE

), shmptr);

printf ("Parent process sleep 2 secondsn");

Sleep (2);

if ((Shmctl (Shmid, Ipc_rmid, 0) < 0))

{

printf ("Shmctl error:%sn", Strerror (errno));

Exit (-1);

}

}

Waitpid (pid,null,0);

Exit (0);

}

The output is:

The role of Shmctl (Shmid, Ipc_rmid, 0) is to remove the Congratulations store segment from the system. Because each shared storage segment has a connection count (Shm_nattch in the SHMID_DS structure), the storage segment is not actually deleted unless the last process using the segment terminates with the segment

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.