ipcs 的「檔案」在哪裡以及為什麼命名管道 (mkfifo) 未在 ipcs 中列出

ipcs 的「檔案」在哪裡以及為什麼命名管道 (mkfifo) 未在 ipcs 中列出

我正在研究 linux 共享記憶體並偶然發現了這個ipcs命令。

從手冊頁:

ipcs - provide information on ipc facilities

ipc手冊頁中沒有解釋,但它很可能代表進程間通訊。從它列出的資訊的上下文來看,這也是有意義的:共享記憶體段、訊息佇列和信號量數組。

我想知道,由於 linux/unix 中的所有內容都是一個“文件”,或者至少是一個類似文件的對象,那麼 中列出的元素中的“文件”在哪裡ipcs

為什麼創建的命名管道mkfifo沒有在 中列出ipcs?據我了解,fifo 是隊列。建立的命名管道mkfifo與建立的訊息佇列有何不同ipcmk

答案1

這裡有幾個問題:

  • ipcs 中列出的元素的檔案在哪裡?

這取決於。隊列在虛擬檔案系統中是可見的。來自 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

共享記憶體(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.

信號量(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.
  • 為什麼創建的命名管道mkfifo沒有在 中列出ipcs

我不太確定,所以我只能給你我的意見,而不是答案。我的假設是,由於它們存在於實際檔案系統中,例如套接字,因此它們的管理方式與核心管理共享記憶體段和訊息佇列的方式不同。

  • mkfifo 所建立的命名管道與 ipcmk 所建立的訊息佇列有何不同?

管道和訊息佇列之間的主要區別在於管道只是兩個進程之間的通訊通道。它在位元組級別起作用。你可以按照你想要的方式讀寫,並且你必須設計通訊協定。它們是嚴格的 FIFO:在另一個位元組之前寫入的位元組總是會在另一端先被讀取。訊息佇列處理的是訊息,而不是位元組。通常,它們是不太嚴格的 FIFO。這取決於實現,但它們可以支援訊息之間的優先機制。

在某種程度上,訊息佇列提供了更多功能,但如果您願意,您可以使用訊息佇列實作 FIFO,反之亦然。

答案2

ipcs讓您看到稱為「System V IPC」的進程間通訊方法。 System V IPC 目前被廣泛忽視,但是過去明顯不喜歡。顯然在早期,不同的團體會實施他們需要的東西,並且有人需要訊息佇列、共享記憶體和信號量。

這些 IPC 方法受到廣泛批評,因為它們不是很 Unixy,不是一個“文件”,這與您所質疑的一樣。

我沒有解釋為什麼命名管道和訊息隊列沒有集成,但我敢打賭它的起源是相同的:一組想要命名管道,所以他們就去做了。

相關內容