#include #include #include #include long long counter = 0; long long niters; sem_t mutex; void *thread_func(void *arg) { for (long long i = 0; i < niters; i++) { sem_wait(&mutex); counter++; sem_post(&mutex); } return NULL; } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } niters = atoll(argv[1]); sem_init(&mutex, 0, 1); pthread_t t1, t2; pthread_create(&t1, NULL, thread_func, NULL); pthread_create(&t2, NULL, thread_func, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); sem_destroy(&mutex); printf("Expected: %lld, Got: %lld\n", 2 * niters, counter); if (counter == 2 * niters) { printf("SUCCESS: No race condition.\n"); } else { printf("ERROR: Race condition detected!\n"); } return 0; }