systems-programming

Why does this program print “forked!” 4 times?

岁酱吖の 提交于 2019-11-26 03:29:46
问题 Why does this program print “forked!” 4 times? #include <stdio.h> #include <unistd.h> int main(void) { fork() && (fork() || fork()); printf(\"forked!\\n\"); return 0; } 回答1: The first fork() returns a non-zero value in the calling process (call it p0) and 0 in the child (call it p1). In p1 the shortcircuit for && is taken and the process calls printf and terminates. In p0 the process must evaluate the remainder of the expression. Then it calls fork() again, thus creating a new child process

What happens to an open file handle on Linux if the pointed file gets moved, delete

心不动则不痛 提交于 2019-11-26 00:52:20
What happens to an open file handle on Linux if the pointed file meanwhile gets: Moved away -> Does the file handle stay valid? Deleted -> Does this lead to an EBADF, indicating an invalid file handle? Replaced by a new file -> Does the file handle pointing to this new file? Replaced by a hard link to a new file -> Does my file handle "follow" this link? Replaced by a soft link to a new file -> Does my file handle hit this soft link file now? Why I'm asking such questions: I'm using hot-plugged hardware (such as USB devices etc.). It can happen, that the device (and also its /dev/file) gets