rws.c
· 1.8 KiB · C
Raw
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
/*
This program provides a possible solution for the first readers-writers problem
using a mutex and a semaphore.
10 readers and 5 writers are used to demonstrate the solution. Feel free to
change these values.
*/
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;
void *writer(void *wno)
{
sem_wait(&wrt);
cnt = cnt * 2;
printf("Writer %d modified cnt to %d\n", *((int *)wno), cnt);
sem_post(&wrt);
return NULL;
}
void *reader(void *rno)
{
// Reader acquires the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader++;
if (numreader == 1) {
sem_wait(&wrt); // If this is the first reader, it blocks the writer
}
pthread_mutex_unlock(&mutex);
// Reading section
printf("Reader %d: read cnt as %d\n", *((int *)rno), cnt);
// Reader acquires the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader--;
if (numreader == 0) {
sem_post(&wrt); // If this is the last reader, it wakes up the writer
}
pthread_mutex_unlock(&mutex);
return NULL;
}
int main()
{
pthread_t read_t[10], write_t[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt, 0, 1);
int a[10] = {1,2,3,4,5,6,7,8,9,10}; // Just used for numbering readers/writers
for (int i = 0; i < 10; i++) {
pthread_create(&read_t[i], NULL, reader, (void *)&a[i]);
}
for (int i = 0; i < 5; i++) {
pthread_create(&write_t[i], NULL, writer, (void *)&a[i]);
}
for (int i = 0; i < 10; i++) {
pthread_join(read_t[i], NULL);
}
for (int i = 0; i < 5; i++) {
pthread_join(write_t[i], NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;
}
| 1 | #include <pthread.h> |
| 2 | #include <semaphore.h> |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | /* |
| 6 | This program provides a possible solution for the first readers-writers problem |
| 7 | using a mutex and a semaphore. |
| 8 | |
| 9 | 10 readers and 5 writers are used to demonstrate the solution. Feel free to |
| 10 | change these values. |
| 11 | */ |
| 12 | |
| 13 | sem_t wrt; |
| 14 | pthread_mutex_t mutex; |
| 15 | int cnt = 1; |
| 16 | int numreader = 0; |
| 17 | |
| 18 | void *writer(void *wno) |
| 19 | { |
| 20 | sem_wait(&wrt); |
| 21 | cnt = cnt * 2; |
| 22 | printf("Writer %d modified cnt to %d\n", *((int *)wno), cnt); |
| 23 | sem_post(&wrt); |
| 24 | return NULL; |
| 25 | } |
| 26 | |
| 27 | void *reader(void *rno) |
| 28 | { |
| 29 | // Reader acquires the lock before modifying numreader |
| 30 | pthread_mutex_lock(&mutex); |
| 31 | numreader++; |
| 32 | if (numreader == 1) { |
| 33 | sem_wait(&wrt); // If this is the first reader, it blocks the writer |
| 34 | } |
| 35 | pthread_mutex_unlock(&mutex); |
| 36 | |
| 37 | // Reading section |
| 38 | printf("Reader %d: read cnt as %d\n", *((int *)rno), cnt); |
| 39 | |
| 40 | // Reader acquires the lock before modifying numreader |
| 41 | pthread_mutex_lock(&mutex); |
| 42 | numreader--; |
| 43 | if (numreader == 0) { |
| 44 | sem_post(&wrt); // If this is the last reader, it wakes up the writer |
| 45 | } |
| 46 | pthread_mutex_unlock(&mutex); |
| 47 | |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | int main() |
| 52 | { |
| 53 | pthread_t read_t[10], write_t[5]; |
| 54 | pthread_mutex_init(&mutex, NULL); |
| 55 | sem_init(&wrt, 0, 1); |
| 56 | |
| 57 | int a[10] = {1,2,3,4,5,6,7,8,9,10}; // Just used for numbering readers/writers |
| 58 | |
| 59 | for (int i = 0; i < 10; i++) { |
| 60 | pthread_create(&read_t[i], NULL, reader, (void *)&a[i]); |
| 61 | } |
| 62 | |
| 63 | for (int i = 0; i < 5; i++) { |
| 64 | pthread_create(&write_t[i], NULL, writer, (void *)&a[i]); |
| 65 | } |
| 66 | |
| 67 | for (int i = 0; i < 10; i++) { |
| 68 | pthread_join(read_t[i], NULL); |
| 69 | } |
| 70 | |
| 71 | for (int i = 0; i < 5; i++) { |
| 72 | pthread_join(write_t[i], NULL); |
| 73 | } |
| 74 | |
| 75 | pthread_mutex_destroy(&mutex); |
| 76 | sem_destroy(&wrt); |
| 77 | |
| 78 | return 0; |
| 79 | } |