zoukankan      html  css  js  c++  java
  • linux高级编程补充知识

    F: 计算机系统结构:

         -------------------------------

                应用程序

               -----------------

           |  库函数

         -------------------------------

                系统调用

         -------------------------------

         虚拟文件系统  |    进程模块

         -(文件模块)-|   进程间通信

          设备文件     |

        -------------------------------

                

        -------------------------------

    G: 内存结构划分(32b)/存储空间分配:

         用户空间3G:代码区,字符串常量区,数据区,堆,栈stack

         内核空间1G

    H: 系统数据文件:

         /etc/passwd:用户相关的信息/用户的数据信息  

           eg:root:x:0:0:root:/root:/bin/bash,用‘:’进行分割

              用户者:密码占位符:用户的id:用户所在组的id:用户的描述信息:用户的家目录:用户登录后默认使用的shell(shell 应用程序,作用:命令行解释器)

      

    h1: /etc/group:组的相关信息

         eg:bin:x:1:bin,daemon

             组名:组密码占位符:组id:组成员

    h2: 用户相关操作:

         struct passwd *getpwnam(const char *name);

         struct passwd *getpwuid(uid_t uid);

         struct passwd *getpwent(void);

         void setpwent(void);//定位到文件开头,反转

         void endpwent(void);

    h3: 组相关操作:

         struct group *getgrnam(const char *name);

         struct group *getgrgid(gid_t gid);

    I: 启动例程:

        命令行参数:字符串的处理

        argv----字符串的数组;argv[argc] == NULL

    J: 位运算:

         bit 0/1

             & |

             00  0 0

             01  0 1

             10  0 1

             11  1 1

        一个bit跟0相与& 结果是0;跟1相与&,结果是他本身.

        一个bit跟0相或| 结果是他本身;跟1相或|,结果是1.

    K: 设置文件权限(权限的掩码)

       mode_t umask(mode_t mask);

       系统调用

        permission & ~umask

        regular file 0666 & ~0022

             0b110 110 110

        0b000 010 010/0b111 101 101 取反做与

            & 0b110 100 100  0644

      linux系统提供的umask命令跟umask()系统调用的作用是一样的

    k1: int chmod(const char *path, mode_t mode);

        int fchmod(int fd, mode_t mode);

      改变文件的存取许可权限

       linux系统提供的chmod命令跟chmod()系统调用作用是一样的

    k2: int chown(const char *path, uid_t owner, gid_t group);

        int fchown(int fd, uid_t owner, gid_t group);

        int lchown(const char *path, uid_t owner, gid_t group);

      改变文件的用户ID uid和组ID gid

       chown 命令改变文件的属主owner和属组group

    k3: int truncate(const char *path, off_t length);

        int ftruncate(int fd, off_t length);

      将文件的长度截短为length

       如果当前文件的长度要是小于我们的length,其结果与系统有关.

      将文件截短为0时,它属于一个特例,与open或creat调用设定O_TRUNC标志是一样的.

    L: 硬链接以及硬链接的限制条件

        1.堆目录不能硬链接

        2.硬链接不能跨文件系统/硬链接不能跨磁盘分区

    l1: link 创建硬链接

          int link(const char *oldpath, const char *newpath);

        symlink 创建符号链接 软链接 快捷方式

          int symlink(const char *oldpath, const char *newpath);

        int unlink(const char *pathname);

          删除文件名,有可能会删除文件(当硬链接数/inode里的引用计数为0的时候,会删除文件)

    M: rm remove cd 路径;rm 文件

        int remove(const char *pathname);

    m1: rename

         int rename(const char *oldpath, const char *newpath);

    m2: mkdir

         int mkdir(const char *pathname, mode_t mode);

    m3: rmdir

         int rmdir(const char *pathname);

        针对空目录进行删除.

    N: 时间和日期

        time() get time in seconds

        time_t time(time_t *t);

       1.出参

       2.返回值

        char *ctime(const time_t *timep);

         eg: "Wed Jun 30 21:49:08 1993 "

        struct tm *localtime(const time_t *timep);    返回值是结构体指针

        time_t mktime(struct tm *tm);

        char *asctime(const struct tm *tm);

    n1: 时间的字符串表达

        ^ ^

        | |asctime

        |

        | struct tm-------->

        | ^  |

        |ctime |localtime  |mktime

        | |  |

        ^---------time_t<---------

        ^

        |time

        |

        内核kernel

    O: 堆heap

       malloc

       free

       char *ptr;

       char *p;

         ptr =malloc(1024*sizeof(char));

      ....

        p=ptr+512;

    o1: 元数据 metadata

         void *realloc(void *ptr, size_t size);//增加空间

    P: 前台/后台:

        命令 & :表示后台运行

        fg 后台进程调入到前台运行  eg:fg %1

    bg 前台进程调入到后台运行  eg:bg %1

        jobs 查看后台作业/进程

    Q: linux如何运行一个程序/如何执行execvp?

        1.将指定的程序/可执行文件加载/复制到调用exec的进程

        2.将给定的字符串数组作为argv传给这个程序

        3.运行这个程序

    R:  status分为3部分:exit返回值(8b);异常退出号(7b);内核映像(core dump)(1b)

    S: 目录块/目录项(inode号和名d_name)

       数据块

       inode节点表/inode节点数组

    T: 自顶向下的程序设计方法       

       分文件:函数数量多的时候,会根据函数的类型分文件

       头文件:宏定义;全局变量的声明;结构体类型的定义;函数的声明

       分函数:一个函数中代码数量多的时候,会跟据完成的任务、功能分函数(每个函数的代码是1-15行)

        分函数通常只做方法处理,数据的输入和打印输出通常在主函数中完成

    t1: #include"strcpypt.h"   // “”----> 当前目录下优先查找

        #include<string.h>   // <>----> 在环境变量PATH制定的目录下优先查找

    t2: #ifndef   //防止头文件被重复的包含

    U: 服务/客户:

          bc----计算器命令(bc----客户;dc----服务)

      bc是dc的预处理

      bc提供界面处理,使用dc提供的服务,bc是dc的客户

      bc和dc两者之间是独立的程序

      双向通信

    V: 普通文件

       竞态文件

       文件锁/记录锁

       fcntl----改变已打开的文件的性质

     加读数据的锁

       void set_read_lock(int fd)

       {

        struct flock lockinfo;

    lockinfo.l_type = F_RDLCK;

    lockinfo.l_whence = SEEK_SET;

    lockinfo.l_start = 0;

    lockinfo.l_len = 0;

    lockinfo.l_pid = getpid();

    fcntl(fd,F_SETLKW,&lockinfo);

    lockinfo.l_type = F_RDLCK;

       }

     加写数据的锁:

       void set_read_lock(int fd)

       {

        struct flock lockinfo;

    lockinfo.l_type = F_WRLCK;

    lockinfo.l_whence = SEEK_SET;

    lockinfo.l_start = 0;

    lockinfo.l_len = 0;

    lockinfo.l_pid = getpid();

    fcntl(fd,F_SETLKW,&lockinfo);

    lockinfo.l_type = F_RDLCK;

       }

     解锁:

       void set_read_lock(int fd)

       {

        struct flock lockinfo;

    lockinfo.l_type = F_UNLCK;

    lockinfo.l_whence = SEEK_SET;

    lockinfo.l_start = 0;

    lockinfo.l_len = 0;

    lockinfo.l_pid = getpid();

    fcntl(fd,F_SETLKW,&lockinfo);

    lockinfo.l_type = F_RDLCK;

       }

    W: 函数名---函数的地址/函数的指针

       指针----1,地址  2,指针变量

    X: 设备文件:

    x1: 串口/终端:

          处理进程和外部设备之间的数据流的内核子程序的集合----被称为终端驱动程序/tty(TeleTYpe)驱动程序

      

      读取终端驱动程序的属性:

       int tcgetattr(int fd, struct termios *termios_p);

      设置终端驱动程序的属性:

       int tcsetattr(int fd, int optional_actions,const struct termios *termios_p);

         TCSANOW:立即更新驱动程序的设置

      结构体 termios:------------------保存属性

        tcflag_t c_iflag;      /* input modes */-------输入:驱动程序如何处理从终端来的字符

        tcflag_t c_oflag;      /* output modes */-------输出:驱动程序如何处理流向终端的字符

        tcflag_t c_cflag;      /* control modes */--------控制:字符如何被表示(位的个数,停止位等)

        tcflag_t c_lflag;      /* local modes */-------本地:驱动程序如何处理来自驱动程序内部的字符

        cc_t     c_cc[NCCS];   /* control chars */

    =============================================================================================================

    书写步骤:

            struct termois attribs;   //保存属性

            tcgetattr(fd,&attribs);   //获取当前属性

       //修改:

            测试位:if(attribs.flagset & MASK)...

    置位:attribs.flagset |= MASK;...

    清除位:attribs.flagset &|= ~MASK;...

            tcsetattr(fd,TCSANOW,&attribs);   //写回属性

     -------------------------------------------------------------------------------------------------------------

    总结:

        改变终端驱动程序的设置:(Read--Mddify--Write)

            1.读取:从驱动程序中获取当前的属性   

        2.修改:修改所要修改的属性

        3.写回:将修改后的属性去回写到驱动程序

     -------------------------------------------------------------------------------------------------------------

     ===============================================================================================================

  • 相关阅读:
    centos 启动报错 “error:failure reading sector 0x9b268 from 'hd0'”
    Jenkins安装及插件管理
    svn关联Jenkins自动发布代码
    LVS逻辑卷的创建、扩展、销毁
    Linux上安装SVN服务端及SVN的可视化工具
    CentOS7下配置防火墙放过Keepalived
    Prometheus apache_exporter 监控配置
    CentOS启动报错:Error at boot time: "[Firmware Bug]: TSC_DEADLINE disabled due to Errata
    linux上安装open***
    python学习day15 Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
  • 原文地址:https://www.cnblogs.com/jfyl1573/p/6250068.html
Copyright © 2011-2022 走看看