linux 进程锁创建,Linux 进程互斥锁

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

#include

#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

#include

#include

#include

#include

#include

#include

#include

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值