how to set close-on-exec by default

No and no. You simply need to be careful and set close-on-exec on all file descriptors you care about. Setting it is easy, though: #include <fcntl.h> fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); #include <unistd.h> /* please don’t do this */ for (i = getdtablesize(); i –> 3;) { if ((flags = fcntl(i, F_GETFD)) != -1) … Read more

Just check status process in c

Then you want to use the waitpid function with the WNOHANG option: #include <sys/types.h> #include <sys/wait.h> int status; pid_t return_pid = waitpid(process_id, &status, WNOHANG); /* WNOHANG def’d in wait.h */ if (return_pid == -1) { /* error */ } else if (return_pid == 0) { /* child is still running */ } else if (return_pid … Read more

Duplicated output using printf() and fork() in C

The output of your program is highly implementation dependent. The C standard library applies buffering to the output stream. This means that it accumulates characters to write until the buffer reaches a certain length (or until a certain condition is met), and then outputs all the text at once. The most common behavior is to … Read more

How does pcntl_fork work in PHP?

PCNTL can not make threads. It only “forks” current PHP process. What does it mean? When you call pcntl_fork(), current process is split into two processes. Whole namespace of parent process is copied into the child and both processes continue with execution in parallel with only one difference: pcntl_fork() returns child’s PID in parent and … Read more

Visually what happens to fork() in a For Loop

Here’s how to understand it, starting at the for loop. Loop starts in parent, i == 0 Parent fork()s, creating child 1. You now have two processes. Both print i=0. Loop restarts in both processes, now i == 1. Parent and child 1 fork(), creating children 2 and 3. You now have four processes. All … Read more