I am working on a project that deals with multiple processes and threads effecting the same data. I have a line of code which can result into a segmentation fault because data can be updated from anywhere. For that particular line, if it causes segmentation fault, I somehow want to handle it instead of letting the program crash. Like I can simply update the memory location if the previous one was causing a segmentation fault. Is there any possible way to do that?
UPDATE(A short summary of my case):
I want extremely speedy access to a file. For that purpose, I am calling mmap(2) to map that file into all processes accessing it. The data I am writing to the file is in form of a particular data structure and it consumes lots of memory. So if a point comes that the size I mapped is not enough, I need to increase file size and mmap(2) that file again with the new size. For increasing the size I call ftruncate(2). ftruncate(2) may get called from any process so it may end up shrinking the file instead. So I need to check if the memory I am accessing doesn’t lead to seg faults. I am working on macOS.