首页 > 其他 > 详细

fork

时间:2015-12-01 21:07:41      阅读:302      评论:0      收藏:0      [点我收藏+]

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函数清理它。

fork

原文:http://www.cnblogs.com/embedded-linux/p/5011222.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!