20. Notes on using IPC shared memory

Source: Internet
Author: User

1. IPC Defects

1) xsi IPC is kernel-related and exists throughout the system, but does not reference the count, which may result in a waste of resources or data insecurity. Therefore, when the process ends, the content in the structure needs to be explicitly deleted. Unlike pipelines, pipelines are completely deleted as soon as the process ends.

2) use an independent namespace and add many new system calls, resulting in complicated operating systems. The xsi IPC structure has no name in the file system, and all functions that can be used for files cannot be used. To support them, they have to add more than a dozen new commands. For example, if you cannot use the LS command to view the IPC object, you cannot use the RM command to delete it, and you cannot use the CHMOD command to change their access permissions, you have to add the new command IPCS (1) and ipcrm (1.

3) it is incompatible with file abstraction and cannot use select/epoll or other multiplexing Io. Process and file abstraction are the most basic concepts of UNIX.

2. Advantages of IPC: (1) reliability; (2) stream control; (3) record-oriented

3. Use ftruncate to change the file size. It is usually used before MMAP. Because MMAP stores ing,The Content Length of the mapped file does not exceed the initial size of the file.. Therefore, the file size determines the maximum size of the ing file. You can use ftruncate to set the desired file size.

Ftruncate (FD, size ).

4. In fact, the ing between xsi IPC and MMAP is very similar, but the latter is associated with a file, and the former is to associate a physical memory with the virtual address space of the current process.

5. After fork,It will obtain copies of the parent process's data space, heap, stack, and other resources, The differences are as follows:

(1) The parent and child process IDs are different;

(2) time-related values (such as time variables) of sub-processes are set to 0;

(3) The filelock of the parent process will not be inherited;

(4) Alarms not processed by sub-processes are cleared;

(5) The unprocessed Signal Set of the sub-process is set as an empty set.

6. Regarding mutex, read/write locks, condition variables and record locks:

Mutex, read/write lock, condition variable for process, process data, child process will also inherit from parent process

The record lock is for files, and the child process does not inherit the record lock from the parent process.

7. Notes

1) # Cat/proc/sys/kernel/shmmax

2) A process can mount the shmat with the same shared memory for multiple times. The physical memory points to the same disk. If the shmaddr is null, the linear address space (in-process address) is returned each time) and the reference count pointing to this shared memory will increase. That is, multiple linear spaces in a process point to the same physical address. In this way, if the linear address of the process that has previously attached this shared memory is not shmdt, that is, the applied linear address is not released, the virtual memory space of the process will be continuously consumed, it is very likely that the linear space of the process will be used up and the next shmat or other operations will fail.

3) when using shared memory, the key is used as the unique identifier of the shared memory. The size of the created shared memory can only be reduced, but cannot be increased.

Set the key to ipc_private, so that the operating system will ignore the key, create a new shared memory, specify a key value, and then return the ID of this shared memory IPC identifier.

4) when a process is being created to notify other processes of mounting, we recommend that you do not use the ftok method to obtain the key, but use the file or inter-process communication method. [4]

5) after the shmdt (const void * shmaddr) is called to remove the process from the shared memory zone, the shm_nattch in the data structure shmid_ds will be reduced by 1. However, the shared block memory still exists. Only when the shm_attch value is 0, that is, no process can use the shared memory area. The shared memory area is deleted from the kernel. Generally, when a process is terminated, the shared memory zone attached to it is automatically detached. You can also call the shmctl function to explicitly Delete the object.

6) if the shared memory has been disconnected from all the processes that access it, the system will immediately delete the identifier of the shared memory and delete the shared memory after the ipc_rmid subcommand is called, and all related data structures. If other processes are still connected to the shared memory, the shared memory will not be immediately deleted from the system after the ipc_rmid sub-command is called, it is set to the ipc_private status and marked as "deleted" (you can see the Dest field using the IPCS command) until all existing connections are disconnected, the shared memory will eventually disappear from the system.

7) message queues and semaphores are System Objects in the kernel space. Additional data copies are required through their data in the kernel and user space. The shared memory and all its applications are accessed.ProgramIn the same user space, application processes can directly read and write memory through address ing to achieve high communication efficiency.

8) when creating shared memory, the shmflg parameter requires at least ipc_creat | permission ID. If only ipc_creat is used, the requested address is k = 0 xffffffff and cannot be used. When obtaining the created shared memory, do not use ipc_creat for shmflg (only permission IDs when the shared memory is created, such as 0640). Otherwise, in some cases, for example, if you use ipcrm to delete shared memory, you can use this function and use the ipc_creat parameter to obtain the shared memory once (of course, the acquisition fails), even if you create the shared memory again, in this case, you must change the key to recreate the shared memory. [5]

9) for 64-bit processes, the same process can connect up to 268435456 shared memory segments. For 32-bit processes, the same process can connect up to 11 shared memory segments, unless an extended shmat is used.

Reference

[1]Http://blog.sina.com.cn/s/blog_50cbca2d0100r2uo.html

[2]Http://www.cnblogs.com/mydomain/archive/2011/09/14/2176573.html

[3] describes a case generated by ftok but with the same key:

Http://hi.baidu.com/baiduqa/blog/item/0a57a5b1fb70fdb4d9335a15.html

[4] common traps and Analysis

Http://www.360doc.com/content/11/0916/17/5407405_148782268.shtml

[5] Good Summary

Http://www.ibm.com/developerworks/cn/aix/library/au-cn-sharemem/index.html

[6]Http://www.cnblogs.com/skyme/archive/2011/01/04/1925404.html

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.