We have a file IO library that basically writes data to a file in a fancy format and then later reads the file back. We must ensure that the file is readable even if the library (or computer) crashes amidst the middle of writing.
We know how to write a program that crashes itself. We can use kill(getpid())
to simulate a crash. Or we can use 1/0
and so on. So we'll write a program that opens the library, writes some data, and then kills itself right in the middle. Then we'd have a file in its half-baked state that we can open in the reader to see if the reader can handle the file.
The question: how to make this automated?
For example, we'd like our unit test suite (which tests many aspects of the library automatically and gives a pass/fail) to create a new program/process that begins writing a file and then commits suicide. Then control can be handled back to the unit test which can then open the file that the process created before seppuku. Then the unit test can do some validation on the file to see if we've satisfied our requirment "leave a valid file upon crash".
I was looking into the exec
function which seems to do what we need. However, I was not able to ascertain whether exec
returns back to the caller program/process if the exec
'd program crashes.