Print Friendly and PDF
Detailed overview of Reader Writer Problem With Example and implementation
In computer science, a readers-writer (RW) or shared-exclusive lock (also known as a multiple readers/single-writer lock or multi-reader lock) is a synchronization primitive that solves one of the readers-writers problems. 

An RW lock allows concurrent access for read-only operations, while write operations require exclusive access. This means that multiple threads can read the data in parallel but an exclusive lock is needed for writing or modifying data.  When a writer is writing the data, all other writers or readers will be blocked until the writer is finished writing. A common use might be to control access to a data structure in memory that cannot be updated atomically and is invalid (and should not be read by another thread) until the update is complete.

Readers–writer locks are usually constructed on top of mutexes and condition variables, or on top of semaphores.

The read-copy-update (RCU) algorithm is one solution to the readers-writers problem. RCU is wait-free for readers. The Linux kernel implements a special solution for few writers called seqlock.


Reader Implementation

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "shm_com.h"
#include <string.h>
#include <semaphore.h>
int main(){
int running = 1;int res;
void *shared_memory = (void *)0;struct shared_use_st *shared_stuff;
int shmid;
srand((unsigned int)getpid());shmid = shmget((key_t)1124, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1) 
{fprintf(stderr, "shmget Failed\n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1)
 {
fprintf(stderr, "shmat Failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %x\n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
shared_stuff->flag = 0;
while(running)
 {
if (shared_stuff->flag)
{
printf("You wrote: %s", shared_stuff->some_text);
sleep( rand() % 4 );sem_wait(shared_stuff->bin_sem);
shared_stuff->flag = 0;
if (strncmp(shared_stuff->some_text, "end", 3) == 0) 
{
running = 0;
}
}
}
if (shmdt(shared_memory) == -1)
 {
fprintf(stderr, "shmdt Failed\n");
exit(EXIT_FAILURE);
}if (shmctl(shmid, IPC_RMID, 0) == -1) 
{
fprintf(stderr, "shmctl(IPC_RMID) Failed\n");exit(EXIT_FAILURE);
}
sem_post(shared_stuff->bin_sem);
exit(EXIT_SUCCESS);
}
Semaphore

#include <semaphore.h>#define TEXT_SZ 2048
struct shared_use_st
 {
sem_t *bin_sem;
char some_text[TEXT_SZ];
int flag;
int res;
};
Writer Implementation

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include <sys/shm.h>
#include <sys/shm.h>
#include "shm_com.h"
#include <semaphore.h>
int main()
{
int run = 1;
int res;
void *shared_mem = (void *)0;
struct shared_use_st *shared_stuff;
char buffer[BUFSIZ];
int shmid;

shmid = shmget((key_t)1124, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1) {
fprintf(stderr, "Semaphore get Failed\n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1) {
fprintf(stderr, "shmat Failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %x\n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
while(run) {
while(shared_stuff->flag == 1)
{sleep(1);
sem_post(shared_stuff->bin_sem);
printf("Waiting for client \n");
}
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->some_text, buffer, TEXT_SZ);
shared_stuff->flag = 1;
if (strncmp(buffer, "end", 3) == 0) 
{
run = 0;
}
}
if (shmdt(shared_memory) == -1) 
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
sem_wait(shared_stuff->bin_sem);
exit(EXIT_SUCCESS);}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: