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

📄 p9.3.c

📁 linux编程源代码,其中是由大量的实例
💻 C
字号:
#include <stdio.h>#include <errno.h>#include <unistd.h>#include <fcntl.h>int main(int argc,char* argv[]){	int fd;	int count;	count=0;	struct flock lock;		if(argc!=2){		printf("Usage: %s lock_file_name\n",argv[0]);		return 1;	}		fd=open(argv[1],O_RDWR);	if(fd<0){		perror("cannot open the file");		return 1;	}	lock.l_type=F_WRLCK;	lock.l_whence=0;	lock.l_start=0;	lock.l_len=0;	while(fcntl(fd,F_SETLK,&lock)<0){		switch(errno){			case EAGAIN:			case EACCES:				if(++count<5)					sleep(1);				else{					fcntl(fd,F_GETLK,&lock);					printf("pid: %ld process find pid: %ld lock the file %s\n",(long)getpid(),(long)lock.l_pid,argv[1]);					return 1;				}				continue;		}		perror("function fcntl call fail");		return 1;	}		printf("pid: %ld process lock the file\n",(long)getpid());	sleep(1);	printf("pid: %ld process release the file\n",(long)getpid());	return 0;}

⌨️ 快捷键说明

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