Use wait function to check if a child process is finished