當我在Linux的某些任意位置回顯文件時,即echo > /tmp/file
,某些正在運行的進程會響應。這個IPC是通過文件管道嗎?通過文件的進程間通信
這是否意味着一個正在運行的進程總是打開要讀取的文件?但是,如何編寫文件,因爲文件流被自己的進程鎖定了?
當我在Linux的某些任意位置回顯文件時,即echo > /tmp/file
,某些正在運行的進程會響應。這個IPC是通過文件管道嗎?通過文件的進程間通信
這是否意味着一個正在運行的進程總是打開要讀取的文件?但是,如何編寫文件,因爲文件流被自己的進程鎖定了?
如果你想使用一個文件與另一個進程通信,你應該看看man fifo
。
我彙報這裏只是第一行:
NAME
fifo - first-in first-out special file, named pipe
DESCRIPTION
A FIFO special file (a named pipe) is similar to a pipe, except that it
is accessed as part of the file system. It can be opened by multiple
processes for reading or writing. When processes are exchanging data
via the FIFO, the kernel passes all data internally without writing it
to the file system. Thus, the FIFO special file has no contents on the
file system; the file system entry merely serves as a reference point
so that processes can access the pipe using a name in the file system.
我覺得這是你所需要的。
只是認爲它是一個緩衝區。必須打開閱讀和寫作的過程。正在讀取的進程將被阻塞,直到寫入進程沒有寫入。當寫入過程完成寫入時,關閉文件,這是讀取過程的綠燈開始清空緩衝區。這是一個FIFO,因此寫入的第一行將成爲第一行。然後寫入過程可以再次打開它們,然後重新開始。
您可以使用mkfifo
創建一個FIFO。看看man mkfifo
。
一些正在運行的進程響應 - 哪些和如何?正在運行的進程總是打開文件 - 哪個文件?文件流被鎖定 - 誰說的? –
我的意思是運行一般的進程。如果我打開一個文件,可能會被鎖定。例如,打開的文件不可能被更改,因爲其他一些進程正在使用它。普通計算機用戶如果試圖刪除文件或修改文件,他們會一直收到此消息。 – Amumu