我正在研究 linux 共享内存并偶然发现了这个ipcs命令。
从手册页:
ipcs - provide information on ipc facilities
Run Code Online (Sandbox Code Playgroud)
ipc手册页中没有解释,但它很可能代表进程间通信。从它列出的信息的上下文来看,这也是有意义的:共享内存段、消息队列和信号量数组。
我想知道,由于 linux/unix 中的所有内容都是一个“文件”,或者至少是一个类似文件的对象,那么 中列出的元素中的“文件”在ipcs哪里?
为什么创建的命名管道mkfifo未在 中列出ipcs?据我所知,fifos 是队列。创建的命名管道与创建mkfifo的消息队列ipcmk有何不同?
这里有几个问题:
这取决于。队列在虚拟文件系统中可见。从 mq_overview(7) :
Mounting the message queue file system
On Linux, message queues are created in a virtual file system. (Other implementations may also provide such a feature, but
the details are likely to differ.) This file system can be mounted (by the superuser) using the following commands:
# mkdir /dev/mqueue
# mount -t mqueue none /dev/mqueue
Run Code Online (Sandbox Code Playgroud)
共享内存 (shm_overview(7))
Accessing shared memory objects via the file system
On Linux, shared memory objects are created in a (tmpfs) virtual file system, normally mounted under /dev/shm. Since kernel
2.6.19, Linux supports the use of access control lists (ACLs) to control the permissions of objects in the virtual file sys-
tem.
Run Code Online (Sandbox Code Playgroud)
信号量 (sem_overview(7))
Accessing named semaphores via the file system
On Linux, named semaphores are created in a virtual file system, normally mounted under /dev/shm, with names of the form
sem.somename. (This is the reason that semaphore names are limited to NAME_MAX-4 rather than NAME_MAX characters.)
Since Linux 2.6.19, ACLs can be placed on files under this directory, to control object permissions on a per-user and per-
group basis.
Run Code Online (Sandbox Code Playgroud)
mkfifo未在 中列出ipcs?我不确定,所以我只给你我的意见,而不是一个答案。我的假设是,由于它们存在于实际的文件系统中,就像套接字一样,它们的管理方式与内核管理共享内存段和消息队列的方式不同。
管道和消息队列的主要区别在于管道只是两个进程之间通信的通道。它在字节级别起作用。你可以按照你想要的方式读写,你必须设计通信协议。它们是严格的 FIFO:在另一个字节之前写入的字节将始终在另一端之前读取。消息队列处理消息,而不是字节。通常,它们不是严格的 FIFO。这取决于实现,但它们可以支持消息之间的优先级机制。
在某种程度上,消息队列提供了更多功能,但如果您愿意,您可以使用消息队列实现 FIFO,反之亦然。