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

📄 file_tc.c

📁 unix linux 编程实践源代码
💻 C
字号:
/* file_tc.c - read the current date/time from a file  *     usage: file_tc filename  *      uses: fcntl()-based locking  */#include <stdio.h>#include <sys/file.h>#include <fcntl.h>#define  oops(m,x)  { perror(m); exit(x); }#define  BUFLEN 10main(int ac, char *av[]){	int	fd, nread;	char	buf[BUFLEN];	if ( ac != 2 ){		fprintf(stderr,"usage: file_tc filename\n");		exit(1);	}	if ( (fd= open(av[1],O_RDONLY)) == -1 )		oops(av[1],3);	lock_operation(fd, F_RDLCK);	  while( (nread = read(fd, buf, BUFLEN)) > 0 )		write(1, buf, nread );	lock_operation(fd, F_UNLCK);	close(fd);}lock_operation(int fd, int op){	struct flock lock;	lock.l_whence = SEEK_SET;	lock.l_start = lock.l_len = 0;	lock.l_pid = getpid();	lock.l_type = op;	if ( fcntl(fd, F_SETLKW, &lock) == -1 )		oops("lock operation", 6);}

⌨️ 快捷键说明

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