zoukankan      html  css  js  c++  java
  • linux 高级编程

    一、IO与文件目录管理

    1.pread与lseek+read的区别

       pread读取后不会改变读写位置

    2.mmap映射

        /proc/${pid}/mem 无法映射,并且 mmap函数最后一个参数 文件中的映射开始位置 必须是pagesize的证书倍,否者出错

    3.IO的实际用户(real user)与有效用户(effective user)

        默认情况:有效用户与实际用户一致,但设置了设置位,就可能不一样了  

       实际用户:执行的用户

       有效用户:权限用户

    获取有效用户与实际用户

            uid_t getuid(void);      //实际用户        uid_t geteuid(void);    //有效用户

    4.目录相关的函数

         int chdir(const char *path);                                        //切换目录

         int mkdir(const char *pathname, mode_t mode);//创建目录

         int rmdir(const char *pathname);                            //删除目录

         int unlink(const char *pathname);                           //删除文件

         mode_t umask(mode_t mask);                               //设置文件权限屏蔽位

         int stat(const char *path, struct stat *buf);       int fstat(int fd, struct stat *buf);                                 //获取文件目录状态

    5.目录的便利

    DIR *opendir(const char *name);  //打开文件目录,DIR为指向文件目录的指针

    struct dirent *readdir(DIR *dirp);    //读取文件目录

    struct dirent {                ino_t          d_ino;       /* inode number */                off_t          d_off;       /* offset to the next dirent */                unsigned short d_reclen;    /* length of this record */                unsigned char  d_type;      /* type of file; not supported                                               by all file system types */                char           d_name[256]; /* filename */            };

    int closedir(DIR *dirp); //关闭文件目录

    void seekdir(DIR *dirp, long offset);

    int dirfd(DIR *dirp);

     

      int scandir(const char *dirp,               //  目录名 

                  struct dirent ***namelist,        //返回目录列表

                  int (*filter)(const struct dirent *),     // 回调函数,用来过滤目录, NULL 表示不过滤               int (*compar)(const struct dirent **, const struct dirent **));   //排序函数

                  //返回目录的个数

     

    例子:

     

    1. #include <dirent.h>  
    2. #include <stdlib.h>  
    3. #include <stdio.h>  
    4.   
    5. int main(int argc, const char *argv[])  
    6. {  
    7.     DIR *d;  
    8.     struct dirent *pdir;  
    9.     d = opendir("../day12");  
    10.     if(d==NULL) printf("opendir error:%m "),exit(-1);  
    11.     while(pdir=readdir(d))  
    12.     {  
    13.         printf("%s %d ",pdir->d_name,pdir->d_type);  
    14.     }  
    15.   
    16.     closedir(d);  
    17.     return 0;  
    18. }  



     

     

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <stdio.h>  
    4. #include <unistd.h>  
    5. #include <dirent.h>  
    6.   
    7. int myfilter(const struct dirent*d)   //筛选掉隐藏文件  
    8. {  
    9.     if(memcmp(d->d_name,".",1)==0){  
    10.         return 0;  
    11.     }else{  
    12.         return -1;  
    13.     }  
    14. }  
    15.   
    16. int main(int argc, const char *argv[])  
    17. {  
    18.     struct dirent** d;  
    19.     int r;  
    20.     int i;  
    21.     r=scandir("/test",&d,myfilter,alphasort);//myfilter 自定义的筛选函数,alphasort系统提供的排序函数,  
    22.     printf("%d ",r);  
    23.     for(i=0;i<r;++i){  
    24.         printf("%s ",d[i]->d_name);  
    25.     }  
    26.     return 0;  
    27. }  

    二、进程

    1.什么是进程

           执行的程序:它能执行必定有代码->内存,文件等资源->CPU等

           进程有很多数据维护:进程状态 / 进程的属性

            所有的进程属性都采用结构体维护->实际上是树性数据结构

    2.创建进程

           1.代码?加载到内存?分配CPU时间片?

           2.进程有关的创建函数

                   iint system(const char *command);

                            建立独立进程,拥有独立的代码空间,内存空间。等待新的进程执行完毕system才返回(阻塞)

                            返回值与进程的返回值有关,system的返回值中8-15位存放返回值。

                             任何线程的返回值都不要超过255,原因就是上面一条

     

    1. #include <stdio.h>  
    2. #include <unistd.h>  
    3. #include <stdlib.h>  
    4. #include <sys/wait.h>  
    5. int main(int argc, const char *argv[])  
    6. {  
    7.     int r;  
    8.     printf("%d ",getpid());  
    9.     r=system("./test");  
    10.     //printf("%d ",r>>8);   //跟下面一行等价  
    11.     printf("%d ",WEXITSTATUS(r));  
    12.     return 0;  
    13. }  

     

                   FILE *popen(const char *command, const char *type);

                              创建进程; 在父子进程之间建立一个管道

     

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #include <stdio.h>  
    2. #include <unistd.h>  
    3. #include <stdlib.h>  
    4. #include <sys/wait.h>  
    5.   
    6. int main(int argc, const char *argv[])  
    7. {  
    8.     char buf[1024];  
    9.     FILE *f=popen("ls -l","r");  
    10.     int fd=fileno(f);  
    11.   
    12.     int r;  
    13.     while((r=read(fd,buf,1024))>0)  
    14.     {  
    15.         buf[r]=0;  
    16.         printf("%s",buf);  
    17.     }  
    18.     close(fd);  
    19.     pclose(f);  
    20.     return 0;  
    21. }  

     exec系列函数

     fork

  • 相关阅读:
    简单的jQuery无缝向上滚动效果
    http://yuanma.wxb0.com/ 唯品源码网站
    vue.js 2.0 --- 安装node环境,webpack和脚手架
    记住密码后,密码框Password会自动带出数据
    http://ask.dcloud.net.cn/question/11695 nativeUI的使用(移动的)
    微信内置浏览器 如何小窗不全屏播放视频?
    webstrom快捷键
    6个html5页面适配iphone6的技巧
    rem的js
    docker打包容器成镜像文件、镜像文件加载到镜像
  • 原文地址:https://www.cnblogs.com/ghostll/p/3537447.html
Copyright © 2011-2022 走看看