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