linux 环境编程学习笔记 第13,14天 IO与文件目录管理,进程


寒假学习 第13,14天 (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 **));   //排序函数

 //返回目录的个数

 

例子:

 

#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, const char *argv[])
{
    DIR *d;
    struct dirent *pdir;
    d = opendir("../day12");
    if(d==NULL) printf("opendir error:%m\n"),exit(-1);
    while(pdir=readdir(d))
    {
        printf("%s\t%d\n",pdir->d_name,pdir->d_type);
    }

    closedir(d);
    return 0;
}

 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>

int myfilter(const struct dirent*d)   //筛选掉隐藏文件
{
    if(memcmp(d->d_name,".",1)==0){
        return 0;
    }else{
        return -1;
    }
}

int main(int argc, const char *argv[])
{
    struct dirent** d;
    int r;
    int i;
    r=scandir("/test",&d,myfilter,alphasort);//myfilter 自定义的筛选函数,alphasort系统提供的排序函数,
    printf("%d\n",r);
    for(i=0;i<r;++i){
        printf("%s\n",d[i]->d_name);
    }
    return 0;
}

 

二、进程

 

1.什么是进程

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

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

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

 

2.创建进程

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

       2.进程有关的创建函数

               iint system(const char *command);

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

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

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

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
    int r;
    printf("%d\n",getpid());
    r=system("./test");
    //printf("%d\n",r>>8);   //跟下面一行等价
    printf("%d\n",WEXITSTATUS(r));
    return 0;
}

 

 

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

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

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(int argc, const char *argv[])
{
    char buf[1024];
    FILE *f=popen("ls -l","r");
    int fd=fileno(f);

    int r;
    while((r=read(fd,buf,1024))>0)
    {
        buf[r]=0;
        printf("%s",buf);
    }
    close(fd);
    pclose(f);
    return 0;
}

 

 

 exec系列函数

 fork