最開始打算用semaphore實現,
sem_t waitNonEmpty;
int empty=0;
sem_init(&waitNonEmpty, 1, empty); /*初始化信號量*/
sem_wait(&waitNonEmpty); /*是否有"非空資源"*/
sem_post(&waitNonEmpty); /*生成一個"非滿資源"*/
sem_destroy(&waitNonEmpty);
后來在fc3 redhat7.3下man了sem_init, 第二個參數int pshared
The pshared
argument indicates whether the semaphore is local to the current pro-
cess ( pshared is zero) or is to be shared between several processes (
pshared is not zero). LinuxThreads currently does not support process-
shared semaphores, thus sem_init always returns with error ENOSYS if
pshared is not zero.
最邪惡的現狀出來了, 這倆系統沒有實現sem的進程間通信, 如果設置not zero, 會返回ENOSYS的錯誤...
后來找了個redhat Enterprise 4 看了下文檔, 貌似又支持了....
做個記號. 一會兒看看flock
fcntl 是唯一符合posix的文件鎖
結構體flock的指針
:
struct flcok {
short int l_type; /* 鎖定的狀態*/
//這三個參數用于分段對文件加鎖,若對整個文件加鎖,則:l_whence=SEEK_SET,l_start=0,l_len=0;
short int l_whence;/*決定l_start位置*/
off_t l_start; /*鎖定區域的開頭位置*/
off_t l_len; /*鎖定區域的大小*/
pid_t l_pid; /*鎖定動作的進程*/
};
l_type 有三種狀態:
F_RDLCK 建立一個供讀取用的鎖定
F_WRLCK 建立一個供寫入用的鎖定
F_UNLCK 刪除之前建立的鎖定
int fcntl(int fd, int cmd, struct flock *lock);
int cmd
//F_SETLKW對加鎖操作進行阻塞,
//F_SETLK不對加鎖操作進行阻塞,立即返回
當使用F_SETLK宏,不進行阻塞加鎖操作時。兩個進程有對文件加鎖失敗的情況出現。
當使用F_SETLKW宏,進行阻塞加鎖操作時。兩個進程沒有文件加鎖失敗的情況。但是根據進程的輸出日志,可以明顯看出A進程對文件操作時,B進程停止對文件操作。
測試過程中打開文件使用O_RDWR | O_CREAT | O_APPEND模式,并對文件加寫鎖。鎖對打開文件沒有影響,但是對另一進程加鎖和寫操作有影響。
加鎖對同一進程中的多線程無效。同一進程中的多線程對同一文件加鎖,后加鎖的線程會覆蓋前一個線程加的文件鎖。
fcntl文檔中寫到,一個進程對一個文件的區域只能加一個類型的鎖
實現代碼如下:
//filelock.h
#ifndef __FILE_LOCK_HPP__
#define __FILE_LOCK_HPP__
#ifdef __cplusplus
extern "C" {
#endif
int file_lock(int fd);
int file_unlock(int fd);
#ifdef __cplusplus
}
#endif
#endif //__FILE_LOCK_HPP__
------------------------華麗分割線-----------------------------
//filelock.cpp
#include <fcntl.h>
#include <unistd.h>
#include "filelock.h";
int file_lock(int fd){
struct flock s_flock;
s_flock.l_type = F_WRLCK;
s_flock.l_whence = SEEK_SET;
s_flock.l_start = 0;
s_flock.l_len = 0;
s_flock.l_pid = getpid();
//F_SETLKW對加鎖操作進行阻塞,
//F_SETLK不對加鎖操作進行阻塞,立即返回
return fcntl(fd, F_SETLKW, &s_flock);
}
int file_unlock(int fd){
return fcntl(fd, F_SETLKW, F_UNLCK);
}
---------------------------------華麗的分割線--------------------------
test.cpp
#include "filelock.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <errno.h>
int main(int argc,char *argv[]) {
int pid = -1;
int i = 5;
while(i--){
if ((pid = fork()) < 0) { //fork出錯
puts("fork1 error");
} else if (pid > 0) {//父進程
sleep(5);
if (waitpid(pid, NULL, 0) < 0)
puts("waitpid error");
} else {//子進程
sleep(1);
int li_file = 0;
int li_lck_st = -1;
li_file = open("tt.txt", O_WRONLY|O_CREAT, 0777);
if( li_file < 0 ) {
printf("file open error\n");
}
printf("li_file=[%d] pid=[%d]\n", li_file , getpid() );
li_lck_st = file_lock(li_file);
sleep(5);
printf("li_lck_st=%d pid =%d\n", li_lck_st, getpid() );
file_unlock(li_file);
close(li_file);
printf("close file pid=%d unlock\n", getpid());
return 0;
}
}
return 0;
}
------------------------------華麗分割線---------------------------
//makefile
#########################################################
#Project: test
#By: Khan.Lau
#Description: test
#########################################################
CC = gcc
CXX = g++
STRIP = strip
AR = ar
PROJ = $(shell pwd)
DLLPATH = $(shell find $(PROJ) -name *.so*)
LIBPATH = /usr
INCS = -I$(LIBPATH)/include \
-I$(PROJ)/src/inc \
-I$(PROJ)/src
CXXINCS = -I$(LIBPATH)/include \
-I$(LIBPATH)/include/c++/4.3 \
-I$(LIBPATH)/include/c++/4.3/backward \
-I$(PROJ)/src/inc \
-I$(PROJ)/src
CFLAGS = -g -Wall $(CXXINCS)
#
CONFIGURATION = release
#
BIN = test.exe
#LIB = lib/HuaWeiBsc.a
#包路徑
LIBS = -L$(PROJ)/lib \
-L$(LIBPATH)/lib -lz
SRCS = $(wildcard \
src/lib/*.cpp \
src/kernel/*.cpp)
OBJBINS = $(SRCS:.cpp=.o)
.c.o:
$(CC) -o $@ -c $< $(CFLAGS)
.cpp.o:
$(CXX) -o $@ -c $< $(CFLAGS)
COLORH=echo -en "\033[1;32;40m"
COLORN=echo -en "\033[m"
all : $(BIN)
$(BIN) : $(OBJBINS)
$(CXX) -o $(CONFIGURATION)/$(BIN) $(OBJBINS) $(LIBS)
@$(COLORN)
$(shell echo "$(BIN) build complete.")
#$(LIB) : $(OBJBINS)
# ar rc $(LIB) $(OBJBINS)
clean :
rm -f $(OBJBINS)
rm -f $(BIN)
-----------------------------華麗分割線--------------------------
測試環境 ubuntu 8.10, redhat 7.3, redhat fc3 redhat enterprise 4.6
結果:
khan@khan-laptop:~/project/filelock/release$ ./test.exe
li_file=[3] pid=[10959]
li_lck_st=0 pid =10959
close file pid=10959 unlock
li_file=[3] pid=[10961]
li_lck_st=0 pid =10961
close file pid=10961 unlock
li_file=[3] pid=[10964]
li_lck_st=0 pid =10964
close file pid=10964 unlock
li_file=[3] pid=[10966]
li_lck_st=0 pid =10966
close file pid=10966 unlock
li_file=[3] pid=[10969]
li_lck_st=0 pid =10969
close file pid=10969 unlock