POSIX shared memory for Linux network programming

Source: Internet
Author: User

Today we continue to study POSIX IPC objects, this time by learning how to use POSIX shared memory, starting with:

The following program is written to create a shared memory:

Compile run:

Where is POSIX's shared memory stored? The POSIX message queue of the upper secondary school is to create a message queue in the virtual file, which requires us to manually mount it to a directory to see, and the same,POSIX shared memory needs to be mounted , but this mount operation is done by the system , without our manual to operate, has been mounted to the/DEV/SHM under the following:

The next function to describe is to modify the size of the shared memory:

description: Actually the Ftruncate function can also modify the size of the file.

Modify the program below to use it:

Compile run:

where struct stat is structured as:

Modify the program as follows:

Compile run:

This is a relatively simple experiment:

Compile run:

Now that we have created a shared memory object, how do we use it? You need to use the following function:

To use the following, after the mapping succeeds, the data is written to memory and then read from memory:

Next, do a fault-tolerant processing:

In fact, the mapping failure has a special macro definition, which can be learned from the man help:

So, instead of the-1 program will be more readable:

Compile run:

What is this for? Or from the man help to find the answer:

So the cause of the problem is found, then modify the open mode to either:

Compile and run again:

There is no successful write, you need to write a read program to verify:

Compile run:

You can actually use the shell command to view the contents of the shared memory directly:

OK, this time to learn something relatively simple, next time to continue ~

POSIX shared memory for Linux network programming

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.