| 1 | #include <cassert> |
|---|---|
| 2 | #include <chrono> |
| 3 | #include <cstdlib> |
| 4 | #include <sys/wait.h> |
| 5 | #include <thread> |
| 6 | #include <unistd.h> |
| 7 | |
| 8 | int main() { |
| 9 | pid_t fork_result = fork(); // break here |
| 10 | assert(fork_result >= 0); |
| 11 | if (fork_result == 0) { |
| 12 | // child |
| 13 | _exit(status: 47); |
| 14 | } |
| 15 | // parent |
| 16 | // Use polling to avoid blocking if the child is not actually resumed. |
| 17 | auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10); |
| 18 | std::chrono::milliseconds poll_interval{10}; |
| 19 | while (std::chrono::steady_clock::now() < deadline) { |
| 20 | int status; |
| 21 | pid_t waitpid_result = waitpid(pid: fork_result, stat_loc: &status, WNOHANG); |
| 22 | if (waitpid_result == fork_result) |
| 23 | return 0; |
| 24 | assert(waitpid_result == 0); |
| 25 | std::this_thread::sleep_for(rtime: poll_interval); |
| 26 | poll_interval *= 2; |
| 27 | } |
| 28 | abort(); |
| 29 | } |
| 30 |
