lockopen2.c

来自「《UNIX进程间通信》中的代码」· C语言 代码 · 共 24 行

C
24
字号
#include	"unpipc.h"#define	LOCKFILE	"/tmp/seqno.lock"voidmy_lock(int fd){	int		tempfd;		/* create with O_TRUNC and no permission bits */	while ( (tempfd = open(LOCKFILE, O_CREAT | O_WRONLY | O_TRUNC, 0)) < 0) {		if (errno != EACCES)			err_sys("open error for lock file");		/* someone else has the lock, loop around and try again */	}	Close(tempfd);			/* opened the file, we have the lock */}voidmy_unlock(int fd){	Unlink(LOCKFILE);		/* release lock by removing file */}

⌨️ 快捷键说明

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