day

xiaoxiao2021-02-28  91

一文件的管理 1 statfstatlstat重点2 chmodfchmod3 truncateftruncate重点4 mmap和munmap5 其他操作函数 1umask2link3unlink4rename5remove 二目录的管理 1 常用的目录操作函数 1opendirfdopendir2readdir3closedir 2 其他目录操作函数 1mkdir - - - 主要用于创建目录2rmdir - - - 主要用于删除目录3chdir fchdir4getcwd 三进程的管理 1 基本概念和基本命令

一、文件的管理

1.1 stat()/fstat()/lstat()(重点)

#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat(const char *pathname, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *pathname, struct stat *buf); 功能: 主要用于获取指定文件的状态信息; 参数: 第一个参数:字符串形式的文件路径名/文件描述符; 第二个参数:结构体指针,传递结构体变量的地址; struct stat { ... mode_t st_mode; /* 文件的类型和权限 %o */ off_t st_size; /* 文件的大小 %ld */ time_t st_mtime; /* 最后一次修改时间 %ld */ ... }; #include <time.h> char *ctime(const time_t *timep); 功能: 主要用于将参数指定的整数时间转换为字符串类型时间; #include <time.h> struct tm *localtime(const time_t *timep); 功能: 主要用于将参数指定的整数时间转换为结构体指针类型; struct tm { int tm_sec; /* 秒 */ int tm_min; /* 分 */ int tm_hour; /* 时 */ int tm_mday; /* 日 */ int tm_mon; /* 月 + 1 */ int tm_year; /* 年 + 1900 */ ... }; 注意: 请问获取一个文件大小的主要方式有哪些??? 1. 调用fseek()和ftell(); 2. 调用lseek(); 3. 调用stat()/fstat()获取;

1.2 chmod()/fchmod()

#include <sys/stat.h> int chmod(const char *pathname, mode_t mode); int fchmod(int fd, mode_t mode); 功能: 主要用于将指定文件的权限修改为指定的新权限; 参数: 第一个参数:字符串形式的路径名/文件描述符; 第二个参数:具体的新权限,如:0664;

1.3 truncate()/ftruncate()(重点)

#include <unistd.h> #include <sys/types.h> int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length); //off_t = long int 功能: 主要用于修改指定文件的大小到指定的长度; 参数: 第一个参数:字符串形式的路径名/文件描述符; 第二个参数:指定最新的长度/大小; 注意: 如果文件的长度变小了,则多余的数据会产生丢失; 如果文件的长度变大了,则扩展空间读到的数据是’\0’;

1.4 mmap()和munmap()

#include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

功能:   主要用于建立文件/设备到虚拟地址的映射;   本质上就是把对文件的读写操作转换为对内存地址的操作,当有大量的数据进行读写时,这种方式效率比read()/write()的方式高一些;

参数: 第一个参数:默认给NULL(由系统内核指定位置) 第二个参数:映射的大小; 第三个参数:映射的权限; 第四个参数:MAP_SHARED(映射对文件有效) 第五个参数:文件描述符; 第六个参数:文件偏移量; 返回值: success —- 指向映射区域的指针,error —- MAP_FAILED 即 (void*)-1; #include <sys/mman.h> int munmap(void *addr, size_t length); 功能: 主要用于解除文件/设备到虚拟地址的映射; 参数: 第一个参数:指向映射区域的指针; 第二个参数:映射的大小;

1.5 其他操作函数

(1)umask()

#include <sys/types.h> #include <sys/stat.h> mode_t umask(mode_t mask); //mode_t = unsigned int 功能: 设置新文件在创建时要屏蔽的权限,并返回设置前存在的屏蔽权限;(默认屏蔽权限mask=02) #include <unistd.h> int link(const char *oldpath, const char *newpath); 功能: 给指定文件创建新的硬链接;若新文件路径名已存在,则创建失败。 #include <unistd.h> int unlink(const char *pathname); 功能: 删除硬链接;(包括软链接)当该硬链接为最后一个链接被删除时,会释放文件所占的磁盘空间。

(4)rename()

#include <stdio.h> int rename(const char *oldpath, const char *newpath); 功能: 将指定文件重命名并移动到指定路径;

(5)remove()

#include <stdio.h> int remove(const char *pathname); 功能: 删除文件或空目录;

… …


二、目录的管理

2.1 常用的目录操作函数

(1)opendir()/fdopendir()

#include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); DIR *fdopendir(int fd); 功能: 主要用于打开参数指定的目录,成功返回一个DIR类型的指针,失败返回NULL;

(2)readdir()

#include <dirent.h> struct dirent *readdir(DIR *dirp);

功能:   主要用于读取参数指定目录中的内容,成功返回一个结构体指针,失败返回NULL;   该函数返回NULL有两种情况:一种是读到了目录的末尾,但是不会改变errno的数值;另一种是出现了错误,但是会设置errno的数值表达错误的原因;

struct dirent { ino_t d_ino; /* I节点编号 */ off_t d_off; /* Not an offset; see below */ unsigned short d_reclen; /* Length of this record */ unsigned char d_type; /* 文件的类型 */ char d_name[256]; /* 文件的名称 */ };

(3)closedir()

#include <sys/types.h> #include <dirent.h> int closedir(DIR *dirp); 功能: 主要用于关闭参数指定的目录,参数为opendir()的返回值;

2.2 其他目录操作函数

(1)mkdir() - - - 主要用于创建目录

#include <sys/stat.h> #include <sys/types.h> int mkdir(const char *pathname, mode_t mode);

(2)rmdir() - - - 主要用于删除目录;

#include <unistd.h> int rmdir(const char *pathname);

(3)chdir() / fchdir()

#include <unistd.h> int chdir(const char *path); int fchdir(int fd);

功能:   chdir() - 主要用于切换当前工作目录;   fchdir() - 除参数外,其余和chdir()相同;

(4)getcwd()

#include <unistd.h> char *getcwd(char *buf, size_t size);

功能:   主要用于获取当前工作目录的绝对路径;

参数:   第一个参数:字符串类型的指针,用来保存当前工作目录的绝对路径;   第二个参数:能够存放字符串的长度;

返回值:   success —- 保存路径的字符串首地址的指针,error —- NULL;


三、进程的管理

3.1 基本概念和基本命令

(1)基本概念

程序 - - - 存放在磁盘/硬盘上的可执行文件; 进程 - - - 运行在内存中的程序; 同一个程序可以同时对应多个进程;

(2)基本命令

ps - - - 表示查看当前终端所启动的进程信息;

ps命令的执行结果如下(了解): PID - - - – 进程的编号(重点) TTY - - - - 终端的次要装置号码 TIME - - - 消耗CPU的时间 CMD - - – 进程的名称以及路径(重点)

ps -aux:表示显示所有包括其它使用者的进程; ps -aux | more:表示分屏显示进程的信息;

ps -aux | more的执行结果如下(了解): USER - - - - - - - - - - -用户名称 PID - - - - - - - - - - - - -进程的编号 %CPU - - - - - - - - - - 占用CPU的百分比 %MEM - - - - - - - - – 占用内存的百分比 VSZ - - - - - - - - - - - - 占用虚拟内存的大小 RSS - - - - - - - - - - - - 占用物理内存的大小 TTY - - - - - - - - - - - - -终端的次要装置号码 STAT - - - - - - - - - - - -进程的状态信息 START - - - - - - - - - – 进程的启动时间 TIME - - - - - - - - - - - -消耗CPU的时间 COMMAND - - - - - – 进程的名称以及路径

常见的进程状态主要有(了解):

S - 表示休眠状态,为了减轻CPU的压力; s - 表示进程的领导者,也就是拥有子进程; Z - 表示僵尸进程,也就是已经结束但资源没有回收的进程; R - 表示正在运行的进程; O - 表示可以运行的进程; T - 表示挂起状态的进程; < - 表示优先级比较高的进程; N - 表示优先级比较低的进程;

ps -ef:表示以全格式的方式显示所有进程信息; ps -ef | more:表示分屏显示进程信息;

ps -ef | more的执行结果如下(了解): UID - - - - - 用户的编号 PID - - - - - 进程的编号 PPID - - - - 父进程的编号(重点) C - - - - - - -占用CPU的百分比 STIME - - -进程的启动时间 TTY - - - - -终端的次要装置号码 TIME - - - -消耗CPU的时间 CMD - - - - 进程的名称以及路径信息

  目前主流的操作系统都支持多进程,如果进程A启动了进程B,那么进程A就叫做进程B的父进程,进程B就叫做进程A的子进程;   当前系统中,进程0(系统内部进程/调度进程)启动了进程1(init)和进程2,其他所有进程都是直接/间接由进程1/进程2启动,从而构成树形结构;   PID - 进程的编号,是操作系统中对进程的唯一标识,进程编号的数据类型一般是int类型,但是从0开始使用,操作系统一般会采用延迟重用的策略进行管理,保证在任意时刻进程号都是唯一的;

  kill -9 进程号:表示杀死指定的进程;

练习: 要求打印指定目录中的所有内容,要求子目录内容也要打印出来;

提示:

void print(char* path) { // 打印所有内容 } int main(void) { print("../../day_03/code"); return 0; } 明日预报: 进程管理
转载请注明原文地址: https://www.6miu.com/read-58511.html

最新回复(0)