日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux进程同步问题,关于LINUX下进程和线程对文件的同步问题,请高手来看看!!!...

發(fā)布時間:2025/4/5 linux 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux进程同步问题,关于LINUX下进程和线程对文件的同步问题,请高手来看看!!!... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

不知道大家有沒有碰到過這樣的問題,即在LINUX下的程序有時候會碰到多個進程訪問同一個文件,

以及一個進程的不同線程對一個文件的同步問題,我嘗試用了系統(tǒng)提供的fcntl函數(shù)來給文件加鎖,

但是這只是一個進程互斥鎖,對于多線程的訪問,后面的操作會覆蓋前面的,看了stevens大爺?shù)?/p>

網(wǎng)絡(luò)編程的第9章中的my_lock和my_unlock的例子,還是不能很好的解決多線程同步的問題,

而他在11章利用信號燈對兩個函數(shù)的另一種實現(xiàn)在LINUX下不行,因為LINUX下不支持有名信號燈,

哪位大蝦能給提供一個比較好的解決方法,能給提供源代碼的(上面兩個函數(shù)的)我再加300分!

|

如果使用進程間同步,使用IPC(信號量(SYS-V)、消息隊列(SYS-V)、SOCKET)都行

如果使用線程同步,建議使用posix?信號量和消息隊列

這些都沒問題,如果不能實現(xiàn)文件互斥,那么最大可能是你設(shè)計的文件互斥邏輯可能存在問題

|

/*?recordio?*/

#include

#include

#include

#include

#include?"recordio.h"

int?initrecord(const?char*?pathname,?unsigned?int?reclen)

{

int?fd;

int?rs;

RECORDINFO?info;

if?(?(fd?=?open(pathname,?O_RDWR|O_CREAT,?0660))?==?-1?)?{

perror("Fail?to?open?file?for?initializing?record");

return?-1;

}

if?(?(rs?=?read(fd,?&info,?sizeof(RECORDINFO)))?==?-1?)?{

perror("Fail?to?read?record?info?for?initializing?record");

return?-1;

}

if?(?rs?==?0?)?{

info.reclen?=?reclen;

info.recnum?=?0;

if?(?write(fd,?&info,?sizeof(RECORDINFO))?==?-1?)?{

perror("Fail?to?write?record?info?for?initializing?record");

return?-1;

}

}

close(fd);

return?0;

}

int?addrecord(const?char*?pathname,?void*?buf,?unsigned?int?reclen,?unsigned?int?recnum)

{

int?fd;

int?rs;

struct?flock?lock;

lock.l_type?=?F_WRLCK;

lock.l_start?=?0;

lock.l_whence?=?SEEK_SET;

lock.l_len?=?0;

if?(?(fd?=?open(pathname,?O_WRONLY|O_APPEND))?==?-1?)?{

perror("Fail?to?open?file?for?adding?record");

return?-1;

}

if?(?fcntl(fd,?F_SETLKW,?&lock)?==?-1?)?{

perror("Fail?to?lock?file?for?adding?record");

return?-1;

}

lock.l_type?=?F_UNLCK;

rs?=?write(fd,?buf,?reclen?*?recnum);

if?(?fcntl(fd,?F_SETLKW,?&lock)?==?-1?)?{

perror("Fail?to?unlock?file?for?adding?record");

return?-1;

}

close(fd);

return?rs;

}

int?getrecord(const?char*?pathname,?void*?buf,?unsigned?int?reclen,?unsigned?int?recnum)

{

int?fd;

int?len;

RECORDINFO?info;

struct?flock?lock;

lock.l_type?=?F_WRLCK;

lock.l_start?=?0;

lock.l_whence?=?SEEK_SET;

lock.l_len?=?0;

if?(?(fd?=?open(pathname,?O_RDWR))?==?-1?)?{

perror("Fail?to?open?file?for?geting?record");

return?-1;

}

if?(?fcntl(fd,?F_SETLKW,?&lock)?==?-1?)?{

perror("Fail?to?lock?file?for?geting?record");

return?-1;

}

lock.l_type?=?F_UNLCK;

if?(?read(fd,?&info,?sizeof(RECORDINFO))?!=?sizeof(RECORDINFO)?)?{

perror("Fail?to?read?record?info");

return?-1;

}

if?(?info.reclen?!=?reclen?)?{

fprintf(stderr,?"Invalid?reclen?parameter?n");

return?-1;

}

if?(?lseek(fd,?sizeof(RECORDINFO)?+?info.reclen?*?info.recnum,?SEEK_SET)?==?-1?)?{

perror("Fail?to?seek?read?position");

return?-1;

}

if?(?(len?=?read(fd,?buf,?reclen?*?recnum))?==?-1?)?{

perror("Fail?to?get?record");

return?-1;

}

if?(?len?==?0?)?{

if?(?ftruncate(fd,?sizeof(RECORDINFO))?==?-1?)?{

perror("Fail?to?truncate?file");

return?-1;

}

info.recnum?=?0;

if?(?lseek(fd,?0,?SEEK_SET)?==?-1?)?{

perror("Fail?to?return?beginning");

return?-1;

}

if?(?write(fd,?&info,?sizeof(RECORDINFO))?==?-1?)?{

perror("Fail?to?update?record?info");

return?-1;

}

if?(?fcntl(fd,?F_SETLKW,?&lock)?==?-1?)?{

perror("Fail?to?unlock?file?for?geting?record");

return?-1;

}

close(fd);

return?0;

}?else?{

info.recnum?+=??len?/?info.reclen;

if?(?lseek(fd,?0,?SEEK_SET)?==?-1?)?{

perror("Fail?to?return?beginning");

return?-1;

}

if?(?write(fd,?&info,?sizeof(RECORDINFO))?==?-1?)?{

perror("Fail?to?update?record?info");

return?-1;

}

if?(?fcntl(fd,?F_SETLKW,?&lock)?==?-1?)?{

perror("Fail?to?unlock?file?for?geting?record");

return?-1;

}

close(fd);

return?len;

}

}

總結(jié)

以上是生活随笔為你收集整理的linux进程同步问题,关于LINUX下进程和线程对文件的同步问题,请高手来看看!!!...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。