Created
September 11, 2015 20:01
-
-
Save msullivan/aabcbcbee1da895fbf17 to your computer and use it in GitHub Desktop.
vfork testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This code is all kinds of illegal and tests whether other threads | |
| // keep running while there is an outstanding vfork | |
| #include <atomic> | |
| #include <thread> | |
| #include <iostream> | |
| #include <unistd.h> | |
| std::atomic<bool> child_running; | |
| std::atomic<bool> child_signaled; | |
| std::atomic<bool> child_done; | |
| void writer() { | |
| while (!child_running) continue; | |
| std::cout << "T2 saw write from vfork child; signalling back\n"; | |
| child_signaled = true; | |
| while (!child_done) continue; | |
| std::cout << "T2 saw final child write\n"; | |
| } | |
| void forker() { | |
| std::cout << "T1 vforking\n"; | |
| if (vfork() == 0) { | |
| child_running = true; | |
| while (!child_signaled) continue; | |
| child_done = true; | |
| _exit(0); | |
| } | |
| std::cout << "T1 back\n"; | |
| } | |
| int main() { | |
| std::thread t1(writer); | |
| std::thread t2(forker); | |
| t1.join(); | |
| t2.join(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment