Kill the Zombie
This week, my task is to create a cache system for the FPGA synthesis and implementation process, and to kill the previous synthesis process. The cache system would be useful if someone replicates or reuse their design. This saves the processing power and time, where the synthesized file would be ready immediately. Caching the file by using cryptographic hash function, is pretty interesting. I get to see the power of these functions and why they are so popular to be used, especially with the usage of salt.
Other than caching the files that had been synthesized, I also tried to work on the function that if the synthesis request is being sent again before the previous request actually finished. This would require a termination of the previous synthesis process before starting a new one. My supervisor recommends me to implement a kill function that uses the bash command
kill -9 $pid where the $pid represent the process ID (pid) of the synthesis.
Zombie in the Code
As I discovered from the Internet, the usage of asynchronous bash command
codelite & pid=$! actually stores the process id into the variable
pid. Making the process run asynchronously is not a good idea, especially when I am implementing it into the C++ program. The system command has not executed finish, and it jumps into the next function. Thus, one of the solution for this problem is the usage of
wait command, the documentation of wait is at here.
I did an experiment on the usage of getting the pid and wait, it works well! However, when I actually implement it in C++ program, things screw up. The pid process finished, but it does not respond back to the wait command. As I try to debug it, I found that the process eventually turned into a zombie (
defunct). I did not notice it until I actually googled it around. The zombie was created due to the parent does not exit properly.
Killing the Zombie in C++ System command
As I spent hours debugging the code, trying to kill the zombie but it does not work. Finally, I found the problem is with the usage of std::system function. The execution of the command does not exit even after the wait signal received or killed it.
One of the possible solution that I found for this problem is to run the command in a bash file. Saving all the necessary command into a bash file, and run the command
bash /xxxx/xxx.sh solves the problem perfectly. The system command of bash would exits and return normally if the pid is finished, without leaving any zombie/orphan behind! While I actually need to kill the pid process before it finishes, this would also return and exit normally but with different number.
The problem could also be solved using the
popen(), which worked similarly to system command.
After playing with all the bash commands, next week I will be implementing the kill process function for the IMP process, and continue to break my colleague, Jeunn Hao’s code!