Linux Shared Memory Design

Source: Internet
Author: User

POSIX's shared memory is implemented through the tmpfs file system attached to the user space, while the shared memory of System V is implemented by the tmpfs of the kernel itself, the two are actually implemented using the same mechanism. The difference is that user interfaces are different. POSIX aims to provide a unified set of available interfaces instead of implementations, therefore, the upper layer of POSIX does not care whether its mechanism is kernel implementation or user space implementation. Therefore, it is better to use a file system to implement it, in Linux, it is actually implemented through the tmpfs mounted to the user space. The shared memory of System V is different. It is provided by the operating system kernel for interfaces and implementations, therefore, Linux has system calls such as shmget/at for shared memory. Although their underlying implementations are consistent, the shared memory of System V is directly supported by the kernel as one of the IPC provided by the kernel, POSIX is not necessarily implemented. In some systems, it is directly implemented by the kernel, while in other systems, such as Linux, it is implemented by the user space. The POSIX shared memory is strictly dependent on the mounting of the user space tmpfs, there is a judgment in shm_open:

Int shm_open (const char * Name, int Oflag, mode_t Mode)

{

Size_t namelen;

Char * fname;

Int FD;

_ Libc_once (once, where_is_shmfs); // locate the mount point of tmpfs

If (mountpoint. dir = NULL)

{

_ Set_errno (enosys); // If tmpfs is not mounted, it is implemented and an error code is returned.

Return-1;

}

While (name [0] = '/')

++ Name;

If (name [0] = '/0 ')

{

_ Set_errno (einval );

Return-1;

}

Namelen = strlen (name );

Fname = (char *) alloca (mountpoint. dirlen + namelen + 1 );

_ Mempcpy (fname, mountpoint. dir, mountpoint. dirlen), name, namelen + 1 );

FD = open (fname, Oflag | o_nofollow, mode );

...

Return FD;

}

In fact, the POSIX shared memory mechanism is displayed as a complete file system call process in the library process and other parts of the user space. After shm_open is called, you need to call MMAP to map the tmpfs file to the address space, and then you can operate on this file. Note that other processes can also operate on this file, therefore, this file is actually shared memory. In contrast to the shared memory of System V, the kernel directly implements shmget/at system calls. Although it is ultimately implemented by tmpfs, the interface design is completely different from POSIX, POSIX is designed to provide interfaces consistent with all systems, while System V only implements its own logic. Shared Memory is actually only part of IPC in sysv, the final management data structure is also IPC rather than shared memory, such as newseg (IPC/SHM. in file C) The shm_addid function in the function calls ipc_addid (in the file IPC/util. c). Let's take a look at this newseg function:

Static int newseg (key_t key, int shmflg, size_t size)

{

...

} Else {

Sprintf (name, "sysv % 08x", key );

File = shmem_file_setup (name, size, vm_account); // It is finally implemented in tmpfs.

}

...

Id = shm_addid (SHP); // Add it to the unified management data structure of IPC

... // Set the values of each SHP Field

Return SHP-> ID;

}

Note that the shmem_file_setup call only uses an instance of tmpfs. From its implementation, we can see that file-> f_vfsmnt = mntget (shm_mnt); shm_mnt is used, so when is the shm_mnt initialized? In init_tmpfs, there are:

Shm_mnt = do_kern_mount (tmpfs_fs_type.name, ms_nouser, tmpfs_fs_type.name, null );

From the document of Linux, we can see that there must be at least one tmpfs instance in the kernel, that is, shm_mnt, which is specially prepared for the shared memory of System V and has nothing to do with the compilation of tmpfs, although POSIX uses TPFs, it is not the same as the kernel-implemented System V shared memory. The latter MNT instance is created when the user executes the Mount tmpfs, although the instance is different, the operation is the same, as though the class instance is different, but the class is the same class, the data is different but the code is the same. The specific file_operations of tmpfs will not be described in detail. Therefore, we can see that neither syem v nor POSIX shared memory exists after the computer is restarted. Because the data room has physical storage or swap partitions, what is different is that, POSIX is implemented by a user space file, and the user space operates on a file descriptor. The file descriptor belongs to task_struct. Therefore, after the process exits, the shared memory will decrease the shared count, if it is 0, it is destroyed. Therefore, although POSIX's shared memory is created or opened with open, it is disabled with unlink instead of close, the function is to destroy it when the count is 0. Check the shared memory of System V. Although it is implemented by the file, the file is not added to the open file table of the process, it is used as an internal attribute of a process. Therefore, the file is not closed when the process exits, as long as the machine is not restarted or the shared memory is not explicitly destroyed, the shared memory will always exist. The process and file are two concepts that are not related to each other. Only files opened by the process are related to the process, at the end of the process, only files related to the process can be closed, while other files For example, the file structure used by the shared memory of sysv is not associated with the process, so it is not disabled. In fact, close does not operate, but just refreshes the buffer, and then decimal user count of the file:

Int filp_close (struct file * filp, fl_owner_t ID)

{

Int retval = 0;

If (! File_count (filp )){

Printk (kern_err "VFS: Close: file count is 0/N ");

Return 0;

}

If (filp-> f_op & filp-> f_op-> flush)

Retval = filp-> f_op-> flush (filp );

Dnotify_flush (filp, ID );

Locks_remove_posix (filp, ID );

Fput (filp); // The Lazy feature of Linux determines that this fput operation is the most important.

Return retval;

}

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.