fork
进程标志
#include <unistd.h>
#include <sys/types.h>
uid_t getpid(void);
uid_t getppid(void);
uid_t geteuid(void);
gid_t getgid(void);
git_t getegid(void);
struct passed *getpwuid(uid_t uid);
struct passwd {
char *pw_name; /* 登录名称 */
char *pw_passwd; /* 登录口令 */
uid_t pw_uid; /* 用户 ID */
gid_t pw_gid; /* 用户组 ID */
char *pw_gecos; /* 用户的真名 */
char *pw_dir; /* 用户的目录 */
char *pw_shell; /* 用户的 SHELL */
};
#include <pwd.h>;
#include <sys/types.h>;
fork
pid_t fork();
当 fork 掉用失败的时候(内存不足或者是用户的最大进程数已到)fork 返回-1.父进程中返回子进程id,子进程返回0.
#include <sys/types.h>;
#include <sys/wait.h>;
pid_t wait(int *stat_loc);
pid_t waitpid(pid_t pid,int *stat_loc,int options);
两个系统调用用于等待子进程状态改变,并获取状态信息。状态改变包含: the child terminated; the child was stopped by a signal; or the child was resumed by a signal. 若没有调用wait或waitpid子进程变为僵尸进程。
两函数阻塞直到子进程状态改变或两函数被中断。若waitpid设置WNOHANG,父进程不阻塞直接返回。
stat_loc保存子进程退出状态(一般exit,_exit,return)。
wait <==> waitpid(-1, &status, 0);
RETURN VALUE wait(): on success, returns the process ID of the terminated child; on error, -1 is returned.
waitpid(): on success, returns the process ID of the child whose state has changed; if WNOHANG was specified and one or more child(ren) specified by pid exist, but have not yet changed state, then 0 is returned.On error, -1 is returned.
僵尸进程危害:
1)占用资源(少量);2)难以清除(杀死僵尸进程父进程)。
如果一个父进程终止,而子进程还存在(仍运行或僵尸进程),子进程的父进程改为init进程,子进程终止时,init调用wait函数清理它。
原文:http://www.cnblogs.com/embedded-linux/p/5011222.html