Chinaunix首页 | 论坛 | 博客
  • 博客访问: 248859
  • 博文数量: 44
  • 博客积分: 1052
  • 博客等级: 少尉
  • 技术积分: 742
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-17 16:51
文章分类

全部博文(44)

文章存档

2013年(7)

2012年(14)

2011年(23)

分类: LINUX

2012-08-12 10:47:42

进程控制

主要内容包括:进程创建,进程执行,进程终止。

进程ID

点击(此处)折叠或打开

  1. #include <unistd.h>

  2. pid_t getpid(void)
  3. pid_t getppid(void);
  4. uid_t getuid(void);
  5. uid_t geteuid(void);
  6. gid_t getgid(void);
  7. gid_t getegid(void);
返回相应的ID,关于real user ID和effective user ID

进程创建

  1. #include <unistd.h>
  2. pid_t fork(void);
  3.                 // returns: 0 in child, process ID of child in parent, -1 on error.
子进程会复制父进程的data space, heap and stack, 与父进程共享text segment.

  1. fork之后,parent和child之间相同的部分
  2. > real user ID, real group ID, effective user ID, effective group ID
  3. > Supplementary group IDs
  4. > Process group ID
  5. > Session ID
  6. > Controlling terminal
  7. > The set-user-ID and set-group-ID flags
  8. > current working directory
  9. > Root directory
  10. > File mode creation mask
  11. > Signal mask and dispositions
  12. > The close-on-exec flag for any open file descriptors
  13. > Environment
  14. > Attached shared memory segments
  15. > memory mappings
  16. > Resource limits

  17. 不同的部分
  18. > The return value from fork
  19. > The process IDs are different
  20. > The tow processes have different parent process IDs
  21. > The child's tms_utime, tms_stime, tms_cutime, tms_cstime值被设为0
  22. > File locks set by the parent are not inherited by the child
  23. > Pending alarms are cleared for the child
  24. > The set of pending signals for the child is set to the empty set

vfork函数
与fork函数类似, 创建子进程. 但子进程与父进程共享空间, 而且vfork保证子进程先开始执行.

exit函数
当一个进程调用exit系列函数, 但该进程的父进程已经结束, 这是init进程成为该进程的父进程.  只有当父进程调用wait系列函数之后, 一个进程才会完全结束, 在此之前该进程为僵尸进程(zombie)

wait系列函数
当进程退出时, 内核通过发送SIGCHLD信号通知其父进程, 父进程可以处理该信号或不处理.

  1. #include <sys/wait.h>

  2. pid_t wait(int *statloc);
  3. pid_t waitpid(pid_t pid, int *statloc, int options);
  4. pid_t waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);

  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <sys/time.h>
  8. #include <sys/resource.h>

  9. pid_t wait3(int *statloc, int options, struct rusage *rusage);
  10. pid_t wait4(pid_t pid, int *statloc, int options, struct rusage *rusage);
进程调用wait或waitpid会产生下面的后果:
i) 阻塞, 如果全部的子进程仍在运行.
ii) 立即返回一个子进程的termination status (注意不是exit status)
iii) 返回错误, 如果没有子进程.

waitpid的pid参数,
pid == -1  : 等待任何子进程
pid > 0     : 等待pid子进程
pid == 0   : 等待任何process group ID与父进程相同的子进程
pid < -1    : 等待任何process group ID与|pid|相同的子进程.

options参数:
WCONTINUED : 
WNOHANG       : 此时如果子进程未退出, waitpid也不会阻塞
WUNTRACED   : 

当wait或waitpid正常执行后, statloc指向的对象中包含了子进程的termination status. 利用下面的宏来取得相应的信息.

点击(此处)折叠或打开

  1. WIFEXITED(status) : 如果子进程正常终止,返回True。然后可以调用WEXITSTATUS(status)得到子进程的exit status。

  2. WIFSIGNALED(status) : 如果子进程因为signal异常终止,返回True,然后调用WTERMSIG(status)得到signal number

  3. WIFSTOPPED(status) : 如果子进程已经stop,返回True,用WSTOPSIG(status),取得使进程stop的信号。

  4. WIFCONTINUED(status) : 如果子进程又继续执行,返回True。

exec系列函数

  1. #include <unistd.h>

  2. int execl(const char *pathname, const char *arg0, ... /* (char *) 0 */);
  3. int execv(const char *pathname, char *const argv[]);
  4. int execle(const char *pathname, char *arg0, ... /* (char *) 0, char *const envp[] */)
  5. int execve(const char *pathname, char *const argv[], char *const envp[]);
  6. int execlp(const char *filename, const char *arg0, ... /* (char *) 0 */ );
  7. int execvp(const char *filename, char *const argv[]);

  8. l : list arguments
  9. v : vector arguments
  10. p : use PATH environment
  11. e : environment
注意不存在同时指定pe的exec函数.







阅读(1341) | 评论(0) | 转发(1) |
0

上一篇:二分搜索

下一篇:grep命令

给主人留下些什么吧!~~