/* Idea: order of locking is important A robot has 4 arms: Arm 1 is made of component A and C Arm 2 is made of component C and B Arm 3 is made of component C and A Arm 4 is made of component B and A */ #include #include pthread_mutex_t component_A_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t component_B_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t component_C_mutex = PTHREAD_MUTEX_INITIALIZER; // Arm 1 must get component A and component C void* arm1(void* arg) { pthread_mutex_lock(&component_A_mutex); printf("Arm 1 acquired Component A\n"); pthread_mutex_lock(&component_C_mutex); printf("Arm 1 acquired Component C\n"); printf("Arm 1 is assembling\n"); pthread_mutex_unlock(&component_C_mutex); pthread_mutex_unlock(&component_A_mutex); return NULL; } // Arm 2 must acquire component C and component B void* arm2(void* arg) { pthread_mutex_lock(&component_C_mutex); printf("Arm 2 acquired Component C\n"); pthread_mutex_lock(&component_B_mutex); printf("Arm 2 acquired Component B\n"); printf("Arm 2 is assembling\n"); pthread_mutex_unlock(&component_C_mutex); pthread_mutex_unlock(&component_B_mutex); return NULL; } // Arm C must acquire component A and component C void* arm3(void* arg) { pthread_mutex_lock(&component_A_mutex); printf("Arm 3 acquired Component C\n"); pthread_mutex_lock(&component_C_mutex); printf("Arm 3 acquired Component A\n"); printf("Arm 3 is assembling\n"); pthread_mutex_unlock(&component_A_mutex); pthread_mutex_unlock(&component_C_mutex); return NULL; } // arm 4 must acquire component A and component B void* arm4(void* arg) { pthread_mutex_lock(&component_A_mutex); printf("Arm 4 acquired Component A\n"); pthread_mutex_lock(&component_B_mutex); printf("Arm 4 acquired Component B\n"); printf("Arm 4 is assembling\n"); pthread_mutex_unlock(&component_B_mutex); pthread_mutex_unlock(&component_A_mutex); return NULL; } int main() { // Create four threads to assemble each arm pthread_t thread_id1; pthread_t thread_id2; pthread_t thread_id3; pthread_t thread_id4; pthread_create(&thread_id1, NULL, arm1, NULL); pthread_create(&thread_id2, NULL, arm2, NULL); pthread_create(&thread_id3, NULL, arm3, NULL); pthread_create(&thread_id4, NULL, arm4, NULL); // Wait for all four threads to finish pthread_join(thread_id1, NULL); pthread_join(thread_id2, NULL); pthread_join(thread_id3, NULL); pthread_join(thread_id4, NULL); return 0; }