⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lock.c

📁 操作系统源代码
💻 C
字号:
#include <lib.h>#include <errno.h>#include <sys/types.h>#include <unistd.h>#include <string.h>#include <fcntl.h>#include <stdio.h>#if _ANSI#include <stdlib.h>#endiftypedef enum {  False, True} BOOLEAN;#define LOCKDIR "/tmp/"		/* or /usr/tmp/ as the case may be */#define MAXTRIES 3#define NAPTIME (unsigned int)5PRIVATE _PROTOTYPE( char *lockpath, (char *name));_PROTOTYPE( void syserr, (char *errstring));_PROTOTYPE( BOOLEAN lock, (char *name));_PROTOTYPE( void unlock, (char *name));voidsyserr(errstring)char *errstring;{	fprintf(stderr,"couldn't %s\n", errstring);	exit(1);}BOOLEAN lock(name)		/* acquire lock */char *name;{  char *path;  int fd, tries;  path = lockpath(name);  tries = 0;  while ((fd = creat(path, 0)) == -1 && errno == EACCES) {	if (++tries >= MAXTRIES) return(False);	sleep(NAPTIME);  }  if (fd == -1 || close(fd) == -1) syserr("lock");  return(True);}void unlock(name)		/* free lock */char *name;{  if (unlink(lockpath(name)) == -1) syserr("unlock");}PRIVATE char *lockpath(name)	/* generate lock file path */char *name;{  PRIVATE char path[20];  strcpy(path, LOCKDIR);  return(strcat(path, name));}

⌨️ 快捷键说明

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