您的位置:首页 > 编程语言

UC编程5-fcntl函数获取文件状态和加锁解锁/dup函数和文件映射

2014-05-02 14:40 344 查看
//myuc.h

#include<stdio.h>//io流
#include<stdlib.h>//标准库
#include<unistd.h>//uc标准头文件
#include<fcntl.h>//文件控制
#include<string.h>//c字符串
#include<sys/types.h>
#include<sys/mman.h>//内存映射
#include<sys/stat.h>//文件状态信息
#include<sys/wait.h>//进程等等
#include<dirent.h>//目录操作
#include<signal.h>//信号
#include<sys/time.h>//时间,计时器
#include<sys/ipc.h>//ipc进程间通信
#include<sys/shm.h>//共享内存段
#include<sys/msg.h>//消息队列


//dup5.c

#include "myuc.h"
int main()
{
	int fd=open("dup5",O_RDWR|O_CREAT|O_TRUNC,0777);
	printf("fd=%d\n",fd);
	int fd2=dup(fd);
	printf("fd2=%d\n",fd2);
	int fd3=dup2(fd,100);
	printf("fd3=%d\n",fd3);
	write(fd,"1",1);
	write(fd2,"2",1);
	close(fd);
	close(fd2);
	close(fd3);
	return 0;
}


//fcntl5.c

#include "myuc.h"
int main()
{
	int fd=open("txt",O_RDWR|O_CREAT|O_APPEND,0777);
	if(fd==-1) perror("open"),exit(-1);
	int fd2=fcntl(fd,F_DUPFD,5);//复制文件描述符
	int fd3=fcntl(fd,F_DUPFD,5);//复制,返回可用的>=参数的最小值
	printf("fd=%d,fd2=%d,fd3=%d\n",fd,fd2,fd3);
	int flags=fcntl(fd,F_GETFL);//获取权限,不包含创建标记

	printf("flags:%d\n",flags);
	
	printf("%d  %d\n",flags&O_RDWR,O_RDWR);
	if((flags&O_RDONLY)==O_RDWR) printf("读写权限\n");
	if((flags&O_RDONLY)==O_RDONLY) printf("读权限\n");
	if((flags&O_WRONLY)==O_WRONLY) printf("写权限\n");
	if((flags&O_APPEND)==O_APPEND) printf("追加\n");
	fcntl(fd,F_SETFL,O_WRONLY);//修改权限,只修改了O_APPEND权限
	printf("^^^^^^^^^^^^^^^^^^\n");
	flags=fcntl(fd,F_GETFL);//
	if((flags&O_APPEND)==O_APPEND) printf("追加\n");
	else printf("无追加属性!\n");

	close(fd);
	close(fd2);
	close(fd3);
	return 0;
}


//lock5.c

#include "myuc.h"
int main()
{
	int fd=open("lock5",O_RDWR|O_CREAT);
	if(fd==-1) perror("open"),exit(-1);
	struct flock lock;
	lock.l_type=F_WRLCK;
	lock.l_whence=SEEK_SET;
	lock.l_start=0;
	lock.l_len=lseek(fd,0,SEEK_END);
	lock.l_pid=-1;
	int res=fcntl(fd,F_SETLK,&lock);//加锁,返回-1代表失败
	if(res==-1) printf("加锁失败\n");
	else{
	   	printf("加锁成功\n");
		write(fd,"12345",5);
		lock.l_type=F_UNLCK;
		lock.l_pid=getpid();
		printf("pid=%d\n",lock.l_pid);
	    res=fcntl(fd,F_SETLK,&lock);//解锁,返回-1代表失败

	}
	sleep(50);
	close(fd);
	return 0;
}
//mmap5.c

/*
 mmap映射到文件,写文件和操作内存一样。
 */
#include "myuc.h"
typedef struct Emp{
	int id;
	char name[20];
	double sal;
}emp;
int main()
{
	int fd=open("mmap5.dat",
					O_RDWR|O_CREAT|O_TRUNC,0777);
	if(fd==-1) perror("open"),exit(-1);
	ftruncate(fd,sizeof(emp)*3);//大小不够,就会出错 
	emp * pe=mmap(NULL,sizeof(emp)*3,
					PROT_READ|PROT_WRITE,//小于等于fd的权限
					MAP_SHARED,//私有方式映射MAP_PRIVATE,不写入文件
					fd,0);
	if(pe==MAP_FAILED) perror("mmap"),exit(-1);
	pe[0].id=1;
	strcpy(pe[0].name,"zf");
	pe[0].sal=1500;
	pe[1].id=2;
	strcpy(pe[1].name,"gy");
	pe[1].sal=1200;
	pe[2].id=3;
	strcpy(pe[2].name,"lb");
	pe[2].sal=120000;
	int i;
	for(i=0;i<3;i++)
	{
		printf("%d,%s,%lf\n",pe[i].id,pe[i].name,pe[i].sal);
	}
	munmap(pe,sizeof(emp)*3);
	close(fd);
}


//stat5.c

/*
 获取文件状态和判断权限
 */
#include "myuc.h"
#include<time.h>
void test1()
{
	printf("^^^^test1^^^^\n");
	struct stat s={};
	int res=stat("a.txt",&s);
	if(res==-1) perror("stat"),exit(-1);

	printf("inode=%d\n",(int)s.st_ino);
	printf("size=%d\n",(int)s.st_size);
	printf("link=%d\n",s.st_nlink);
	printf("mdate=%s\n",ctime(&s.st_mtime));
	printf("mode=%o\n",s.st_mode);
	printf("access=%o\n",s.st_mode& 0777);
	
	if(S_ISREG(s.st_mode))
			printf("普通文件\n");
	else if(S_ISDIR(s.st_mode))
			printf("是目录\n");
	if(access("a.txt",R_OK)==0) printf("可读\n");
	if(access("a.txt",W_OK)==0) printf("可写\n");
	if(access("a.txt",X_OK)==0) printf("可执行\n");
	if(access("a.txt",F_OK)==0) printf("存在\n");
	
}
void test2()
{
	printf("^^^^test2^^^^\n\n");
	chmod("a.txt",0740);
	truncate("a.txt",100);
	struct stat s={};
	int res=stat("a.txt",&s);
	if(res==-1) perror("stat"),exit(-1);
	printf("access=%o\n",s.st_mode& 0777);
	printf("size=%d\n",(int)s.st_size);
	mode_t old=umask(0000);//修改屏蔽字,并保存旧的屏蔽字
	int fd=open("a1.txt",O_RDWR|O_CREAT,0777);//使用新的屏蔽字创建文件
	printf("旧屏蔽字:%o\n",old);
	umask(old);//恢复系统默认屏蔽字
	close(fd);
}
int main()
{
	test1();
	test2();
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: