added optimised OS codes
This commit is contained in:
parent
0fc0033265
commit
fbcc758f43
2 changed files with 39 additions and 45 deletions
BIN
OS/C/Week7/q2
Executable file
BIN
OS/C/Week7/q2
Executable file
Binary file not shown.
|
@ -6,106 +6,100 @@
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define NUM_READERS 3
|
#define NR 3 // NUM_READERS
|
||||||
#define NUM_WRITERS 2
|
#define NW 2 // NUM_WRITERS
|
||||||
|
|
||||||
// shared data structure - simple integer
|
// shared data structure - simple integer
|
||||||
int shared_data = 0;
|
int sd = 0; // shared_data
|
||||||
|
|
||||||
// sync variables
|
// sync variables
|
||||||
pthread_mutex_t mutex; // mutex for read_count protection
|
pthread_mutex_t m; // mutex for rc protection
|
||||||
sem_t wrt; // semaphore for writer access control
|
sem_t w; // semaphore for writer access control
|
||||||
int read_count = 0; // tracks active readers
|
int rc = 0; // read_count: tracks active readers
|
||||||
|
|
||||||
void* reader(void* arg) {
|
void* r(void* arg) { // reader
|
||||||
int id = *((int*)arg);
|
int id = *((int*)arg);
|
||||||
while (1) {
|
while (1) {
|
||||||
// read delay sim
|
// read delay sim
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
// critical section entry
|
// critical section entry
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&m);
|
||||||
read_count++;
|
rc++;
|
||||||
if (read_count == 1) {
|
if (rc == 1) {
|
||||||
// first reader blocks writers
|
// first reader blocks writers
|
||||||
sem_wait(&wrt);
|
sem_wait(&w);
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&m);
|
||||||
|
|
||||||
// critical section
|
// critical section
|
||||||
printf("Reader %d is reading shared_data = %d\n", id, shared_data);
|
printf("Reader %d is reading sd = %d\n", id, sd);
|
||||||
sleep(2); // read time sim
|
sleep(2); // read time sim
|
||||||
|
|
||||||
// critical section exit
|
// critical section exit
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&m);
|
||||||
read_count--;
|
rc--;
|
||||||
if (read_count == 0) {
|
if (rc == 0) {
|
||||||
// last reader unblocks writers
|
// last reader unblocks writers
|
||||||
sem_post(&wrt);
|
sem_post(&w);
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&m);
|
||||||
}
|
}
|
||||||
pthread_exit(NULL);
|
pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* writer(void* arg) {
|
void* w_func(void* arg) { // writer
|
||||||
int id = *((int*)arg);
|
int id = *((int*)arg);
|
||||||
while (1) {
|
while (1) {
|
||||||
// write delay sim
|
// write delay sim
|
||||||
sleep(3);
|
sleep(3);
|
||||||
|
|
||||||
// critical section entry
|
// critical section entry
|
||||||
sem_wait(&wrt); // exclusive access control
|
sem_wait(&w); // exclusive access control
|
||||||
|
|
||||||
// critical section
|
// critical section
|
||||||
shared_data += 1;
|
sd++; // shared_data modification
|
||||||
printf("Writer %d is writing: shared_data becomes %d\n", id, shared_data);
|
printf("Writer %d is writing: sd becomes %d\n", id, sd);
|
||||||
sleep(2); // write time sim
|
sleep(2); // write time sim
|
||||||
|
|
||||||
// critical section exit
|
// critical section exit
|
||||||
sem_post(&wrt);
|
sem_post(&w);
|
||||||
}
|
}
|
||||||
pthread_exit(NULL);
|
pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
pthread_t readers[NUM_READERS], writers[NUM_WRITERS];
|
pthread_t rts[NR], wts[NW]; // reader/writer threads
|
||||||
int reader_ids[NUM_READERS], writer_ids[NUM_WRITERS];
|
int rids[NR], wids[NW]; // reader/writer ids
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
// sync init
|
// sync init
|
||||||
pthread_mutex_init(&mutex, NULL);
|
pthread_mutex_init(&m, NULL);
|
||||||
sem_init(&wrt, 0, 1); // binary semaphore init
|
sem_init(&w, 0, 1); // binary semaphore init
|
||||||
|
|
||||||
// reader thread creation
|
// reader thread creation
|
||||||
for (i = 0; i < NUM_READERS; i++) {
|
for (i = 0; i < NR; i++) {
|
||||||
reader_ids[i] = i + 1;
|
rids[i] = i + 1;
|
||||||
if (pthread_create(&readers[i], NULL, reader, &reader_ids[i]) != 0) {
|
pthread_create(&rts[i], NULL, r, &rids[i]);
|
||||||
perror("Failed to create reader thread");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// writer thread creation
|
// writer thread creation
|
||||||
for (i = 0; i < NUM_WRITERS; i++) {
|
for (i = 0; i < NW; i++) {
|
||||||
writer_ids[i] = i + 1;
|
wids[i] = i + 1;
|
||||||
if (pthread_create(&writers[i], NULL, writer, &writer_ids[i]) != 0) {
|
pthread_create(&wts[i], NULL, w_func, &wids[i]);
|
||||||
perror("Failed to create writer thread");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// thread joining
|
// thread joining
|
||||||
for (i = 0; i < NUM_READERS; i++) {
|
for (i = 0; i < NR; i++) {
|
||||||
pthread_join(readers[i], NULL);
|
pthread_join(rts[i], NULL);
|
||||||
}
|
}
|
||||||
for (i = 0; i < NUM_WRITERS; i++) {
|
for (i = 0; i < NW; i++) {
|
||||||
pthread_join(writers[i], NULL);
|
pthread_join(wts[i], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanup
|
// cleanup
|
||||||
pthread_mutex_destroy(&mutex);
|
pthread_mutex_destroy(&m);
|
||||||
sem_destroy(&wrt);
|
sem_destroy(&w);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue