If you're going to modify and post code, a brief description of the changes you made would be appropriate.

Alexdev wrote:Am I doing something wrong?

Yes, you've posted buggy code to test for a possible system problem.
When I run your code, I see messages about invalid file descriptors (caused by stack corruption?) , which you apparently recklessly choose to ignore.