Skip to content
Advertisement

Intermittent segmentation faults in main after fork

I’m taking a class on how to learn programming multi process programs on linux on university. I’m still very green and trying my best to learn, so any thing you might see that is wrong would be welcome.

I have a problem that asks me to iterate an array, one half on main process, the other half on the child process. I wrote code that does that, but the problem is, i’ve noticed if i run a few times the binary, the main(father) process from times to times segmentations faults.

Please have a look at the code, and tell me what’s wrong with it or if i am missing a key aspect of this type of programming. My answer starts after the comment //answer starts here.

JavaScript

Advertisement

Answer

The segmentation fault is caused by n being out of bounds sometimes:

JavaScript

Half can be 5000, but numbers is only 1000 elements.

Maybe you meant:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement