jixuduxing

linux学习笔记(7)

第六章:系统数据文件和信息
6.2 口令文件
#include <pwd.h>
struct passwd * getpwuid(uid_t uid);
struct passwd * getpwnam(const char * name);


struct passwd * getpwent(void);
void setpwent(void);
void endpwent(void);
这三个函数用来获取指令文件对应每个用户的信息;

6.3 阴影文件
加密口令存放在阴影口令文件中  /etc/shadow
该文件只有root用户可以访问;
访问该文件的函数
#include <shadow.h>
struct spwd *getspnam(const char * name);
struct spwd *getspent(void);

void setspent(void);
void endspent(void);

6.4 组文件
#include <grp.h>
struct group *getgrgid(gid_t gid);
struct group *getgrnam(const char *name);

struct group *getgrent(void);
void setgrent(void);
void endgrent(void);

6.5 附加组id
每个用户可以有多个组
拥有这些组的权限;

#include <unistd.h>

int getgroups(int gidsetsize, gid_t grouplist[]);

Returns: number of supplementary group IDs if OK, 1 on error
 
#include <grp.h>     /* on Linux */
#include <unistd.h>  /* on FreeBSD, Mac OS X, and
 Solaris */

int setgroups(int ngroups, const gid_t grouplist[]);

#include <grp.h>     /* on Linux and Solaris */
#include <unistd.h>  /* on FreeBSD and Mac OS X */

int initgroups(const char *username, gid_t basegid);



以上
get方法都表示读下一条记录
set 表示打开该数据文件;
end 表示关闭该数据文件;
6.9
#include <sys/utsname.h>

int uname(struct utsname *name);
获取系统信息
#include <unistd.h>

int gethostname(char *name, int namelen);
获取主机名

6.10 时间和日期例程

#include <time.h>

time_t time(time_t *calptr);
获取当前时间(相对于1970年1月0日0分)
#include <sys/time.h>

int gettimeofday(struct timeval *restrict tp, void
 *restrict tzp);
获取秒和微妙

#include <time.h>

struct tm *gmtime(const time_t *calptr);
//转化为国际时间
struct tm *localtime(const time_t *calptr);
//转化为本地时间
#include <time.h>

time_t mktime(struct tm *tmptr);
//反转

The asctime and ctime functions produce the familiar 26-byte string that is similar to the default output of the date(1) command:

    Tue Feb 10 18:27:38 2004\n\0

#include <time.h>

char *asctime(const struct tm *tmptr);

char *ctime(const time_t *calptr);
 

 #include <time.h>

size_t strftime(char *restrict buf, size_t maxsize,
                const char *restrict format,
                const struct tm *restrict tmptr);
//自定义格式化时间
Chapter 7. Process Environment 
7.2 main函数
每个进程都是由一个起动例程启动的。
7.3 程序终止
#include <stdlib.h>

void exit(int status);

void _Exit(int status);

#include <unistd.h>

void _exit(int status);
_exit and _Exit, which return to the kernel immediately 立即进入内核,关闭程序
exit, which performs certain cleanup processing and then returns to the kernel.
先做清理处理(执行终止处理程序,关闭所有标准io流等),对于所有的打来的流,执行fclose操作

大多数unix shell提供一个检查一个进程终止状态的方法,如果a)该函数返回时不带终止状态,b)执行不带返回值的return语句,c)main隐式返回,则该进程的终止状态是未定义的;


#include <stdlib.h>

int atexit(void (*func)(void));
终止处理函数注册函数
比如 atexit(my_exit2)

注:main函数中 exit (0)等价于return (0);
7.4 命令行参数

7.5 环境表
Access to specific environment variables is normally through the getenv and putenv functions

7.6. Memory Layout of a C Program 
c程序的存储空间布局
Historically, a C program has been composed of the following pieces:
1、Text segment, 
2、Initialized data segment
3、Uninitialized data segment
4、Stack
5、Heap

7.7. Shared Libraries 
共享库
就是动态连接库
cc -static a.c
静态编译

7.8. Memory Allocation 
存储分配

#include <stdlib.h>

void *malloc(size_t size);

void *calloc(size_t nobj, size_t size);

void *realloc(void *ptr, size_t newsize);
 
All three return: non-null pointer if OK, NULL on error
 
void free(void *ptr);
以上都是在堆中分配

alloca 在栈中分配

7.9 环境变量
  #include <stdlib.h>

  int putenv(char *str);

  int setenv(const char *name, const char *value,
 int rewrite);

  int unsetenv(const char *name);
7.10 setjmp和longjmp
非局部跳转函数,goto是局部跳转的,无法跨越函数实现跳转
#include <setjmp.h>

int setjmp(jmp_buf env);

Returns: 0 if called directly, nonzero if returning from a call to longjmp 
 
void longjmp(jmp_buf env, int val);
val表示第几次调用,传递该参数后,从此处跳转,setjmp会返回对应的val;
注意:使用的时候,可能会对自动变量,寄存器变量和易失变量产生影响,导致其值“看情况”,在使用的时候,必需使用validate属性;
 
一个文件存储的时候主要包含三块:
数据块  i节点   目录项
i节点是唯一的,对应一个或者几个数据块,同时对应一个或者几个目录块
对一个文件创建一个硬链接,产生新的目录项,然后这个目录项指向了原来文件对应的i节点
目录项主要包含两个属性:i节点号和文件名;

但是对于创建一个软链接来说,不仅产生了新的目录项,也产生了新的i节点和数据块,该数据块存储的
内容就是原文件的文件名,需要注意的是,不同的命令对符号链接的处理不一样,有些“跟随”,有些不
“跟随”;

1、访问符号链接的效率应该比硬链接要低那么一点点,因为牵扯一个跟随的过程:
从目录块到i节点,然后到数据块,读取文件内容,然后找到被链接接的文件的目录块,再到i节点,访
问数据块;但是对于硬链接来说的过程是:目录块-》i节点-》数据块即可,跟访问被连接文件的过程是完全一致的;
2、硬链接的使用存在限制:只能在一个文件系统之中使用,因为对于一个文件而言,数据库,i节点,目录项必须存在在一个文件系统之中,甚至应该是存在一个文件系统的一个柱面组之中;
附:硬盘、分区、文件系统的关系:
硬盘分为N个分区,每个分区包含一个文件系统,文件系统由自举块、超级块和M个柱面组组成,而每个柱面组分为超级副本块、配置信息、i节点图,块位图、i节点数组、数据块数组,数据块中穿插着分布目录块,一个文件就存放在该柱面之中,目录块、数据块、i节点分别存储;这也导致硬链接必须在该柱面之中,方可有链接关系,但是软链接就没有这种限制,因为它是通过文件内容和文件属性来跟随所链接的文件并访问其数据,放在哪里都是一样;
3、i节点存储的有文件的链接数,这个链接是硬链接,初始为1,每增加一个硬链接,该数+1,删除一个硬链接,该数-1,该数为0时释放数据块;

08 进程控制
8.2 进程标示
#include <unistd.h>

pid_t getpid(void);
Returns: process ID of calling process

pid_t getppid(void);
Returns: parent process ID of calling process

uid_t getuid(void);
Returns: real user ID of calling process
 
uid_t geteuid(void);

Returns: effective user ID of calling process
 
gid_t getgid(void);
Returns: real group ID of calling process

gid_t getegid(void);

Returns: effective group ID of calling process
注;以上函数都没有出错返回

特殊进程:
id=0  系统进程(调度进程)(交换进程)  内核的一部分,不执行磁盘上的任何程序
id=1  init进程 在自举过程中由内核调用  相当于初始化进程,不是内核中的系统进程,而是普通的用户进程,以超级用户特权运行,会接管所有的野进程;
8.3 fork函数
An existing process can create a new one by calling the fork function.
#include <unistd.h>

pid_t fork(void);

posted on 2010-05-05 09:41 高磊 阅读(174) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜