2011-07-17 16 views
0

這裏分享的話:文件系統通過multiprecoesses與多線程

I am talking about general linux concurrent programming environment 

Definition: 
    Node: a machine with a processor. 
    file system: can be accessed both locally and remotely. 
it includes large set of files varied in random size.  

Node I: Process A with multithreads access A's file system, operations include 
read and write. Process B, similar to A. Think about more similar processes 
C,D,etc. 

Then, thinking about scaling. The same FS system is located on a separate node. 
Operated by processes E,F,G etc on node II, and processes A,B,C,D on Node I. 
thinking about similar node III,IV,V, etc. 

這既是一種實用的面試問題。這裏是我的解決方案:

I can use mutex and signal resolve multi reader and writer of the same 
file within a process. And also using IPC resolve multiprocesses 
communication and synchronization. 
the code could work very well for single node multiprocesses. 

But, when dealing with multi node. We need similar but more 
complicated mechanism to detect are there any node 
writting on the FS, if yes, wait; otherwise, access 
writting mutex and write, then notify waiting guys. 

經過思考,如下是我的想法:

From the point of a NFS, we define file lock of course based on file. 
My target is: 
at each moment,there is only one writer write the file, 
there can be more than one reader read the file. 
Then, all the processes on different nodes are the same. 
they should have their own mechanism to acquire either read or write lock, 
of course, dealing with connection, failures and retries. 

我想知道是否有一些原型爲這樣那樣的問題呢?

回答

1

我假設「節點」是指「網絡節點」,即運行其自己的操作系統副本的實體。它可能是一臺真正的機器或虛擬機。

老實說,這個問題措辭很糟糕。不清楚被問到的是什麼;我可以假設他們沒有詢問節點間的同步或鎖定。

所以你很擅長第一部分:線程之間的互斥體,同一機器上進程之間的IPC信號量。

如果要處理單獨節點之間的交互,首先需要具有網絡文件系統,例如NFS或CIFS。其次,您需要文件鎖(或鎖文件)來管理對共享文件的訪問。文件鎖也可以用於其他級別,線程間和進程間,儘管它們不像互斥和信號量那麼簡單。

您也可以從套接字構建一個同步系統,但這需要每個節點都有一個到其他節點的套接字,這意味着N^2套接字可能存在競爭狀況,或者中央交換所節點成爲單個節點失敗點。

+0

我認爲節點間同步可能是多個節點訪問共享NFS的一個解決方案。同樣,如果自上次讀取後沒有更新,則一個消費節點可能會暫停讀取。另一位消費者在更新文件後可能會恢復此節點。我認爲解決方案有點複雜。 – SecureFish

+0

如果您有生產者和消費者進行通信,您可能需要查看某種形式的[消息傳遞接口(MPI)](http://en.wikipedia.org/wiki/Message_Passing_Interface)(例如[MPICH](http: //www.mcs.anl.gov/mpi/))。 –