1 原子操作
所谓原子操作,就是该操作绝不会在执行完毕前被任何其他任务或事件打断,也就说,它的最小的执行单位,不可能有比它更小的执行单位,因此这里的原子实际是使用了物理学里的物质微粒的概念。
参考:点击打开链接
2 内存映射函数
Linux提供了内存映射函数mmap, 它把文件内容映射到一段内存上(准确说是虚拟内存上), 通过对这段内存的读取和修改, 实现对文件的读取和修改, 先来看一下mmap的函数:
参考:点击打开链接
/********************************************************************************
** POXIX系统编程---IO操作
**
**----------FIleInof------------------------------------------------------------
** 文件信息:
** 创建日期:2014-9-29
** 修改日期:
** 文件信息:mmap()函数的用法,文件的大小决定写入的大小
********************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
int fd = open("hello",O_RDWR);
if(fd < 0){
perror("open error!\n");
exit(1);
}
/*求文件的长度*/
struct stat buf;
int st = stat("hello",&buf);
if(-1 == st){
perror("stat error!\n");
exit(1);
}
printf("st_size : %d\n",buf.st_size);
/*NULL-> 表示操作系统自己选择内存地址*/
void *ps = mmap(NULL, buf.st_size, PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
if(ps == (void *)-1){
perror("mmap error!\n");
exit(1);
}
strcpy(ps,"hello world"); /*字符串拷贝*/
printf("ps : %s\n",ps);
return 0;
}
3 设备的IO通道进行管理的函数 -->ioctl()函数 参考:点击打开链接
/********************************************************************************
** POXIX系统编程---IO操作
**
**----------FIleInof------------------------------------------------------------
** 文件信息:
** 创建日期:2014-9-29
** 修改日期:
** 文件信息:获得屏幕分辨率的信息
********************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
struct fb_var_screeninfo mys;
/*打开设备文件*/
int fd = open("/dev/fb0",O_RDWR);
if(fd < 0){
perror("open error!\n");
exit(1);
}
/*获取设备信息*/
int ioc = ioctl(fd,FBIOGET_VSCREENINFO,&mys);
if(-1 == ioc){
perror("ioctl error!\n");
exit(1);
}
/*信息打印*/
printf("xres = %d, yres = %d\n",mys.xres,mys.yres);
printf("bits_per_pixel = %d, yres = %d\n",mys.bits_per_pixel );
return 0;
}
4 文件锁 flock()函数 fcntl()函数 参考:点击打开链接 5 实现目录的删除
//实现目录的删除
include
#include
#include
#include
#include
#include
void rm_dir(char *,DIR *);
int main()
{
DIR *dir = opendir("kk");
if(dir == NULL){
perror("1 opendir\n");
exit(1);
}
char *path = "kk";
rm_dir(path,dir);
rmdir("kk");
return 0;
}
void rm_dir(char *path,DIR *dir)
{
char p_path[100] = {0};
strcpy(p_path,path);//kk/1/
printf("p : %s\n",p_path);
if(NULL == dir)
return;
while(1){//kk/1/
//p_path
struct dirent *rdir = readdir(dir);
if(NULL == rdir){
perror("read\n");
break;
}
if(strcmp(rdir->d_name ,".")==0 ||
strcmp(rdir->d_name , ".." )==0)
continue;
if(rdir->d_type == DT_REG){
char *tmp = (char *)malloc(100);
strcpy(tmp,p_path);
strcat(tmp,"/");
strcat(tmp,rdir->d_name);//kk/a || kk/1
remove(tmp);//kk/1/a
}
if(rdir->d_type == DT_DIR){
char *tmp = (char *)malloc(100);
strcpy(tmp,p_path);
strcat(tmp,"/");
strcat(tmp,rdir->d_name);//kk/a || kk/1
DIR *n_dir = opendir(tmp);
if(n_dir == NULL){
perror("2 opendir\n");
exit(1);
}
rm_dir(tmp,n_dir);//kk/1
}
}
rmdir(path);//kk/1/
}