Assignment 4 os
Assignment 4 os
#define READER_COUNT 5
#define WRITER_COUNT 2
#define READ_TIMES 3
#define WRITE_TIMES 2
pthread_mutex_t lock_access;
sem_t writer_control;
pthread_mutex_lock(&lock_access);
active_readers--;
if (active_readers == 0) {
sem_post(&writer_control);
}
pthread_mutex_unlock(&lock_access);
usleep(500000);
}
return NULL;
}
sem_post(&writer_control);
usleep(500000);
}
return NULL;
}
pthread_mutex_init(&lock_access, NULL);
sem_init(&writer_control, 0, 1);
pthread_mutex_destroy(&lock_access);
sem_destroy(&writer_control);
}
int main() {
printf("Choose Mode: 1 Synchronized 2 Unsynchronized\nPlease enter the choice (1/2):
");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nRunning Synchronized Execution...\n");
run_sync();
printf("\nSynchronized Execution Completed!\n");
break;
case 2:
printf("\nRunning Unsynchronized Execution...\n");
run_unsync();
printf("\nUnsynchronized Execution Completed!\n");
break;
default:
printf("\nInvalid Choice! Please restart the program.\n");
}
return 0;
}
Output: