#include"errno.h"
#include"unistd.h"
#include"stdlib.h"
int main(void)
{
pid_t pid;
int count=0;
if((pid=vfork())==-1)
{
printf("vfork error:%s\n",strerror(errno));
exit(1);
}
count++;
printf("count=%d\n",count);
return(0);
}
执行结果为:
count=1
count=1
段错误
而把return(0)换成exit(0)后
就能顺利执行呢?此时结果为
count=1
count=2
期待各位大侠回答
14 个解决方案
#1
return 函数返回。。结束一个函数
exit是抛出异常。。。结束整个进程
exit是抛出异常。。。结束整个进程
#2
#include"stdio.h"
#include"errno.h"
#include"unistd.h"
#include"stdlib.h"
int main(void)
{
pid_t pid;
int count=0;
if((pid=vfork())==-1)
{
perror("vfork()");
exit(1);
}
count++;
printf("%d\n",getpid());
printf("count=%d\n",count);
exit (0);
}
return 0时,一部分结果
22985
count=1
15974
count=2
22986
count=1
15974
count=2
22987
count=1
15974
count=2
22988
count=1
15974
count=2
22989
count=1
15974
count=2
22990
count=1
15974
count=2
22991
count=1
15974
count=2
22992
count=1
15974
exit 0时的结果:
23037
count=1
23036
count=2
return 0只是告诉linux main函数的返回值是0
由于创建进程时,没结束进程的运行,父子进程会一直运行下去
exit 0表示正常退出函数
#3
return(0) 在main函数中返回是等同于exit(0)的,exit(0) 无非就是做下清理工作
ls说上父进程会一直跑下去?测试过没?
ls说上父进程会一直跑下去?测试过没?
#4
Parent and child processes share the same stack space within the
[vfork,exec] window. If the size of the stack has been changed
within this window by the child process (return from or call to a
function, for example), it is likely that the parent and child
processes will be killed with signal SIGSEGV or SIGBUS.
[vfork,exec] window. If the size of the stack has been changed
within this window by the child process (return from or call to a
function, for example), it is likely that the parent and child
processes will be killed with signal SIGSEGV or SIGBUS.
#6
exit(0) 情况下正常运行可以理解..
return(0) 手册中解释了..
return(0) 手册中解释了..
#7
vfork() differs from fork(2) in that the parent is suspended until the child
terminates (either normally, by calling _exit(2), or abnormally, after
delivery of a fatal signal), or it makes a call to execve(2). Until that
point, the child shares all memory with its parent, including the stack. The
child must not return from the current function or call exit(3), but may call
_exit(2).
terminates (either normally, by calling _exit(2), or abnormally, after
delivery of a fatal signal), or it makes a call to execve(2). Until that
point, the child shares all memory with its parent, including the stack. The
child must not return from the current function or call exit(3), but may call
_exit(2).
#8
在其他函数中return(0)和exit(0)容易区别,就是在main()函数中这两者概念比较模糊。
#9
正如4楼所说,问题在于vfork.
如果没有使用vfork(), 在main函数中return (0)和exit(0)是没有区别的。
#10
嗯,明白了许多。
#11
vfork是在子进程结束后父进程才继续运行,这个和fork是有区别的。另外,vfork并不完全复制父进程的地址空间。结合这些好好考虑下。
#12
#13
父进程挂起了
而子进程没结束
#14
还是没怎么懂
#1
return 函数返回。。结束一个函数
exit是抛出异常。。。结束整个进程
exit是抛出异常。。。结束整个进程
#2
#include"stdio.h"
#include"errno.h"
#include"unistd.h"
#include"stdlib.h"
int main(void)
{
pid_t pid;
int count=0;
if((pid=vfork())==-1)
{
perror("vfork()");
exit(1);
}
count++;
printf("%d\n",getpid());
printf("count=%d\n",count);
exit (0);
}
return 0时,一部分结果
22985
count=1
15974
count=2
22986
count=1
15974
count=2
22987
count=1
15974
count=2
22988
count=1
15974
count=2
22989
count=1
15974
count=2
22990
count=1
15974
count=2
22991
count=1
15974
count=2
22992
count=1
15974
exit 0时的结果:
23037
count=1
23036
count=2
return 0只是告诉linux main函数的返回值是0
由于创建进程时,没结束进程的运行,父子进程会一直运行下去
exit 0表示正常退出函数
#3
return(0) 在main函数中返回是等同于exit(0)的,exit(0) 无非就是做下清理工作
ls说上父进程会一直跑下去?测试过没?
ls说上父进程会一直跑下去?测试过没?
#4
Parent and child processes share the same stack space within the
[vfork,exec] window. If the size of the stack has been changed
within this window by the child process (return from or call to a
function, for example), it is likely that the parent and child
processes will be killed with signal SIGSEGV or SIGBUS.
[vfork,exec] window. If the size of the stack has been changed
within this window by the child process (return from or call to a
function, for example), it is likely that the parent and child
processes will be killed with signal SIGSEGV or SIGBUS.
#5
#6
exit(0) 情况下正常运行可以理解..
return(0) 手册中解释了..
return(0) 手册中解释了..
#7
vfork() differs from fork(2) in that the parent is suspended until the child
terminates (either normally, by calling _exit(2), or abnormally, after
delivery of a fatal signal), or it makes a call to execve(2). Until that
point, the child shares all memory with its parent, including the stack. The
child must not return from the current function or call exit(3), but may call
_exit(2).
terminates (either normally, by calling _exit(2), or abnormally, after
delivery of a fatal signal), or it makes a call to execve(2). Until that
point, the child shares all memory with its parent, including the stack. The
child must not return from the current function or call exit(3), but may call
_exit(2).
#8
在其他函数中return(0)和exit(0)容易区别,就是在main()函数中这两者概念比较模糊。
#9
正如4楼所说,问题在于vfork.
如果没有使用vfork(), 在main函数中return (0)和exit(0)是没有区别的。
#10
嗯,明白了许多。
#11
vfork是在子进程结束后父进程才继续运行,这个和fork是有区别的。另外,vfork并不完全复制父进程的地址空间。结合这些好好考虑下。
#12
#13
父进程挂起了
而子进程没结束
#14
还是没怎么懂