随笔 - 171  文章 - 257  trackbacks - 0
<2006年1月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(33)

随笔分类(225)

随笔档案(171)

相册

技术

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 440551
  • 排名 - 48

最新随笔

最新评论

阅读排行榜

评论排行榜

   最开始打算用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

posted on 2008-12-24 10:45 Khan 阅读(8176) 评论(3)  编辑 收藏 引用 所属分类: GCC/G++跨平台开发

FeedBack:
# re: Linux 进程互斥锁 2008-12-24 17:32 lymons
虽然没有仔细看你写的代码,但从设计思路上也能看出一些端倪出来。

flock实现的文件锁实际上是一个建议锁。也就是意味着在这个锁很容易
收到外界的干扰。如果用于锁定的文件被某人/某进程删除了,那使用
这个文件锁的程序就很容易出现紊乱。这点也需要考虑。

从你man出来的信息来看
LinuxThreads currently does not support process-shared semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero.
这说的应该指的是LinuxThread吧,指的不是进程。
sema设计的初衷就是为了让进程间进行通信,信号量如果不能共享那也就不能进行通信是吧。所以,我认为你应该放心的去用sema。至少是目前版本的大多数的linux系统都没有问题。这仅仅是俺一家之言,如有不对之处请斧正。  回复  更多评论
  
# re: Linux 进程互斥锁 2008-12-24 17:50 Khan.Lau
我这里没有使用flock 直接使用了fcntl, 貌似flock也是用fcntl来实现的.
我猜测 LinuxThreads currently does not support process-shared semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero.这句可能说LinuxThreads 这部分负责实现semaphores, 但是当前的版本没有实现进程间共享信号灯的部分,

实际我在我系统里面测试, 确实是没有返回ENOSYS, 但是sem_wait在semaphore大于0的情况下这里根本就没有阻塞
  回复  更多评论
  
# re: Linux 进程互斥锁 2008-12-25 10:43 lymons
flock就是用fcntl系统调用实现的。
因为在linux内核里fcntl就是按照advise模式来实现,所以他一直是一个建议锁。

我建议你可以使用sys v的信号量,在Linux下这毕竟比posix的更常用一些。
如,使用semget(2)来创建信号量,利用semop(2)来做PV操作。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理