#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int createproc();
pid_t pid;
int main()
{
createproc();
printf("%d\n", pid);
exit(0);//_exit(0) gives the same result
}
int createproc()
{
if(!(pid=vfork())) {
printf("child proc:%d\n", pid);
}
else
printf("parent proc:%d\n", pid);
}
the output of the program is below:
child proc:0
0
parent proc:6958
child proc:0
Segmentation fault
As I know, vfork will suspend the parent process unless the exec or exit function is called and the stack segment is shared. So Here I have two questions:
Since they share a common address space, does exit(0) affect both process? If so, how? If not, why?
Why there is a line of "child proc:0" after "parent proc:6958"? I don't expect an answer like unexpected behavior.
Besides, through disassemble, I notice that the call of vfork didn't behave as normal function. There is no stack balance: Dump of assembler code for function vfork:
0xb7ed2050 <+0>: pop ecx
=> 0xb7ed2051 <+1>: mov edx,DWORD PTR gs:0x6c
0xb7ed2058 <+8>: mov eax,edx
0xb7ed205a <+10>: neg eax
0xb7ed205c <+12>: jne 0xb7ed2063 <vfork+19>
0xb7ed205e <+14>: mov eax,0x80000000
0xb7ed2063 <+19>: mov gs:0x6c,eax
0xb7ed2069 <+25>: mov eax,0xbe
0xb7ed206e <+30>: int 0x80
0xb7ed2070 <+32>: push ecx
0xb7ed2071 <+33>: test eax,eax
0xb7ed2073 <+35>: je 0xb7ed207c <vfork+44>
0xb7ed2075 <+37>: mov DWORD PTR gs:0x6c,edx
0xb7ed207c <+44>: cmp eax,0xfffff001
0xb7ed2081 <+49>: jae 0xb7ed2084 <vfork+52>
0xb7ed2083 <+51>: ret
0xb7ed2084 <+52>: call 0xb7f44d87 <__i686.get_pc_thunk.cx>
0xb7ed2089 <+57>: add ecx,0xedf77
0xb7ed208f <+63>: mov ecx,DWORD PTR [ecx-0x104]
0xb7ed2095 <+69>: xor edx,edx
0xb7ed2097 <+71>: sub edx,eax
0xb7ed2099 <+73>: add ecx,DWORD PTR gs:0x0
0xb7ed20a0 <+80>: mov DWORD PTR [ecx],edx
0xb7ed20a2 <+82>: or eax,0xffffffff
0xb7ed20a5 <+85>: jmp 0xb7ed2083 <vfork+51>
It actually pop the return address into ecx and push back after the system call(0xb7ed206e <+30>: int 0x80 0xb7ed2070 <+32>: push ecx). With the most unusual thing that there is a ret instruction: 0xb7ed2083 <+51>: ret
I am not familiar with assemble language, can anyone explain it to me?
execve()
), it clobbers the stack, see mail-archive.com/[email protected]/msg01290.html – ninjalj