#include <iostream>
#include <thread>
#include <chrono>

using namespace std;

volatile int turn = 0;  // Global variable controlling process execution order

void process(int id) {
    int other = 1 - id;

    for (int i = 0; i < 3; ++i) {
        // Busy wait until the process gets its turn
        while (turn != id) {
            // Do nothing, just wait
        }

        // Critical section
        cout << "Process " << id << " is in critical section" << endl;
        this_thread::sleep_for(chrono::seconds(1));  // Simulating critical section delay

        // Change turn to allow the other process to execute
        turn = other;

        // Remainder section
        cout << "Process " << id << " is in remainder section" << endl;
        this_thread::sleep_for(chrono::milliseconds(500));  // Simulating remainder section delay
    }
}

int main() {
    // Creating two threads (simulating two processes)
    thread t1(process, 0);
    thread t2(process, 1);

    // Wait for both threads to finish
    t1.join();
    t2.join();

    return 0;
}
