locklink.c

来自「unix网络编程的源码」· C语言 代码 · 共 29 行

C
29
字号
#include	"unpipc.h"

#define	LOCKFILE	"/tmp/seqno.lock"
#define	PREFIX		"/tmp"		/* prefix for temp pathname */

void
my_lock(int fd)
{
	char	*ptr;

	ptr = tempnam(PREFIX, NULL);
		/* create/open and then close */
	Close(Open(ptr, O_CREAT | O_RDWR | O_TRUNC, FILE_MODE));

	while (link(ptr, LOCKFILE) < 0) {
		if (errno != EEXIST)
			err_sys("link error for lock file");
		/* someone else has the lock, loop around and try again */
	}
	Unlink(ptr);			/* linked the file, we have the lock */
	free(ptr);				/* tempnam() calls malloc() */
}

void
my_unlock(int fd)
{
	Unlink(LOCKFILE);		/* release lock by removing file */
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?