一種方法是使用檔案鎖,利用fcntl函式對於檔案進行加鎖解鎖操作,以達到互斥訪問的目的,但是在控制時需要注意程序之間的關係,在有多個程序和共享資源時,應注意死鎖的避免。
上乙個最開始寫的程式吧,簡單,就是兩個程序互斥訪問檔案。
#include
#include
#include
#include
void readfile(file*fd);
void writefile(file*fd);
int lock_file(int fd,int cmd,int type,off_t offset,int whence,off_t len);
using namespace std;
int main()
else
fclose(fd);
}void fileop1(file* fd)
cout<<"fileop1 locked" if (lock_file(fileno(fd),f_setlk,f_unlck,0,seek_set,0)<0) }void fileop2(file* fd) cout<<"fileop2 locked" if (lock_file(fileno(fd),f_setlk,f_unlck,0,seek_set,0)<0) }int lock_file(int fd,int cmd,int type,off_t offset,int whence,off_t len) usr bin python coding utf 8 from multiprocessing import process,array,value deff n,a n.value 3.1415926 for i in range 5 a i a i if name main num value... 不用程序池的多程序的資料共享from multiprocessing import process,queue queue queue def func a,q re f 我是子程序 我放入data q.put re if name main ls for i in range 10 p proce... 使用manager在程序之間事項共享資料.栗子 主程序呼叫manager,建立乙個字典d和乙個列表l,啟動十個子程序,每個子程序都向d和l中放資料 輸出結果 d 7 python python37 python.exe d 7 python s14 其他 aaaa.py 0,1,2,3,4,1012...多程序共享 同步
多程序資料共享
多程序manager共享