[완료]vfork() 문제 - 스택프레임 손실?

nicessj0919의 이미지

#include <stdio.h>
#include <sys/types.h>
static void f1(void), f2(void);
 
int main(void)
{
	f1();
	f2();
	_exit(0);
}
 
static void f1(void)
{
	pid_t pid;
	if((pid = vfork()) < 0)
 
		printf("vfork error");
}
 
static void f2(void)
{
	char buf[1000];
	int i;
	for(i = 0; i < sizeof(buf); i++)
		buf[i] = 0;
}

시험 문제인데요.
스택 프레임에 대한 정보 손실로 인해 segmentation fault가 발생한다고 합니다.
gdb를 해서 백트레이스를 하는데 No stack이라고 뜨네요.
도대체 뭐가 문제인지 모르겠습니다.

익명 사용자의 이미지

man vfork
하시면 모든 필요한 설명이 있습니다.

Quote:

vfork() is a special case of clone(2). It is used to create new pro‐
cesses without copying the page tables of the parent process. It may
be useful in performance-sensitive applications where a child is cre‐
ated which then immediately issues an execve(2).

위의 설명이 왜 스택이 변질 되는지에 대한 이유입니다.
그것은 또한, 왜 아래의 제한 사항이 생겨나는지에대한 이유이기도 합니다.

Quote:

The vfork() function has the same effect as fork(2),
except that the behavior 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(2) or one of the exec(3) family of
functions.

사실 f2()를 호출하지 않아도
이미 undefined behavior 입니다. 왜나면, vfork() 되어진 프로세스가 f1()에서 main()으로 돌아가거든요.

따라서, 제 컴에서는 f2()를 삭제하버려도 여전히 segment fault 가 발생합니다.

nicessj0919의 이미지

답변 감사합니다! ^-^