Linux programming-how to use shared memory functions

Source: Internet
Author: User

During the experiment on shared memory today, we found that the usage of functions related to semaphores used in the previous experiment was forgotten .. It is a waste of time to search again. I decided to drag this kind of things from others' blogs in the future to facilitate searching in the future...

To use shared memory, follow these steps:
1. Open up a shared memory shmget ()
2. allow this process to use a shared memory shmat ()
3. Write/read
4. prohibit this process from using this shared memory shmdt ()
5. Delete this shared memory shmctl () or ipcrm under the command line


Ftok (). It has two parameters: a string and a character. Generally, the program name of the current process is used as a string to mark the shared memory identified by this identifier as the first shared memory opened by this process. Ftok () returns a value of the key_t type, that is, the calculated Identifier value.

Shmkey = ftok ("mcut", 'A'); // calculate the identifier

The following function is used to operate the shared memory.
# Include <sys/types. h>
# Include <sys/IPC. h>
# Include <sys/SHM. h>

Int shmget (key_t shmkey, int shmsiz, int flag );
Void * shmat (INT shmid, char * shmaddr, int shmflag );
Int shmdt (char * shmaddr );

Shmget () is a function used to open/point to a shared memory. Parameters are defined as follows:
Key_t shmkey is the identifier of the shared memory. For inter-process communication of the parent-child relationship, this identifier is replaced by ipc_private. However, our two processes have nothing to do with each other, so we use ftok () to calculate an identifier.
Int shmsiz is the memory size.
Int flag is the memory mode and permission ID.
The mode can be set to ipc_creat.
Use the opened memory: ipc_alloc
If the identifier already exists, the error value ipc_excl is returned.
Then, the "Mode" and "permission ID" are calculated as "or" and used as the third parameter.
For example, ipc_creat | ipc_excl | 0666
If this function succeeds, the ID of the shared memory is returned. If the function fails, the value-1 is returned.

// Open shared memory in shmid

Shmid = shmget (shmkey, sizeof (in_data), ipc_creat | 0666 );

Shmat () is a function used to allow the process to access a shared memory.
Int shmid is the ID of the shared memory.
Char * shmaddr is the starting address of the shared memory.
Int shmflag is the memory operation mode of the current process. If it is shm_rdonly, it is read-only mode. The other is the read/write mode.
If successful, this function returns the starting address of the shared memory. -1 is returned if the request fails.

Char * head, * POs,

Head = Pos = shmat (shmid, 0, 0 );

// Allow this process to use this shared memory

Shmdt (), in contrast to shmat (), is a function used to prohibit the process from accessing a shared memory.
The char * shmaddr parameter is the starting address of the shared memory.
If the call succeeds, 0 is returned. -1 is returned if the request fails.

Shmdt (head); // disable this process from using this memory.

In addition, there is a shmctl () function used to control the shared memory as follows:
# Include <sys/types. h>
# Include <sys/IPC. h>
# Include <sys/SHM. h>

Int shmctl (INT shmid, int cmd, struct shmid_ds * BUF );
Int shmid is the ID of the shared memory.
Int CMD is a control command with the following values:
Ipc_stat obtains the shared memory status.
Ipc_set changes the shared memory status
Ipc_rmid: delete shared memory
Struct shmid_ds * Buf is a struct pointer. In ipc_stat, the obtained status is placed in this struct. To change the shared memory status, use this struct.
Returned value: Success: 0
Failed:-1

Shmctl (shmid, ipc_rmid, null );

Just now, we can add a few more words in our mpaste. C program.

Struct shmid_ds Buf;
......
Shmctl (shmid, ipc_stat, & BUF); // gets the shared memory status
......
Shmctl (shmid, ipc_rmid, & BUF); // Delete shared memory

Note !!!!!!!!! : When shared memory is used, the program exits. If you have not used shmctl () in the program to delete the shared memory, you must use the ipcrm command to delete the shared memory. If you don't care, it's always there.
The IPCS command and ipcrm command are briefly explained.

 

Obtain IPC information:
IPCS [-M |-q |-S]
-M outputs information about shared memory.
-Q: output information about Message Queue
-S outputs information about the semaphore.
% IPCS-m

Delete IPC
Ipcrm-M |-q |-s shm_id
% Ipcrm-M 105



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.