第八章读书笔记

第八章读书笔记

8.1系统调用

  • 在操作系统中,进程以两种不同的模式运行,即内核模式和用户模式,简称Kmode和Umode。在Umode中,进程的权限非常有限。它不能执行任何需要特殊权限的操作。特殊权限的操作必须在Kmode下执行。系统调用(简称syscall)是一种允许进程进入Kmode以执行Umode不允许操作的机制。复刻子进程、修改执行映像,甚至是终止等操作都必须在内核中执行。

8.2系统调用手册页

  • 在Unix以及大多版本的Linux中,在线手册页保存在/usr/man/目录中(Goldt等1995;Kerrisk 2010,2017)。而在Ubuntu Linux中,则保存在/usr/share/man目录中。man2子目录中列出了所有系统调用手册页。sh命令man 2 NAME显示了系统调用名称的手册页。
    例如:

简单的系统调用

  • access: 检查对某个文件的权限
    int access(char *pathname, int mode);
  • chdir: 更改目录
    int chdir(const char *path);
  • chmod:更改某个文件的权限
    int chmod(char *path, mode_t mode);
  • chown:更改文件所有人
    int chown(char *name,int uid,int gid);
  • chroot:将(逻辑)根目录更改为路径名
    int chroot(char *pathname);
  • getewd:获取CWD的绝对路径名
    char *getcwd(char *buf, int aize);
  • mkdir:创建目录
    tnt mkdir(char *pathname,mode_t mode);
  • rmdir:移除目录(必须为空)
    int rmdir(char *pathname);
  • link:将新文件名硬链接到旧文件名
    tnt 1ink(char *o1dpath,char *newpath);
  • umlink:减少文件的链接数;如果链接数达到0,则删除文件
    int unlink(char *pathname);
  • symlink:为文件创建一个符号链接
    int symlink(char *o1dpath, char*newpath);
  • rename:更改文件名称
    int rename (char *oldpath, char *newpath);
  • utime:更改文件的访问和修改时间
    int utime(char *pathname, struct utimebuf *time)

以下系统调用则要用到超级用户权限

  • mount: 将文件系统添加到挂载点目录上
    int mount(char *specialfile, char *mountDir);
  • umount: 分离挂载的文件系统
    int umount(char *dix);
  • mknod: 创建特殊文件
    int mknod(char *path,int mode, int device);

常用的系统调用

  • stat: 获取文件状态信息
点击查看代码
int stat(char *filename, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(char *filename, struct stat *buf);

  • open:打开一个文件进行读、写和追加
    int open(char *file, int flags,int mode)
  • close:关闭打开的文件描述符
    int close(int fd);
  • read:读取打开的文件描述符
    int read(int fd, char buf[], int count);
  • write:写入打开的文件描述符
    int write(int fd, char buf[], int count);
  • lseek:重新定位文件描述符的读/写偏移量
    int lseek(int fd, int offset, int whence);
  • dup:将文件描述符复制到可用的最小描述符编号中
    int dup(int oldfd);
  • dup2:将oldfd复制到newfd中,如果文件链接数为0,则删除文件
    int dup2(int oldfd, int newfd);
  • link:将新文件硬链接到旧文件
    int link(char *oldPath, char *newPath);
  • unlink:取消某个文件的链接;如果文件链接数为0,则删除文件
    int unlink(char *pathname);
  • symlink:创建一个符号链接
    int symlink(char *target, char *newpath);
  • readlink:读取符号链接文件的内容
    int readlink(char *path, char *buf, int bufsize);
  • umask:设置文件创建掩码;文件权限为(mask & ~umask)
    int umask(int umask);

链接文件

硬链接文件

  • 硬链接:
    ln oldpath newpath
    创建从newpath到oldpath的硬链接。对应的系统调用为:
    link(char *oldpath,char *newpath)
    删除:
    unlink(char *pathname)

符号链接文件

  • 软连接:
    ln -s oldpath newpath #in command with the -s flag
    创建从newpath到oldpath的软链接或符号链接。对应的系统调用是:
    symlink(char *oldpath,chat *newpath)
posted @ 2022-09-23 13:47  给我个名字  阅读(15)  评论(0编辑  收藏  举报