日期:2014-05-16  浏览次数:20748 次

使用vfork(),后用return 出现段错误问题
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
main()
{
   pid_t pid;
   int count=0;
   pid=vfork();
    count++;    
   printf("count=%d, pid=%d\n",count,getpid());
    return(0);
}
运行出现count=1,pid=25127
        count=1, pid=25126 后又显示“段错误”
尝试把retrurn(0); 改为 exit(0);之后运行结果  count=1;pid=25145
                                            count=2;pid=25144
查看了有关return() 和 exit()还是不理解,求解释!!!!!!

------解决方案--------------------
NAME
       vfork - create a child process and block parent

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

       pid_t vfork(void);

STANDARD DESCRIPTION
       (From  XPG4  /  SUSv2  /  POSIX draft.)  The vfork() function has the same effect as fork(), except that the behaviour is undefined if the process created by
       vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or  returns  from  the  function  in  which
       vfork() was called, or calls any other function before successfully calling _exit() or one of the exec family of functions.

ERRORS
       EAGAIN Too many processes - try again.

       ENOMEM There is insufficient swap space for the new process.

LINUX DESCRIPTION
       vfork, just like fork(2), creates a child process of the calling process.  For details and return value and errors, see fork(2).

       vfork()  is  a special case of clone(2).  It is used to create new processes without copying the page tables of the parent process.  It may be useful in per-
       formance sensitive applications where a child will be created which then immediately issues an execve().

       vfork() differs from fork in that the parent is suspended until the child makes a ca