利用创建临时文件实现共享存储锁

原创 2012年03月21日 22:35:07

实现的是读写锁,很垃圾,但是也保留着,以后鄙视自己用。

#include "bson_rw.h"

//return true if count file exist,
bool CheckReadLock(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+13);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock.count");

	FILE *fp = fopen(tmpname, "r");
	if(fp == NULL)
	{
		return false;
	}
	fclose(fp);
	return true;
}
int FetchReadLockNum(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+13);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock.count");

	FILE *fp = fopen(tmpname, "r");
	if(fp == NULL)
	{
		free(tmpname);
		return 0;
	}

	int num = 0;
	char c;
	while((c = fgetc(fp)) != EOF)
	{
		num++;
	}
	fclose(fp);
	
	return num;
}
void UlinkFile(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+13);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock.count");

	unlink(tmpname);
}
void AddReadLockCount(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+13);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock.count");

	FILE *fp = fopen(tmpname,"a");
	char c = 'a';
	fprintf(fp, "%c",c);
	fclose(fp);
	free(tmpname);
}
void DelReadLockCount(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+13);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock.count");

	FILE *fp = fopen(tmpname, "r");
	if(fp == NULL)
	{
		free(tmpname);
		return ;
	}

	int num = 0;
	char c;
	while((c = fgetc(fp)) != EOF)
	{
		num++;
	}
	fclose(fp);

	if(num == 0)
	{
		unlink(tmpname);
	}
	else
	{
		FILE *fp_w = fopen(tmpname,"w");
		char *tmpstr = (char*)malloc(num);
		memset(tmpstr,'a',num);
		*(tmpstr+num-1) = '\0';
		fprintf(fp_w,"%s",tmpstr);
		fclose(fp_w);
	}
	free(tmpname);
}

#ifdef WIN32

HANDLE lock_file_w(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".wlock");

	HANDLE handle = CreateFileA(tmpname,
		0,
		FILE_SHARE_READ,
		NULL,
		CREATE_NEW ,
		FILE_ATTRIBUTE_SYSTEM ,
		NULL);

	if(handle == INVALID_HANDLE_VALUE)
	{
		free(tmpname);
		return INVALID_HANDLE_VALUE;
	}
	free(tmpname);
	return handle;
}

void unlock_file_w(const char *filename, HANDLE handle)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".wlock");

	CloseHandle(handle);
	unlink(tmpname);
	free(tmpname);
}

int lock_test_w(const char *name)
{
	assert(name != NULL);
	int len = strlen(name);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,name);
	strcat(tmpname,".wlock");

	HANDLE handle = CreateFileA(tmpname,
		0,
		FILE_SHARE_READ,
		NULL,
		CREATE_NEW ,
		FILE_ATTRIBUTE_SYSTEM ,
		NULL);

	if(handle == INVALID_HANDLE_VALUE)
	{
		free(tmpname);
		CloseHandle(handle);
		return -FILE_ERR_ALREADY_LOCK;
	}

	CloseHandle(handle);
	unlink(tmpname);
	free(tmpname);
	return 0;
}

HANDLE lock_file_r(const char *filename)
{
	assert(filename != NULL);
	AddReadLockCount(filename);

	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock");
	
	HANDLE handle = CreateFileA(tmpname,
		0,
		FILE_SHARE_READ,
		NULL,


		CREATE_NEW ,
		FILE_ATTRIBUTE_SYSTEM ,
		NULL);

	if(handle == INVALID_HANDLE_VALUE)
	{
		free(tmpname);
		return INVALID_HANDLE_VALUE;
	}
	free(tmpname);
	return handle;
}

void unlock_file_r(const char *filename, HANDLE handle)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock");

	CloseHandle(handle);
	unlink(tmpname);
	free(tmpname);
}

int lock_test_r(const char *name)
{
	assert(name != NULL);
	int len = strlen(name);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,name);
	strcat(tmpname,".rlock");

	HANDLE handle = CreateFileA(tmpname,
		0,
		FILE_SHARE_READ,
		NULL,
		CREATE_NEW ,
		FILE_ATTRIBUTE_SYSTEM ,
		NULL);

	if(handle == INVALID_HANDLE_VALUE)
	{
		free(tmpname);
		CloseHandle(handle);
		return -FILE_ERR_ALREADY_LOCK;
	}

	CloseHandle(handle);
	unlink(tmpname);
	free(tmpname);
	return 0;
}

#else

int lock_file_w(const char *filename)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".wlock");

	int fd = open(tmpname, O_CREAT | O_EXCL);
	if(fd == -1)
	{
		free(tmpname);
		return -1;
	}
	free(tmpname);
	return fd;	
}

void unlock_file_w(const char *filename, int fd)
{
	assert(filename != NULL);
	
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".wlock");

	close(fd);
	unlink(tmpname);
	free(tmpname);
}

int lock_test_w(const char *name)
{
	assert(name != NULL);
	int len = strlen(name);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,name);
	strcat(tmpname,".wlock");

	int fd = open(tmpname,  O_CREAT | O_EXCL);
	if(fd == -1)
	{
		close(fd);
		free(tmpname);
		return FILE_ERR_ALREADY_LOCK;
	}
	
	close(fd);
	unlink(tmpname);
	free(tmpname);
	return 0;
}

int lock_file_r(const char *filename)
{
	assert(filename != NULL);
	AddReadLockCount(filename);

	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock");

	int fd = open(tmpname, O_CREAT | O_EXCL);
	if(fd == -1)
	{
		free(tmpname);
		return -1;
	}
	free(tmpname);
	return fd;	
}
void unlock_file_r(const char *filename, int fd)
{
	assert(filename != NULL);
	int len = strlen(filename);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,filename);
	strcat(tmpname,".rlock");

	close(fd);
	unlink(tmpname);
	free(tmpname);
}
int lock_test_r(const char *name)
{
	assert(name != NULL);
	int len = strlen(name);
	char *tmpname = (char*) malloc(len+7);
	strcpy(tmpname,name);
	strcat(tmpname,".rlock");

	int fd = open(tmpname,  O_CREAT | O_EXCL);
	if(fd == -1)
	{
		close(fd);
		free(tmpname);
		return FILE_ERR_ALREADY_LOCK;
	}

	close(fd);
	unlink(tmpname);
	free(tmpname);
	return 0;
}

#endif


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

hadoop mr的一些文件归属(包括临时文件的存储情况)

一、概述 一个计算的流程如下图所示,对于一个简单的wordcount的计算中,总共要经历哪些文件呢?本文将详细探讨这个话题。文章可能会重新编辑,如果想浏览最新内容请访问原创博客:http://blo...

vc创建临时文件的例子

  • 2013-02-28 16:19
  • 11KB
  • 下载

VC++创建临时文件实例

  • 2012-08-23 09:08
  • 12KB
  • 下载

shell创建临时文件

再学shell编程,创建临时文件,创建临时目录。将信息输出的同时写入到指定文件中。到目前为止,您已经学习了两种显示脚本输出的方法::1》在监视器屏幕上显示输出。2》将输出重定向到文件标准文件描述符。L...

创建临时文件

linux下创建临时文件

前述: linux下删除一个文件需要满足两个条件: 1.文件的链接计数为0 2.没有进程打开该文件 我们即使使用rm命令删除了文件,实际上如果有进程打开了该文件,该文件的内容实际上没有...

创建临时文件 tmpnam tmpfile tempnam mkstemp unlink

推荐使用 tmpfile mkstemp #include #include #include #include int main(void) { FILE *pfile,...

Linux下创建临时文件

已移到:http://www.wypblog.com/archives/172        临时文件是一个暂时用来存储数据的文件。如果使用建立普通文件的方法来创建文件,则可能遇到文件是否存在,是否有...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)