1

I open a file in program A. Its file descriptor is 3. Using fork followed by an execve I execute another program B, where I immediately open another file. This files descriptor is 4. If A and B was not sharing the file descriptor table then the file descriptor of file opened in B should have been 3. I need to create child processes not sharing the parents address space including open files.

Thanks a lot

Lipika Deka
  • 3,774
  • 6
  • 43
  • 56

2 Answers2

2

The child doesn't share the same FD table, you simply forgot to close them in the child or mark them close-on-exec.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Close the file before execing the new process. Do that in the code between the fork() and exec().

wallyk
  • 56,922
  • 16
  • 83
  • 148