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

📄 lock.c

📁 该代码为unix环境高级编程的源代码
💻 C
字号:
#include	"calld.h"typedef struct {  char	*line;	/* points to malloc()ed area */  				/* we lock by line (device name) */  pid_t	pid;	/* but unlock by process ID */				/* pid of 0 means available */} Lock;static Lock	*lock = NULL;	/* the malloc'ed/realloc'ed array */static int	 lock_size;		/* #entries in lock[] */static int	 nlocks;		/* #entries currently used in lock[] *//* Find the entry in lock[] for the specified device (line). * If we don't find it, create a new entry at the end of the * lock[] array for the new device.  This is how all the possible * devices get added to the lock[] array over time. */static Lock *find_line(char *line){	int		i;	Lock	*lptr;	for (i = 0; i < nlocks; i++) {		if (strcmp(line, lock[i].line) == 0)			return(&lock[i]);	/* found entry for device */	}	/* Entry not found.  This device has never been locked before.	   Add a new entry to lock[] array. */	if (nlocks >= lock_size) {	/* lock[] array is full */		if (lock == NULL)		/* first time through */			lock = malloc(NALLOC * sizeof(Lock));		else			lock = realloc(lock, (lock_size + NALLOC) * sizeof(Lock));		if (lock == NULL)			err_sys("can't alloc for lock array");		lock_size += NALLOC;	}	lptr = &lock[nlocks++];	if ( (lptr->line = malloc(strlen(line) + 1)) == NULL)		log_sys("calloc error");	strcpy(lptr->line, line);	/* copy caller's line name */	lptr->pid  = 0;	return(lptr);}voidlock_set(char *line, pid_t pid){	Lock	*lptr;	log_msg("locking %s for pid %d", line, pid);	lptr = find_line(line);	lptr->pid  = pid;}voidlock_rel(pid_t pid){	Lock	*lptr;	for (lptr = &lock[0]; lptr < &lock[nlocks]; lptr++) {		if (lptr->pid == pid) {			log_msg("unlocking %s for pid %d", lptr->line, pid);			lptr->pid  = 0;			return;		}	}	log_msg("can't find lock for pid = %d", pid);}pid_tis_locked(char *line){	return( find_line(line)->pid );	/* nonzero pid means locked */}

⌨️ 快捷键说明

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