107
votes
Accepted
Why does a program with fork() sometimes print its output multiple times?
When outputting to standard output using the C library's printf() function, the output is usually buffered. The buffer is not flushed until you output a newline, call fflush(stdout) or exit the ...
89
votes
Accepted
Why can't I crash my system with a fork bomb?
You probably have a Linux distro that uses systemd.
Systemd creates a cgroup for each user, and all processes of a user belong to the same cgroup.
Cgroups is a Linux mechanism to set limits on ...
46
votes
Accepted
Why "the process must not fork" for simple type services in systemd?
The service is allowed to call the fork system call. Systemd won't prevent it, or even notice if it does. This sentence is referring specifically to the practice of forking at the beginning of a ...
23
votes
Accepted
Double fork() - why?
The following paragraphs, quoted from Stevens and Rago Advanced Programming in the UNIX Environment, describe two of six coding rules for writing a daemon. Specifically, they implement them in a ...
21
votes
Accepted
When to call fork() and exec() by themselves?
Sure! A common pattern in "wrapper" programs is to do various things and then replace itself with some other program with only an exec call (no fork)
#!/bin/sh
export BLAH_API_KEY=blub
...
exec /the/...
19
votes
Why does a program with fork() sometimes print its output multiple times?
It does not affect the fork in any way.
In the first case, you end up with 8 processes with nothing to write, because the output buffer was emptied already (due to the \n).
In the second case you ...
16
votes
Why can't I crash my system with a fork bomb?
This won't crash modern Linux systems anymore anyway.
It creates hoards of processes but doesn't really burn all that much CPU as the processes go idle. You run out of slots in the process table ...
15
votes
When to call fork() and exec() by themselves?
There are plenty.
Programs that call fork() without exec() are usually following a pattern of spawning child worker processes for performing various tasks in separate processes to the main one. You'...
15
votes
Accepted
fork() Causes DMA Buffer in Physical Memory to Retain Stale Data on Subsequent Writes
My Assumptions & Concerns: fork() causes COW (Copy-on-Write), but since only the parent writes to the buffer, I expected the updated contents to reflect in physical memory.
man madvise contains:
...
14
votes
Accepted
Why should fork() have been designed to return a file descriptor?
The problem is described there in your source, select() should be interrupted by signals like SIGCHLD, but in some cases it doesn't work that well. So the workaround is to have signal write to a pipe, ...
14
votes
Why "the process must not fork" for simple type services in systemd?
Ignore this Arch wiki page.
It has got things quite considerably wrong with respect to the Type setting.
This is not just limited to its descriptions of simple, moreover.
What it says about forking is ...
13
votes
How to stop kate forking itself
From the Kate manual (which you evidently are already aware of):
kate -b --block
If using an already running Kate instance, block until it exits, if URLs given to open.
You can use Kate with this ...
12
votes
Why does a program with fork() sometimes print its output multiple times?
@Kusalananda explained why the output is repeated. If you are curious why the output is repeated 8 times and not only 4 times (the base program + 3 forks):
int main()
{
printf("hello world...");
...
10
votes
How does copy-on-write in fork() handle multiple fork?
The behavior of fork() depends on whether the *nix system has a MMU or not. On a non-MMU system (like early PDP-11s) the fork() system call copied all of the parent's memory for each child. On a MMU-...
10
votes
Why are file descriptors shared between forked processes?
Consider a shell snippet
{ somecmd; othercommand *.txt; } > outputfile
The shell opens outputfile once, when starting the redirection, and then passes the file handle to the somecmd and othercmd, ...
9
votes
Accepted
Systemd timeout because it doesn't detect daemon forking
Do not do that.
At all. Any of it, either through a library function or rolling your own code. For any service management system. It has been a wrongheaded idea since the 1990s.
Your dæmon is ...
9
votes
Why can't I crash my system with a fork bomb?
Back in the 90's I accidentally unleashed one of these on myself. I had inadvertently set the execute bit on a C source file that had a fork() command in it. When I double-clicked it, csh tried to run ...
8
votes
Accepted
Why forking is used in a unit file of a service?
Why a service does that?
Services generally do not do that, in fact. Aside from the fact that it isn't good practice, and the idea of "dæmonization" is indeed fallacious, what services do ...
7
votes
Accepted
How does the fork system call work?
Quoting from POSIX fork definition (bold emphasis mine):
RETURN VALUE
Upon successful completion, fork() shall return 0 to the child process
and shall return the process ID of the child process to ...
6
votes
Accepted
Why are file descriptors shared between forked processes?
POSIX explains the reasoning thus:
There are two reasons why POSIX programmers call fork(). One reason is to create a new thread of control within the same program (which was originally only ...
6
votes
Process descendants
You could use this kind of shell script:
set -m; (
# start the processes in this container:
...
) & set +m; pid=$!
...
# terminate the container:
kill -- -"$pid"
The trick is to enable job ...
6
votes
Accepted
What is the easiest way to run a VM that executes a simple C program
With qemu kvm, booting the host kernel in a headless VM with console I/O on serial (redirected to the terminal you run it from):
Compile that fork-bomb.c into a static init executable:
gcc -static -o ...
5
votes
Why does a program with fork() sometimes print its output multiple times?
The important background here is that stdout is required to be line buffered by the standard as default setup.
This causes a \n to flush the output.
Since the second example does not contain the ...
5
votes
Question about global environment variables and fork() & exec()
There are no global environment variables. They are passed from parent to child.
fork does not change the environment variables.
exec without the e post-fix does not change the environment variables.
...
5
votes
Accepted
Why is 'ls' being created by execve() call and not fork()
Your understanding is correct. When you run strace ls there are even two forks. The shell forks itself and uses exec() to run strace and strace does the same to run ls.
You don't see the fork in the ...
4
votes
Accepted
Is copy-on-write not implemented based on page fault?
(Since this is tagged linux, I’m answering in that context. None of this is exclusive to Linux.)
Is copy-on-write not implemented based on page fault?
It is based on page faults. “Copied” pages are ...
4
votes
Accepted
Two processes with identical PID after a fork call
Process IDs are unique.
Per the POSIX fork() documentation:
DESCRIPTION
The fork() function shall create a new process. The new process (child
process) shall be an exact copy of the calling ...
4
votes
Accepted
How does a process and its children use memory in case of mmap()?
On fork() the memory space of the parent process is cloned into the child process. As an optimization, modern operating systems use COW (copy on write), so all private memory is shared with the child ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
fork × 193process × 63
linux × 45
bash × 29
c × 22
exec × 18
shell × 17
shell-script × 10
system-calls × 10
systemd × 9
signals × 9
memory × 8
background-process × 8
file-descriptors × 8
pipe × 7
ubuntu × 6
daemon × 6
virtual-memory × 6
debian × 5
linux-kernel × 5
strace × 5
zsh × 4
process-management × 4
subshell × 4
ulimit × 4