linux-sysprog · intermediate · ~15 min

Opposite lock ordering

Detect the classic lock-ordering deadlock.

Challenge

Two threads each take two locks. Implement int deadlock_risk(int t1_first, int t1_second, int t2_first, int t2_second) returning 1 if the threads acquire the same pair of distinct locks in opposite order (the AB-BA pattern), else 0.

Starter code

int deadlock_risk(int t1_first, int t1_second, int t2_first, int t2_second) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.