当前位置: 技术问答>linux和unix
使用vfork(),后用return 出现段错误问题
来源: 互联网 发布时间:2017-03-15
本文导语: #include #include #include main() { pid_t pid; int count=0; pid=vfork(); count++; printf("count=%d, pid=%dn",count,getpid()); return(0); } 运行出现count=1,pid=25127 count=1, pid=25126 后又显示“段错误...
#include
#include
#include
main()
{
pid_t pid;
int count=0;
pid=vfork();
count++;
printf("count=%d, pid=%dn",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()还是不理解,求解释!!!!!!
#include
#include
main()
{
pid_t pid;
int count=0;
pid=vfork();
count++;
printf("count=%d, pid=%dn",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
#include
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 call to execve(2) or _exit(2). The child shares all memory with its par-
ent, including the stack, until execve() is issued by the child. The child must not return from the current function or call exit(), but may call _exit().
Signal handlers are inherited, but not shared. Signals to the parent arrive after the child releases the parent.
自己读?
|
#include
#include
#include
int main(int argc, char ** argv )
{
int pid = fork();
if (pid