Questions tagged [vfork]

vfork() is a C function available on many Unix-like systems to fork a process. There are strict rules about what you can do in the child process. It was part of POSIX (Single Unix Specification) in the 1997 and 2004 editions (marked 'obsolescent' in POSIX 2004), but it is not a part of POSIX 2008 (2016).

The vfork() system call was added to BSD systems in the days before ubiquitous paging and copy-on-write techniques. It avoided having to replicate the entire image of the process and worked well as long as the next operation was one of the exec*() functions or _exit().

These days, there is little benefit to using vfork(). Kernels still need custom implementations of vfork() because the memory management of vfork() is different from fork() — the vforked child can (but should not!) modify the parents variables, file descriptors, etc., which is not possible with fork().

39 questions
41
votes
6 answers

What is the difference between fork() and vfork()?

What is the difference between fork() and vfork()? Does vfork() return like fork().
user507401
  • 2,809
  • 9
  • 25
  • 20
7
votes
3 answers

When I call vfork(), can I call any exec*() function, or must I call execve()?

From the Linux man page: The vfork() function has the same effect as fork(2), except that the behavior is undefined if the process [...] calls any other function before successfully calling [...] one of the exec(3) family of functions. This…
Cornstalks
  • 37,137
  • 18
  • 79
  • 144
4
votes
4 answers

Can a fork child determine whether it is a fork or a vfork?

Within the child process, is there any way that it determine whether it was launched as a fork with overlay memory, or a vfork with shared memory? Basically, our logging engine needs to be much more careful (and not log some classes of activity) in…
Gem Taylor
  • 5,381
  • 1
  • 9
  • 27
4
votes
6 answers

Using fork(), how can I make child process run always first?

Child and parent process execution is parallel and which starts first depends on OS scheduling. But what can be done to start child always before the parent? This is the pseudo code for my problem, int start_test() { pid_t pid; pid = fork(); …
Jagdish
  • 1,848
  • 3
  • 22
  • 33
3
votes
3 answers

Incorrect result from getpid() for grandchild with vfork() and -lpthread

In one of the special cases shown below, getpid() for the grandchild created with vfork() returns the PID of the parent process. #include #include int main() { if(vfork()) { /* parent */ printf("parent pid = %d\n",…
endgame
  • 35
  • 5
3
votes
2 answers

About vfork() system call?

#include #include #include #include #include #include int main() { pid_t child_pid = vfork(); if(child_pid < 0) { printf("vfork() error\n"); exit(-1); } …
Guna K K
  • 155
  • 2
  • 9
2
votes
1 answer

Can I call dup2 after vfork?

I want to vfork() a child process, but have its stdout be different than the parent's stdout. The obvious way to achieve this with fork() would be to dup2() (and close() the original file descriptor) in the child after forking. Let's say I have the…
talshorer
  • 662
  • 3
  • 9
2
votes
1 answer

Get new pid from nd_syscall.vfork.return in systemtap

I'm trying to extract things from a weird makefile, and I found that systemtap is a potential good solution so here I am: I can get correct pid() ppid() called from the new process when probing with nd_syscall.clone.return, however this doesn't…
tdihp
  • 2,329
  • 2
  • 23
  • 40
2
votes
1 answer

Busybox udhcpd vfork starts two processes

In my application I require to start Busybox udhcpd (dhcp server), the code is below. While udhcpd does start and run I get two versions in the process list. udhcpd is running correctly, i.e. assigned IP addresses to devices. pid_t forked_pid =…
2
votes
1 answer

popen() implementation,fd leaks,and vfork()

In the glibc implementation of popen(), it specifies that The popen() function shall ensure that any streams from previous popen() calls that remain open in the parent process are closed in the new child process. Why? If the purpose is to avoid fd…
user3547691
  • 227
  • 2
  • 9
2
votes
1 answer

fork vs vfork functionality in a C program

I am doing some C exercise for self-learning, and came across the following problem: Part a: int main(int argc, char **argv) { int a = 5, b = 8; …
theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
2
votes
2 answers

Need clarification regarding vfork use

I want to run the child process earlier than the parent process. I just want to use execv call from the child process, So i am using vfork instead of fork. But suppose execv fails and returns, i want to return non-zero value from the function in…
Jagdish
  • 1,848
  • 3
  • 22
  • 33
2
votes
1 answer

vfork() with gcc -O2 seems to have a wrong result.

I took the code from APUE. In Ubuntu 12.04 if I use gcc without any optimization, I will get the results same as the book's: pid = 4122, glob = 7, var = 89. If I use gcc -O2, then the var will be 88. Is this because the gcc optimization will do…
2
votes
5 answers

vfork never ends

The following code never ends. Why is that? #include #include #include #define SIZE 5 int nums[SIZE] = {0, 1, 2, 3, 4}; int main() { int i; pid_t pid; pid = vfork(); if(pid == 0){ /* Child process */ …
zsljulius
  • 3,813
  • 8
  • 36
  • 61
1
vote
1 answer

How to call manually fork handlers registered by `pthread_atfork()`?

I am using vfork() in glibc and according to vfork()'s man page: Fork handlers established using pthread_atfork(3) are not called when a multithreaded program employing the NPTL threading library calls vfork(). Fork handlers are called in this…
joepol
  • 744
  • 6
  • 22
1
2 3