Linux shared memory technology

Source: Internet
Author: User
Linux shared memory technology-general Linux technology-Linux programming and kernel information. The following is a detailed description. Linux shared memory technology
Baidu

The source code is copied from others' articles. It is not original, but well written. It mainly describes two different processes accessing data from the shared memory, I added some analysis:

The first is to write data:
/***** Testwrite. c *******/
# Include
# Include
# Include
# Include
Typedef struct {
Char name [4];
Int age;
} People;
Main (int argc, char ** argv)
{
Int shm_id, I;
Key_t key;
Char temp;
People * p_map;
Char * name = "/dev/shm/myshm2 ";
Key = ftok (name, 0 );
If (key =-1)
Perror ("ftok error ");
/* First Use ftok to create a key, then call shmget to create a shared memory area */
Shm_id = shmget (key, 4096, IPC_CREAT );
If (shm_id =-1)
{
Perror ("shmget error ");
Return;
}
/* Append this shared memory area to your memory segment */
P_map = (people *) shmat (shm_id, NULL, 0 );
Temp = 'a ';
For (I = 0; I <10; I ++)
{
Temp + = 1;
Memcpy (* (p_map + I). name, & temp, 1 );
(* (P_map + I). age = 20 + I;
}
/* After writing data, delete it from its memory segment */
If (shmdt (p_map) =-1)
Perror ("detach error ");
}

/* The read process must first obtain the memory block. At this time, the key created by ftok Based on the file name and ID is
Very useful. The steps are the same as those for creation */
/********** Testread. c ************/
# Include
# Include
# Include
# Include
Typedef struct {
Char name [4];
Int age;
} People;
Main (int argc, char ** argv)
{
Int shm_id, I;
Key_t key;
People * p_map;
Char * name = "/dev/shm/myshm2 ";
Key = ftok (name, 0 );
If (key =-1)
Perror ("ftok error ");
Shm_id = shmget (key, 4096, IPC_CREAT );
If (shm_id =-1)
{
Perror ("shmget error ");
Return;
}
P_map = (people *) shmat (shm_id, NULL, 0 );
For (I = 0; I <10; I ++)
{
Printf ("name: % s \ n", (* (p_map + I). name );
Printf ("age % d \ n", (* (p_map + I). age );
}
If (shmdt (p_map) =-1)
Perror ("detach error ");
}

Note: in fact, the ftok function only generates an identifier key based on the file name and ID. According to the GNU Linux programming guide, there are still some problems to implement this function in Linux, it is not recommended. Therefore, I generally define a unique key myself without using the ftok function.

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.