【第6章 系统数据文件和信息】一、<pwd.h>中的passwd结构/* The passwd structure. */struct passwd{char *pw_name; /* Username. */char *pw_passwd; /* Password. */__uid_t pw_uid; /* User ID. */__gid_t pw_gid; /* Group ID. */char *pw_gecos; /* Real name. */char *pw_dir; /* Home directory. */char *pw_shell; /* Shell program. */};/etc/passwd文本文件#include <sys/types.h>#icnlude <pwd.h>struct passwd* getpwuid(uid_t uid);struct passwd* getpwnam(const char* name);查看passwd文件struct passwd* getpwent(void);void setpwent(void);void endpwent(void);影子口令/etc/shadow或者/etc/master.passwd组文件/etc/group#include<sys/types.h>#include<unistd.h>int getgroups(int gidsetsize, gid_t grouplist[]);int setgroups(int* groups, const gid_tgrouplist[]);int initgroups(const char* initername, gid_t basegid);二、<netdb.h>主机/etc/hosts网络(记录网络数据信息的数据文件)/etc/networks协议/etc/protocols服务(网络服务器提供的服务的数据文件)/etc/servicesget读下一个记录set打开相应数据文件end关闭相应数据文件三、登录会计:登录和注销事件/var/adm或者/var/run/utmp /var/log/wtmp系统标识#include <sys/utsname.h>int uname(struct utsname* name)返回系统和主机信息int gethostname(char* name, int namelen);主机名四、时间和日期历程#include <time.h>time_t time(time_t* carptr)返回当前日期struct tm* gmtime(const time_t* carptr)转化为国际标准时间struct tm* localtime(const time_t* calptr)转化为本地时间//转化为形如 Tue Jan 14 17:49:03 1992/n/0char* asctime(const struct tm* calptr)char* ctime(const time_t calptr);size_t strftime(char* buf, siz_t maxsize, const char* format, const struct tm* tmptr);格式化时间函数【第7章 UNIX进程的环境】一、进程调用main()函数 进程终止的方式内核->用户进程:“exec()”调用“c起动例程”调用“main()”调用“用户函数”...->调用“exit()”调用“_exit()”退出。#include <stdlib.h>void exit(int status); #include <unistd.h>void _exit(int status); //直接进入内核#include <stdlib.h>int atexit((void* func)(void));登记函数,exit()以相反的顺序调用,二、使用环境变量命令行参数argv[argc] == NULL环境表 name=value#include <stdlib.h>char* getenv(const char* name) //得到环境变量未找到返回NULLint putenv(const char* str);取"name=value"的形式放入环境表中int setenv(const char* name, const char* value, int rewrite);将name设置为value rewrite=0时,删除原来的定义三、存储器布局高地址 命令行参数和环境变量 栈 ↓ ... ↑ 堆 未初始化的数据-由exec赋初值0 bss 初始化的数据-又exec从程序文件中读到 data 正文 text低地址size exec e.g. size /bin/cc /bin/sh四、分配另外的存储空间#include <stdlib.h>void* malloc(size_t size); //分配但不初始化void* calloc(size_t nobj, size_t size); //分配后初始化为0 nobj元素个数 size元素大小原型:extern void *calloc(int num_elems, int elem_size);用法:#include <alloc.h>功能:为具有num_elems个长度为elem_size元素的数组分配内存说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。当内存不再使用时,应使用free()函数将内存块释放。void* realloc(void* ptr, size_t newsize); //改变(增加,减少)已经分配的长度失败返回NULLvoid free(void* ptr)跳转 goto语句#include <setjmp.h>int longjmp(jmp_buf env) setjmp(jmp_buf env, int val)跳转时变量的"回滚"查询和更改进程的资源限制getrlimit()和setrlimit()【第8章 进程控制】一、进程标识——进程ID非负整数ID=0 交换进程(swapper)负责调度,内核的一部分,系统进程ID=1 init进程/etc/init或/sbin/init,读与系统有关的初始化文件/etc/rc*ID=2 精灵进程(pagedaem on),系统进程二、fork()#include <sys/types.h>#include <unistd.h>pid_t fork(void);返回:子进程中为0,父进程中为子进程ID,出错为-1子进程是父进程的复制品;写时复制(Copy-On-Wirte,COW)技术三、vfork()#include <vfork.h>pid_t vfork();没有copy-on-write,调用exec之前在父进程的空间里运行四、进程终止正常终止 exit()<=>return异常终止 abort() 产生SIGABRT信号终止后将其终止状态返回给父进程,父进程在子进程之前终止则子进程被init进程领养僵死(zomebie)进程:一个已经终止但其父进程没有对终止的子进程做善后处理(释放子进程占有的资源)的进程。是一种临时的状态///main(){ for (;;) /*制作一个死循环*/ fork(); /*开始创建一个子进程*/}//五、wait()函数#include <sys/types.h>#include <sys/wait.h>pid_t wait(int* statloc);pid_t waitpid(pid_t pid, int* statloc, int options);成功返回进程ID,失败-1WIFEXITED(status)检测进程状态pid = -1 等待任一子进程pid > 0 等待其进程与pid相等pid =0 等待其组ID,等于调用进程的组ID的任一子进程pid < -1 等待其组ID进程中的绝对值等于pid的进程options WNOHANG 不阻塞 立即返回 WUNTRACED支持组作业#include <sys/types.h>#include <sys/wait.h>#include <sys/time.h>#include <sys/resource.h>pid_t wait3(int* statloc, int options, struct rusage* rusage);pid_t wait4(pid_t pid, int* statloc, int options, struct rusage* rusage)参考手册 getrusage(2)六、竞争条件当多个进程都企图对共享数据进行某种处理,而最后的结果又取决于进程运行的顺序时,则我们认为这发生了竞态条件( race condition)。定期询问(polling)while(getppid() != 1) sleep(1);信号机制 和 各种形式的进程间通信(IPC)///伪代码 TELL_WAIT(); /* set things up for TELL_XXX & WAIT_XXX */ if (0 > (pid = fork)) { //fork error } else if (0 == pid) { //child does whatever is neccessary ... TELL_PARENT(getpid()); //tell parent we are done WAIT_PARENT(); //and wait for parent //and child continues on its way ... exit(0); } //parent does whatever is neccessary ... TELL_CHILD(pid); //tell child we are done WAIT_CHILD(); //and wait for child //and parent continues on its way ... exit(0);七、exec()函数#include <unistd.h>int execl(const char* pathname, const char* arg0, ... /*(char*) 0*/);int execv(const char* pathname, char* constgarv[]);int execle(const char* pathname, const char* arg0, ... /*(char*)0, char* const envp[]*/);int execve(const char* pathname, char* const argv[], char* const envp[]);//系统调用int execlp(const char* filename, const char* arg0, ... /*(char*)0*/);int execvp(const char* filename, char* const argv[]);p表示filename做参数, l表示取一个参数表与v互斥,v表示argv[],e表示envp[]char* const envp[](e.g. "path=/home/jack")八、更改用户ID和组ID#include <sys/types.h>#include <unistd.h>//设置int setuid(uid_t uid);int setgid(gid_t gid);//切换int setreuid(uid_t* uid, uid_t euid);int setregid(gid_t* gid, gid_t egid);//更改有效idint seteuid(uid_t uid);int setgid(gid_t gid);九、解释器文件1 解释器文件是一个文本文件2 起始行形式为!# pathname [optional-agrument]3 本质:执行起始行的程序并把本身当作参数传递给起始行的那个程序4 awke.g. awkeg#! awk -fBEGIN{ for(i = 0; i < argv; i++) printf "ARGV[%d] = %s", i, ARGV[i] exit}5 好处:隐藏某些执行文件是用脚本编写的事实;提高效率;可以使用除/bin/sh以外的其他shell来编写shell脚本十、system函数#include <stdlib.h>int system(const char* cmdstring)系统依赖性很强e.g. system("date > file");e.g. system("ls -a");十一、进程结束后,内核会写一个进程的会计记录u_short compt 在<sys/acct.h>中struct acct{ char ac_flag; /* Accounting flags. */ u_int16_t ac_uid; /* Accounting user ID. */ u_int16_t ac_gid; /* Accounting group ID. */ u_int16_t ac_tty; /* Controlling tty. */ u_int32_t ac_btime; /* Beginning time. */ comp_t ac_utime; /* Accounting user time. */ comp_t ac_stime; /* Accounting system time. */ comp_t ac_etime; /* Accounting elapsed time. */ comp_t ac_mem; /* Accounting average memory usage. */ comp_t ac_io; /* Accounting chars transferred. */ comp_t ac_rw; /* Accounting blocks read or written. */ comp_t ac_minflt; /* Accounting minor pagefaults. */ comp_t ac_majflt; /* Accounting major pagefaults. */ comp_t ac_swaps; /* Accounting number of swaps. */ u_int32_t ac_exitcode; /* Accounting process exitcode. */ char ac_comm[ACCT_COMM+1]; /* Accounting command name. */ char ac_pad[10]; /* Accounting padding bytes. */};accton命令/sbin/accton filename turn onTO DO STH/sbin/accton turn off十二、用户标识#include <unistd.h>char* getlogin(void); //得到用户名十三、进程时间#include <sys/times.h>/* Structure describing CPU time used by a process and its children. */struct tms{ clock_t tms_utime; /* User CPU time. */ clock_t tms_stime; /* System CPU time. */ clock_t tms_cutime; /* User CPU time of dead children. */ clock_t tms_cstime; /* System CPU time of dead children. */};clock_t times(struct tms* buf)