Linux Ftok () function--shared memory of multi-process IPC

Source: Internet
Author: User
Tags function prototype

An ID value must be specified when the system establishes an IPC communication (such as Message Queuing, shared memory). Typically, this 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 sub-ordinal, although it is an int, but only 8 bits are used (0-255).

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

In a typical UNIX implementation, the index node number of the file is taken out, preceded by a sub-ordinal to get the return value of key_t. If the specified file has an index node number of 65538, converted to 16 in 0x010002, and you specify an ID value of 38, converted to 16 0x26, then the last 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 <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>

#define Ipckey 0x11
int main (void)
{
int i=0;
for (i = 1; i <; + + i)
printf ("key =%x\n", Ftok ("/tmp", i));

return 0;
}

After a successful acquisition of key, you can use the key as the key value for interprocess communication of a method, such as how to Shmget shared memory.

The function prototype for Shmget 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 Shmget is generated by the Ftok way.

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

removing shared memory
if ((Shmctl (Shmid, Ipc_rmid, 0) < 0))
{
printf ("Shmctl error:%s\n", Strerror (errno));
return-1;
}
}

Shared memory situation between 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);
}

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) (shmpt R + 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) (SHMP TR + 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);
}


Linux Ftok () function--shared memory of multi-process IPC

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.