UNIX环境高级编程--知识点整理(第7.8.9章)

第7章进程环境

1.             

5种正常终止进程的方法:

1)  从main返回;

2)  调用exit;

3)  调用_exit或_Exit;

4)  最后一个线程从其启动例程返回;

5)  最后一个线程调用pthread_exit。

3种异常终止进程的方法:

1)  调用abort;

2)  接到一个信号并终止;

3)  最后一个线程对取消请求做出响应。

 

2.

_exit和_Exit立即进入内核,exit先执行一些清理处理,然后进入内核。

#include <stdlib.h>

Void exit(int status);

Void _Exit(int status);

#include <unistd.h>

Void _exit(int status);

 

3.

登记终止处理程序:

#include <stdlib.h>

Int atexit(void (*func)(void));           /* 登记多次,则会调用多次 */

 

4.

存储器分配:

#include <stdlib.h>

Void *malloc(size_t size);         /* 分配指定字节数的存储区,初始值不定 */

Void *calloc(size_t nobj, size_t size);      /* 分配指定数量,指定长度的存储区,初始值为0*/

Void *realloc(void *ptr, size_t newsize);                   /* 更改以前的分配区 */

Void free(void *ptr);

 

5.

取环境变量:

#include <stdlib.h>

Char *getenv(const char *name);  /* 返回与name关联的value的指针,未找到返回null*/

 

6.设置环境变量:

#include <stdlib.h>

Int putenv(char *str);      /* name = value */

Int setenv(const char *name, const char*value, int rewrite);  /* rewrite=0 不删除现有定义 */

Int unsetenv(const char *name);    /* 删除name的定义,即使不存在也不报错*/

 

7.

跳转功能:

#include <setjmp.h>

Int setjmp(jmp_buf env);

Void longjmp(jmp_buf env, int val);

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

jmp_buf jmpbuffer;

int main(void)

{

         Charline[MAXLINE];

         If(setjmp(jmpbuffer) != 0)

                   Printf(“error”);

         While(fgets(line, MAXLINE, stdin) != NULL)

                   Do_line(line);

         exit(0);

}

Void cmd_add(void);

{

         …

         If(token < 0)

                   longjmp(jmpbuffer,1);

}

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

 

8.

每个进程都有一组资源限制,可用getrlimit和setrlimit函数查询和更改:

#include <sys/resource.h>

Int getrlimit(int resource, struct rlimit*rlptr);

Int setrlimit(int resource, const structrlimit *rlptr);

Struct rlimit

{

         rlim_trlim_cur;

         rlim_trlim_max;

};

 

第8章进程控制

1.

进程标识符:

#include <unistd.h>

Pid_t getpid(void);      // 进程ID

Pid_t getppid(void);     // 父进程ID

Uid_t getuid(void);             // 实际用户ID

Uid_t geteuid(void);          // 有效用户ID

Gid_t getgid(void);             // 实际组ID

Gid_t getegid(void);          // 有效组ID

 

2.

创建子进程:

#include <unistd.h>

Pid_t fork(void);        // 子进程返回0,父进程返回子进程ID,出错返回-1

Pid_t vfork(void);  // 不将父进程的地址空间完全复制到子进程中,保证子进程先于父进程运行

 

3.

#include <sys/wait.h>

Pid_t wait(int *statloc);            // 一个子进程返回则返回

Pid_t waitpid(pid_t pid, int *statloc, intoptions);  // 子进程pid返回,才返回

 

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

等待父进程终止:

While (getppid() != 1)

Sleep(1);

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

 

4.

Exec函数:

Int execl(const char *pathname, const char*arg0, … /* (char *)0 */);

Int execv(const char *pathname, char *constargv[]);

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[]);

 

5.

设置实际用户ID和有效ID:

#include <unistd.h>

Int setuid(uid_t uid);

Int setgid(gid_t gid);

 

6.

交换实际用户ID和有效用户ID:

#include <unistd.h>

Int setreuid(uid_t ruid, uid_t euid);

Int setregid(gid_t rgid, gid_t egid);

 

7.

对一个字符串进行分析并执行:

#include <stdlib.h>

Int system(const char *cmdstring);

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

System(“date > file”);

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

 

8.

获取用户登录名:

#include <unistd.h>

Char *getlogin(void);

 

9.

获取进程时间:

#include <sys/times.h>

Clock_t times(struct tms *buf);

Struct tms

{

         Clock_ttms_utime;                   // user CPUtime

         Clock_ttms_stime;          // system CPU time

         Clock_ttms_cutime;        // user CPU time,terminated children

         Clock_ttms_cstime;        // system CPU time,terminated children

}

 

第9章进程关系

1.

进程组ID:

获取:

#include <unistd.h>

Pid_t getpgrp(void);

Pid_t getpgid(pid_t pid);         // 若pid为0, 则等价于getpgrp()

设置:

Int setpgid(pid_t pid, pid_t pgid);

 

2.

建立新会话:

#include <unistd.h>

Pid_t setsid(void);    // 成功返回进程组ID,出错返回-1

#include <unistd.h>

Pid_t getsid(pid_t pid);             // 返回会话首进程的进程组ID

 

3.

前台进程组:

#include <unistd.h>

Pid_t tcgetpgrp(int filedes);

Pid_t tcsetpgrp(int filedes, pid_t pgrpid);

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值