前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POSIX系统编程之IO操作

POSIX系统编程之IO操作

作者头像
程序手艺人
发布2019-02-21 15:42:06
6900
发布2019-02-21 15:42:06
举报
文章被收录于专栏:程序手艺人程序手艺人

1 原子操作

    所谓原子操作,就是该操作绝不会在执行完毕前被任何其他任务或事件打断,也就说,它的最小的执行单位,不可能有比它更小的执行单位,因此这里的原子实际是使用了物理学里的物质微粒的概念。

    参考:点击打开链接

2 内存映射函数

    Linux提供了内存映射函数mmap, 它把文件内容映射到一段内存上(准确说是虚拟内存上), 通过对这段内存的读取和修改, 实现对文件的读取和修改, 先来看一下mmap的函数:

    参考:点击打开链接

代码语言:javascript
复制
/********************************************************************************
**          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()函数     参考:点击打开链接

代码语言:javascript
复制
/********************************************************************************
**          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 实现目录的删除

代码语言:javascript
复制
//实现目录的删除
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/
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014年10月05日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档