首页 技术 正文
技术 2022年11月8日
0 收藏 938 点赞 2,073 浏览 9909 个字

  这一章节侧重一些基本概念和书中用到的一些名词。

一.引言

  所有的操作都提供服务,典型的服务包括:执行新程序、打开文件、读写文件、分配存储区以及获得当前时间等。

二.UNIX体系结构

  《UNIX环境高级编程》笔记——1.UNIX基础知识

  其实linux常见的系统调用不是非常多,本书附录A按字符顺序列出了常用系统调用的原型。其实linux的应用编程,一要熟悉linux的基本概念,二要对各系统调用充分了解练习。

三.登录和shell

   3.1 登录

  口令文件/etc/passwd记录用户的信息,作为登录的依据。以下为该文件的一条内容:

   username:x:1000:1000:username,,,:/home/username:/bin/bash

   username:用户名

   x:加密口令

   1000:用户ID

   1000:用户组ID

   username,,,:注释字段

:/home/username,起始目录,登录后shell默认在此目录

   :/bin/bash,该用户使用的shell程序

  3.2 shell

   unix等系统有多种shell,如下表格:

  《UNIX环境高级编程》笔记——1.UNIX基础知识

  不同发行版本的linux,使用的默认shell不一定是bash,例如Ubuntu,就是用dash,dash与bash支持的script语法不完全一直,有时会导致脚本错误,需要注意bash的设置和切换。

四.文件和目录

4.1 文件系统

  文件系统是目录和文件的一种层次结构,所有的起点称为“根root目录”,即“/”.

  目录是一个包含目录项的文件。可以理解成目录本身是个特殊文件,包含很多目录项,每个目录项对应该目录下的一个文件,目录项中包含文件名和文件属性(类型、大小、所有者、权限、修改时间等)信息。

  涉及文件系统的知识,后面再深入。

4.2 文件名

  最好不要包含有其他意义的特殊字符,例如shell里的$,路径中的\等,最好由字母、数字、点、横杠-、下划线_等组成。现在的操作系统,一般最少支持255字符的文件名。

4.3 路径名

  绝对路径,/开头

  相对路径,没有/开头,从当前目录开始找

4.4 工作目录

  working directory,也叫current working derectory,每个进程都有一个工作目录,所有相对路径都是从工作目录开始解释。

  可以用chdir()函数改变工作目录。

4.5 起始目录

  登录以后的初始目录,/etc/passwd中存储的那个 /home/usrname路径

五.输入和输出

5.1 文件描述符

  非负整数,内核标识一个特定进程正在访问的文件。读写接口都用此描述符。

5.2 标准输入、输出、错误

  每当运行一个新程序,所有shell都为其打开3个标准文件描述符,分别为:

  STDIN:标准输入

  STDOUT:标准输出

  STDERR:标准错误

  默认都指向屏幕,可以重定向 >,>>, <,例如

  ls > ls_result_file    #ls的标准输出STDOU重定向到ls_result_file中

  【收获】:STDIN/STDOUT/STDERR是3个特殊的文件描述符!

5.3 不带缓冲的IO

  open、read、write、lseek、close等函数不带缓冲。

5.4 标准IO

  对系统调用的二次封装,为不带缓冲的IO提供缓冲,不用关心buf的大小,常见的函数printf、fgets等。fgets里调用了read,能读取一行,而read以字节为单位。

六.程序和进程

6.1 程序

   程序是一个可执行文件,可以被exec函数执行。

6.2 进程和进程ID

   程序的执行实体是进程(process),进程的唯一标识是process ID,即PID,是一个非负整数。

6.3 进程控制

   有三个主要函数,fork,exec,waitpid

6.4 线程和线程ID

     一个进程,一般有一个控制线程thread,但也可以有多个线程。

  • 线程也有ID,但是只在进程内有效,在其他进程内无效
  • 多个线程共享进程的地址空间,文件描述符,栈以及进程相关属性。需注意多个线程访问共享资源时的保护
  • 线程控制函数与进程类似,是另一套接口

七.出错处理

  有个全局变量errno,操作系统统一定义了若干错误,出错时付给这个变量。例如,open打开文件错误,返回-1,具体错误由errno体现。

#<errno.h>: 定义errno全局变量
/* Declare the `errno' variable, unless it's defined as a macro by
   bits/errno.h.  This is the case in GNU, where it is a per-thread
   variable.  This redeclaration using the macro still works, but it
   will be a function declaration without a prototype and may trigger
   a -Wstrict-prototypes warning.  */
#ifndef    errno
extern int errno;
#endif#经过一级级的include,各个具体错误码定义如下:
#asm-generic/errno-base.h:
#define    EPERM         1    /* Operation not permitted */
#define    ENOENT         2    /* No such file or directory */
#define    ESRCH         3    /* No such process */
#define    EINTR         4    /* Interrupted system call */
#define    EIO         5    /* I/O error */
#define    ENXIO         6    /* No such device or address */
#define    E2BIG         7    /* Argument list too long */
#define    ENOEXEC         8    /* Exec format error */
#define    EBADF         9    /* Bad file number */
#define    ECHILD        10    /* No child processes */
#define    EAGAIN        11    /* Try again */
#define    ENOMEM        12    /* Out of memory */
#define    EACCES        13    /* Permission denied */
#define    EFAULT        14    /* Bad address */
#define    ENOTBLK        15    /* Block device required */
#define    EBUSY        16    /* Device or resource busy */
#define    EEXIST        17    /* File exists */
#define    EXDEV        18    /* Cross-device link */
#define    ENODEV        19    /* No such device */
#define    ENOTDIR        20    /* Not a directory */
#define    EISDIR        21    /* Is a directory */
#define    EINVAL        22    /* Invalid argument */
#define    ENFILE        23    /* File table overflow */
#define    EMFILE        24    /* Too many open files */
#define    ENOTTY        25    /* Not a typewriter */
#define    ETXTBSY        26    /* Text file busy */
#define    EFBIG        27    /* File too large */
#define    ENOSPC        28    /* No space left on device */
#define    ESPIPE        29    /* Illegal seek */
#define    EROFS        30    /* Read-only file system */
#define    EMLINK        31    /* Too many links */
#define    EPIPE        32    /* Broken pipe */
#define    EDOM        33    /* Math argument out of domain of func */
#define    ERANGE        34    /* Math result not representable */
#asm-generic/errno.h:
#define    EDEADLK        35    /* Resource deadlock would occur */
#define    ENAMETOOLONG    36    /* File name too long */
#define    ENOLCK        37    /* No record locks available */
#define    ENOSYS        38    /* Function not implemented */
#define    ENOTEMPTY    39    /* Directory not empty */
#define    ELOOP        40    /* Too many symbolic links encountered */
#define    EWOULDBLOCK    EAGAIN    /* Operation would block */
#define    ENOMSG        42    /* No message of desired type */
#define    EIDRM        43    /* Identifier removed */
#define    ECHRNG        44    /* Channel number out of range */
#define    EL2NSYNC    45    /* Level 2 not synchronized */
#define    EL3HLT        46    /* Level 3 halted */
#define    EL3RST        47    /* Level 3 reset */
#define    ELNRNG        48    /* Link number out of range */
#define    EUNATCH        49    /* Protocol driver not attached */
#define    ENOCSI        50    /* No CSI structure available */
#define    EL2HLT        51    /* Level 2 halted */
#define    EBADE        52    /* Invalid exchange */
#define    EBADR        53    /* Invalid request descriptor */
#define    EXFULL        54    /* Exchange full */
#define    ENOANO        55    /* No anode */
#define    EBADRQC        56    /* Invalid request code */
#define    EBADSLT        57    /* Invalid slot */#define    EDEADLOCK    EDEADLK#define    EBFONT        59    /* Bad font file format */
#define    ENOSTR        60    /* Device not a stream */
#define    ENODATA        61    /* No data available */
#define    ETIME        62    /* Timer expired */
#define    ENOSR        63    /* Out of streams resources */
#define    ENONET        64    /* Machine is not on the network */
#define    ENOPKG        65    /* Package not installed */
#define    EREMOTE        66    /* Object is remote */
#define    ENOLINK        67    /* Link has been severed */
#define    EADV        68    /* Advertise error */
#define    ESRMNT        69    /* Srmount error */
#define    ECOMM        70    /* Communication error on send */
#define    EPROTO        71    /* Protocol error */
#define    EMULTIHOP    72    /* Multihop attempted */
#define    EDOTDOT        73    /* RFS specific error */
#define    EBADMSG        74    /* Not a data message */
#define    EOVERFLOW    75    /* Value too large for defined data type */
#define    ENOTUNIQ    76    /* Name not unique on network */
#define    EBADFD        77    /* File descriptor in bad state */
#define    EREMCHG        78    /* Remote address changed */
#define    ELIBACC        79    /* Can not access a needed shared library */
#define    ELIBBAD        80    /* Accessing a corrupted shared library */
#define    ELIBSCN        81    /* .lib section in a.out corrupted */
#define    ELIBMAX        82    /* Attempting to link in too many shared libraries */
#define    ELIBEXEC    83    /* Cannot exec a shared library directly */
#define    EILSEQ        84    /* Illegal byte sequence */
#define    ERESTART    85    /* Interrupted system call should be restarted */
#define    ESTRPIPE    86    /* Streams pipe error */
#define    EUSERS        87    /* Too many users */
#define    ENOTSOCK    88    /* Socket operation on non-socket */
#define    EDESTADDRREQ    89    /* Destination address required */
#define    EMSGSIZE    90    /* Message too long */
#define    EPROTOTYPE    91    /* Protocol wrong type for socket */
#define    ENOPROTOOPT    92    /* Protocol not available */
#define    EPROTONOSUPPORT    93    /* Protocol not supported */
#define    ESOCKTNOSUPPORT    94    /* Socket type not supported */
#define    EOPNOTSUPP    95    /* Operation not supported on transport endpoint */
#define    EPFNOSUPPORT    96    /* Protocol family not supported */
#define    EAFNOSUPPORT    97    /* Address family not supported by protocol */
#define    EADDRINUSE    98    /* Address already in use */
#define    EADDRNOTAVAIL    99    /* Cannot assign requested address */
#define    ENETDOWN    100    /* Network is down */
#define    ENETUNREACH    101    /* Network is unreachable */
#define    ENETRESET    102    /* Network dropped connection because of reset */
#define    ECONNABORTED    103    /* Software caused connection abort */
#define    ECONNRESET    104    /* Connection reset by peer */
#define    ENOBUFS        105    /* No buffer space available */
#define    EISCONN        106    /* Transport endpoint is already connected */
#define    ENOTCONN    107    /* Transport endpoint is not connected */
#define    ESHUTDOWN    108    /* Cannot send after transport endpoint shutdown */
#define    ETOOMANYREFS    109    /* Too many references: cannot splice */
#define    ETIMEDOUT    110    /* Connection timed out */
#define    ECONNREFUSED    111    /* Connection refused */
#define    EHOSTDOWN    112    /* Host is down */
#define    EHOSTUNREACH    113    /* No route to host */
#define    EALREADY    114    /* Operation already in progress */
#define    EINPROGRESS    115    /* Operation now in progress */
#define    ESTALE        116    /* Stale NFS file handle */
#define    EUCLEAN        117    /* Structure needs cleaning */
#define    ENOTNAM        118    /* Not a XENIX named type file */
#define    ENAVAIL        119    /* No XENIX semaphores available */
#define    EISNAM        120    /* Is a named type file */
#define    EREMOTEIO    121    /* Remote I/O error */
#define    EDQUOT        122    /* Quota exceeded */#define    ENOMEDIUM    123    /* No medium found */
#define    EMEDIUMTYPE    124    /* Wrong medium type */
#define    ECANCELED    125    /* Operation Canceled */
#define    ENOKEY        126    /* Required key not available */
#define    EKEYEXPIRED    127    /* Key has expired */
#define    EKEYREVOKED    128    /* Key has been revoked */
#define    EKEYREJECTED    129    /* Key was rejected by service *//* for robust mutexes */
#define    EOWNERDEAD    130    /* Owner died */
#define    ENOTRECOVERABLE    131    /* State not recoverable */#define ERFKILL        132    /* Operation not possible due to RF-kill */#define EHWPOISON    133    /* Memory page has hardware error */

通过errno输出字符错误信息。

#include <string.h>
char * strerror(int errnum);  #通过errno获取一个错误信息的字符串#include <stdio.h>
void perror( const char *msg )  #向stderr上输出错误信息"msg:"+"errno代表的错误信息"

八.用户标识

用户ID: user ID,与用户一一对应,在/etc/passwd中有,

用户组ID: group ID,多个用户可以有相同组ID,便于分享文件

附属组ID():允许用户除了组ID,还可以有若干其他组的ID,保存在/etc/group文件里。

【收获】为什么用数字表示用户?历史原因,每个文件都保存该文件的用户ID和用户组ID,如果用字符类型,会占用过多空间,且不易操作,所以用数字,只要两个int就可以了。

九.信号

signal用于通知进程发生某种情况,信号处理处理有三种方式:

  • 忽略该信号
  • 系统按默认方式处理,如果进程特殊设置,系统就按照默认的方式处理信号
  • 进程提供一个函数,信号发生时调用

十.时间值

10.1 日历时间(UTC)

 该值是自1 9 7 0年1月1日0 0 : 0 0 : 0 0以来国际标准时间( U T C)所经过的秒数累计值(早期的手册称 U T C为格林尼治标准时间) ,系统中用time_t类型表示。

10.2 进程时间

  也叫CPU时间,用来度量占用的CPU资源。

  单位:时钟滴答,系统每秒的滴答数可以通过sysconf获取,曾经取值50/60/100等,用clock_t类型表示;

  三个进程时间值:时钟时间real、用户时间user、系统时间sys。时钟时间是整个进程的执行时间,此时间跟进程的数量有关,例如同样一个进程,如果系统中有3个和10个其他进程和的情况下,时钟时间是不一样的,即跟系统的负荷有关系。用户时间是该进程处于用户态时运行的时间。系统时间是该进程进入内核态以后的运行时间。 read >= (user+sys)

:/work/platform-zynq/linux-xlnx-repo-smp$ time -p grep 'xilinx'  */*.h
real 0.38
user 0.00
sys 0.05real > (user+sys)

十一.系统调用和库函数

 《UNIX环境高级编程》笔记——1.UNIX基础知识

  C库函数也可以不调用系统调用。

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898