File descriptor table for vfork vs. fork [duplicate] - c

This question already has answers here:
What is the difference between fork() and vfork()?
(6 answers)
Closed 2 years ago.
I am a newbie in System Programming and I encounter some misunderstanding in fork and vfork.
From my knowledge, fork duplicate parent process's process and child process has its own VM and own file descriptor table.
As for vfork, it shared parent process's VM but child process will has its own file descriptor table.
So here comes the problem:
As child process share parent process' address, why does it need its own file descriptor table?
Where will the variable be stored if I declare one in the child process? (Will it use the space of parent process)
Thanks a lot.

Because it's common to redirect stdin and/or stdout before calling exec in the child process. If they shared the same file descriptor table, this would modify the parent process's I/O.
You shouldn't store any variables in the child process. vfork() should only be used if you're going to immediately call an exec function.
Note that vfork() is obsolete on modern operating systems. Instead of copying the address space they use copy-on-write.
For more information see What is the difference between fork() and vfork()?

Related

Child process executing parent code (lines before the fork) [duplicate]

This question already has answers here:
printf anomaly after "fork()"
(3 answers)
Duplicated output using printf() and fork() in C
(1 answer)
Closed 1 year ago.
I'm having difficulty understanding the behavior of fork(). I thought the child process will execute the lines "after" the fork(). So I expected to see only one "Hello world!", but this code:
printf("Hello World!\n");
fork();
return 0;
outputs two "Hello World". Why is that?
I also noticed from online examples using pipe() that pipes are created before forking a child. How does the child also have a pipe when it was created after the creation of the pipe in the parent process?
The first question is more than likely related to your program's file buffering mode, it's likely using full buffering setup, meaning that the stream is only written once the buffer (stdout) is full, this will delay the output, and the child process will also output, because it has the same stdout.
If you use fflush(stdout) right after the printf, or if you change your buffering mode (setvbuf) to line buffered or not buffered at all, you will prevent the duplicate output because it will happen before the fork.
As for the second question, the child process duplicates the code of the parent process, after the fork, as you correctly mentioned, but it also duplicates the file table. By creating the unnamed pipes before the fork you will assure the child has the same file descriptors. This is commonly used to setup pipe comunication between child and parent processes.
You can check the /proc/<process id>/fd folder for each one of the two processes to confirm this.
Footnote
For future reference, these are two different subjects, albeit tangential, they belong in different questions, you can see one of the problems here, the question was close with duplicates related to the first question but not the second, though by chance it didn't remain unanswered, it's still burried in a different matter and virtually undiscoverable by other users.
When we do fork the child process get the exact copy of address space of parent process. So such behaviour should be expected. The pipe system call returns a file descriptor and both parent and child process have this descriptor .

Will exec() called after fork() cause data loss in the existing process

I am comparatively new to the linux programming. I wonder whether the exec() function called after fork() can cause data loss in the parent process.
After a successful call to fork, a new process is created which is a duplicate of the calling process. One thing that gets duplicated are the file descriptors, so it's possible for the new process to read/write the same file descriptors as the original process. These may be files, sockets, pipes, etc.
The exec function replaces the currently running program in the current process with a new program, overwriting the memory of the old program in that process. So any data stored in the memory of the old program is lost. This does not however affect the parent process that forked this process.
When a new program is executed via exec, any open file descriptors that do not have the FD_CLOEXEC (close-on-exec) flag set (see the fcntl man page) are again preserved. So now you have two processes, each possibly running a different program, which may both write to the same file descriptor. If that happens, and the processes don't properly synchronize, data written by one process to the file may overwritten by the other process.
So data loss can occur with regard to writing to file descriptors that the child process inherited from the parent process.

Is a file descriptor local to its process or global on Unix [duplicate]

This question already has answers here:
Can I share a file descriptor to another process on linux or are they local to the process?
(6 answers)
Closed 6 years ago.
int fd = socket(//arguments);
Can this descriptor be passed to another process via IPC and still be valid or is it local to the process that created it?
File descriptors are local to the process. For instance, every process will have its standard input, output, and error on file descriptors 0, 1, and 2.
It is possible to pass a file descriptor to another process over a UNIX domain socket. When this is done, each process will have a copy of the file descriptor, possibly under a different FD number. It's a kind of messy API, though, and is rarely used.
Yes, file descriptors are local to the process. When a process forks a child, however, the parent and child have the same file descriptor table. This is great because it allows for IO redirection, which is a pretty handy trick.

Safe shm detach for mother process

might be my question is a bit too deep in the matter and i think of a problem that does not exist. I hope you can help. ;-)
The problem is the following:
I start a process in Linux at startup (rc.d) which then creates a shared memory and forks two daemon processes. The daemons, once detached from the parent processes have inherited the shared memory of the parent process but have also an own session and are no more connected to the parent.
Do they have an own link to the shared memory and does the kernel count the references? I ask because i would like to safely detach the parent process before it bails out. In my implementation it works, the shared memory is detached by the parent process but the daemons still can use it. But ist that safe or just by coincidence??
Thanks for your thoughts in advance!
Martell
In linux each process has a File Descriptor Table, file descriptors index into a per-process file descriptor table maintained by the kernel, that in turn indexes into a system-wide table of files opened by all processes, called the file table.
Now at fork, each of the child processes will have its own FD and each entry pointing to the same Objects as in the parent process. So if the parent process closes his shared-memory index in his FD this won't affect the other processes as the OS won't close this shared memory as its still in use by different processes. (In other words, child processes are still attached to this S.M and have a link to it and the OS counts them)

If a parent opens a file, then forks a child, does the child have access to the open file? (C programming) [duplicate]

This question already has answers here:
Seeking a simple description regarding 'file descriptor' after fork()
(2 answers)
Closed 6 years ago.
so I have a question regarding what a child process has access to.
If a parent open()s a file, and then fork()s a child, does the child process have access to the open file, or would it need to open the file itself? Would it be possible to have access to the already open file through shared memory between the processes? (C programming)
Short answer yes.
You can read more about it here:
http://man7.org/linux/man-pages/man2/fork.2.html
As it is said there, the child process is an exact duplicate of the parent process except
for the following points:
* The child has its own unique process ID, and this PID does not
match the ID of any existing process group (setpgid(2)).
* The child's parent process ID is the same as the parent's process
ID.
* The child does not inherit its parent's memory locks (mlock(2),
mlockall(2)).
* Process resource utilizations (getrusage(2)) and CPU time counters
(times(2)) are reset to zero in the child.
* The child's set of pending signals is initially empty
(sigpending(2)).
* The child does not inherit semaphore adjustments from its parent
(semop(2)).
* The child does not inherit process-associated record locks from
its parent (fcntl(2)). (On the other hand, it does inherit
fcntl(2) open file description locks and flock(2) locks from its
parent.)
* The child does not inherit timers from its parent (setitimer(2),
alarm(2), timer_create(2)).
* The child does not inherit outstanding asynchronous I/O operations
from its parent (aio_read(3), aio_write(3)), nor does it inherit
any asynchronous I/O contexts from its parent (see io_setup(2)).
The process attributes in the preceding list are all specified in
POSIX.1. The parent and child also differ with respect to the
following Linux-specific process attributes:
* The child does not inherit directory change notifications
(dnotify) from its parent (see the description of F_NOTIFY in
fcntl(2)).
* The prctl(2) PR_SET_PDEATHSIG setting is reset so that the child
does not receive a signal when its parent terminates.
* The default timer slack value is set to the parent's current timer
slack value. See the description of PR_SET_TIMERSLACK in
prctl(2).
* Memory mappings that have been marked with the madvise(2)
MADV_DONTFORK flag are not inherited across a fork().
* The termination signal of the child is always SIGCHLD (see
clone(2)).
* The port access permission bits set by ioperm(2) are not inherited
by the child; the child must turn on any bits that it requires
using ioperm(2).
Note the following further points:
* The child process is created with a single thread—the one that
called fork(). The entire virtual address space of the parent is
replicated in the child, including the states of mutexes,
condition variables, and other pthreads objects; the use of
pthread_atfork(3) may be helpful for dealing with problems that
this can cause.
* After a fork(2) in a multithreaded program, the child can safely
call only async-signal-safe functions (see signal(7)) until such
time as it calls execve(2).
* The child inherits copies of the parent's set of open file
descriptors. Each file descriptor in the child refers to the same
open file description (see open(2)) as the corresponding file
descriptor in the parent. This means that the two file
descriptors share open file status flags, file offset, and signal-
driven I/O attributes (see the description of F_SETOWN and
F_SETSIG in fcntl(2)).
* The child inherits copies of the parent's set of open message
queue descriptors (see mq_overview(7)). Each file descriptor in
the child refers to the same open message queue description as the
corresponding file descriptor in the parent. This means that the
two file descriptors share the same flags (mq_flags).
* The child inherits copies of the parent's set of open directory
streams (see opendir(3)). POSIX.1 says that the corresponding
directory streams in the parent and child may share the directory
stream positioning; on Linux/glibc they do not.
Yes, using the same file descriptor. No need for shared memory.
It would kind of work using a stdio FILE* but I would no advise doing so for files you plan to write to, as buffering in the two separate processes would lead to unexpected and confusing results.
To prevent this sharing of file descriptors - if you want to - you can of course call close() on the file descriptor as soon as the fork() call returns 0.

Resources