发新话题
打印

为什么FORK函数返回两次救命

为什么FORK函数返回两次救命

为什么FORK函数,被调用过一次,却返回两个值?( N- t1 f; I# \0 g( P6 y6 p
是不是,难道,FORK函数,调用的时候被执行过了,这个时候,有两个进程,所以,有两个返回值,?为什么      

TOP

复制内容到剪贴板
代码:
FORK(2)                    Linux Programmer's Manual                   FORK(2)

NAME
       fork - create a child process

SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>

       pid_t fork(void);

DESCRIPTION
       fork  creates a child process that differs from the parent process only
       in its PID and PPID, and in the fact that resource utilizations are set
       to 0.  File locks and pending signals are not inherited.

       Under Linux, fork is implemented using copy-on-write pages, so the only
       penalty incurred by fork is the time and memory required  to  duplicate
       the parent's page tables, and to create a unique task structure for the
       child.

RETURN VALUE
       [color=red]On success, the PID of the child process is returned  in  the  parent's
       thread  of execution, and a 0 is returned in the child's thread of exe-
       cution.  On failure, a -1 will be returned in the parent's context,  no
       child process will be created, and errno will be set appropriately.[/color]

ERRORS
       EAGAIN fork cannot allocate sufficient memory to copy the parent's page
              tables and allocate a task structure for the child.

       ENOMEM fork failed to allocate the necessary kernel structures  because
              memory is tight.

CONFORMING TO
       The fork call conforms to SVr4, SVID, POSIX, X/OPEN, BSD 4.3.

SEE ALSO
       clone(2), execve(2), vfork(2), wait(2)

Linux 1.2.9                       1995-06-10                           FORK(2)
      
'
梅须逊雪三分白 雪却输梅一段香

TOP

in the fact that resource utilizations are set
% o: T  t. k5 P! I! V7 ?# X       to 0.  File locks and pending signals are not inherited$ I0 f, f# _4 z" j7 N8 F
怎么理解      

TOP

[QUOTE=dearvoid]
复制内容到剪贴板
代码:
FORK(2) Linux Programmer's Manual FORK(2)

NAME
fork - create a child process

SYNOPSIS
#include <sys/types.h>
#include <unistd.h>

pid_t fork(void);

DESCRIPTION
fork creates a child process that differs from the parent process only
in its PID and PPID, and in the fact that resource utilizations are set
to 0. File locks and pending signals are not inherited.

Under Linux, fork is implemented using copy-on-write pages, so the only
penalty incurred by fork is the time and memory required to duplicate
the parent's page tables, and to create a unique task structure for the
child.

RETURN VALUE
[color=red]On success, the PID of the child process is returned in the parent's
thread of execution, and a 0 is returned in the child's thread of exe-
cution. On failure, a -1 will be returned in the parent's context, no
child process will be created, and errno will be set appropriately.[/color]

ERRORS
EAGAIN fork cannot allocate sufficient memory to copy the parent's page
tables and allocate a task structure for the child.

ENOMEM fork failed to allocate the necessary kernel structures because
memory is tight.

CONFORMING TO
The fork call conforms to SVr4, SVID, POSIX, X/OPEN, BSD 4.3.

SEE ALSO
clone(2), execve(2), vfork(2), wait(2)

Linux 1.2.9 1995-06-10 FORK(2)
[/QUOTE]in the fact that resource utilizations are set
( p; v- ~3 y. G  Lto 0. File locks and pending signals are not inherited怎么理解 ?      

TOP

你理解什么是 resource, file lock 和 pending signal 吗?      
'
梅须逊雪三分白 雪却输梅一段香

TOP

[QUOTE=dearvoid]你理解什么是 resource, file lock 和 pending signal 吗?[/QUOTE]不理解,斑竹,能仔细讲一下吗,教一下小弟,谢谢....;)      

TOP

基础问题 论坛上讲不清楚 建议自己找些资料看看      
'
梅须逊雪三分白 雪却输梅一段香

TOP

[QUOTE=dearvoid]基础问题 论坛上讲不清楚 建议自己找些资料看看[/QUOTE]谢谢斑竹,有找过,但找不到,想要的答案,斑竹,能给我一个网址吗      

TOP

resource 资源,比如说,没有 cpu 时间片,你的进程是不能运行的
  M5 C7 y: W2 K5 l7 }+ }
1 H/ _% l1 J& L8 C1 {file lock 文件锁,你的进程打开一个文件,读了它的内容,然后准备往里面写点什么,这时候你总是要给他加个锁的,要不然别的进程也往里面写点儿什么不就乱套了吗?如果这时候你 fork () 了一下,子进程要不要
% C% f: E( ?2 U9 c6 {把这个 file lock 也继承了?当然不能,否则你这两个进程就谁也别干了
7 n) W6 `& @: i2 h- G' e# a0 \' ?$ l6 C' P8 f
pending signal 未处理的信号,我给你的进程 kill 了一下,不过这个 signal 的处理需要时间,在系统没有
% w, R8 f+ a& G- v& \7 J9 m把这个 signal 提交给你的进程的时候,你 fork () 成功了,那么,这个信号系统该怎么办?给你的两个进程
" d) l4 x! |  e$ w一人一下?不行,那样也容易乱套,还是直接给你原来那个进程比较好      

TOP

fork()==0时进程为子进程,return值忽略,应该用exit(),退出码由wait()或waitpid()获取      

TOP

发新话题