I have a program that executes the same code sequentially and in parallel simultaneously between various checkpoints throughout the program. To do this, the initial program forks off a child process which runs sequentially whilst the parent process runs in parallel. Whichever process reaches the next checkpoint first then kills off the other process and this repeats until the end of execution thereby executing as fast as possible (ignoring fork and copy overheads).
I have implemented this and everything works fine when only sequential execution is the fastest, or only parallel execution is the fastest, but when the first few sections execute fastest in parallel followed by the next few executing fastest sequentially the program stalls with both processes sleeping. I cannot see what could be causing this. Have I hit some limit imposed on forking processes? Can killing a parent process affect the execution of a forked child process or vice versa?
The code below gives my function checkPoint()
which is executed at every checkpoint, with some custom code handling the end of the final section. Parallel code is implemented using OpenMP with sequential code never encountering an OpenMP pragma statement.
pid_t parent = 0;
pid_t child = 0;
void checkPoint() {
if (parent == 0) {
// First Time
parent = getpid();
} else if (child == 0) {
// Child Process
kill(parent, SIGKILL);
parent = getpid();
} else {
// Parent Process
kill(child, SIGKILL);
}
child = fork();
}
Thanks,
Dan