2

I am getting segmentation fault in the following program. Why? And how to resolve it?

#include <stdio.h>
main()
{
    int pid;
    printf("I'm the original process with PID %d and PPID %d.\n", getpid(),getppid());
    pid=vfork();
    if (pid!=0)
    {
         printf("I'm the parent process with PID %d and PPID %d.\n",getpid(),getppid());
         printf("My child's PID is %d.\n", pid);
    }
    else  
    {
         printf("I'm the child process with PID %d and PPID %d.\n",getpid(),getppid());
    }
}

Output:

I'm the original process with PID 18563 and PPID 18500.
I'm the child process with PID 18564 and PPID 18563.
I'm the parent process with PID 18563 and PPID 18500.
My child's PID is 18564.
Segmentation fault
3
  • 2
    It worked well for me. No segmentation fault. Commented Aug 18, 2014 at 6:16
  • No segmentation fault for me too ... This is all your code or just a simplified example? Commented Aug 18, 2014 at 6:19
  • man vfork. Important is the part: "The child must not return from the current function or call exit(3), but may call _exit(2)." as well as: "Until that point, the child shares all memory with its parent, including the stack.". What happens is that the return from main of the child moves the stack pointer. Depending on what happens next, you'll get a SEGV in the parent or not. In the simple example, the odds of everything working are good Commented Aug 18, 2014 at 6:20

2 Answers 2

4

From vfork man page

(From POSIX.1) 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.

You are returning before a successful call to _exit, so this behavior is undefined. Try fixing that and see if the problem persists.

Sign up to request clarification or add additional context in comments.

1 Comment

You were about 10secs slower than I (I commented, didn't answer), but about 1 minute faster than SzG :-). Anyway, we all agree that using vfork() just because it's "faster" is not a good idea if you don't know the restrictions. I'll +1 both of you
2

A quote from the man page of vfork:

vfork() differs from fork(2) in that the calling thread 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).

Your child process has returned from the function it was created in, so you probably corrupted the stack shared by both threads.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.