fork and waitpid fail on linux. Without hitting the hard or soft limits
I finally found the problem, it is because of a memory leak in the Perl's thread module that causes the memory to grow after a long time. Then open2 can not allocate memory and fails.
Linux x86-64 fork syscall strange behavior against C standard libc FILE I/O (keywords: fork, fclose, linux)
Thanks to @Jonathan Leffler!
This problem is a duplicate of Why does forking my process cause the file to be read infinitely
The missing knowledge, why does the bug not occur on Linux 5.10.0-051000-generic
turned out that it is not related to the kernel.
It turned out that the parent process competes with the child processes (not related to kernel).
- Note: change the offset of file handle from child process will also change the offset in parent process if the handle is created by the parent.
- If there is no
fclose(3)
in the childs, the child processes will calllseek(2)
as soon as they callexit(3)
. This will cause the parent re-read the same offset, because the childs calllseek(2)
with negative offset +SEEK_CUR
.
(I don't know why it is necessary to call lseek(2)
before exit, it might have been explained in @Jonathan Leffler's answer, I did not read the whole answer carefully).
- If the parent finishes to read the entire file before the childs call
lseek(2)
. Then there is no problem at all.
Also, as @iBug has mentioned But keep in mind that process scheduling may make the result non-predictable, unless you implement some kind of "syncing".
The parent process on Linux 5.10.0-051000-generic
machine I used was just a lucky process that always won to read the entire file first before the childs call lseek(2)
.
I tried to add more lines to the file (to be 150 lines), so the parent will mostly be slower than reading 6 lines, and the undefined behavior happens.
Test result: https://gist.githubusercontent.com/ammarfaizi2/b72bd03fcc13779f96b8bbeef9253e66/raw/da1eff4ed5434aa51929e5c810d54de8ffe15548/test2_fix.txt
Related Topics
How to Make the Watch Command Interpret Vt100 Sequences
Dlopen Failed: Cannot Open Shared Object File: No Such File or Directory
How to Cd into a Directory Using Perl
Setting the Thread /Proc/Pid/Cmdline
Initiating Dynamic Variables (Variable Variables) in Bash Shell Script
Spell Checking a File Using Command Line, Non-Interactively
Bash Script to Run a Constant Number of Jobs in the Background
Intellij Idea 2017.2 Can't Add Openjk 9 on Linux Mint 18
Python Read Linux Memory Process Error (/Proc/$Pid/Mem)
How to Autocomplete a Bash Commandline with File Paths
Sshpass: Command Not Found Error
How to Prevent Matlab Printing False Space and Use Wrong Fonts
Replace Text Based on a Dictionary
Having Trouble Finding the Method _Kernel_Vsyscall Within the Linux Kernel
Installing Octave Package in Ubuntu