Resources Given: Both of the problems described later (i.e., readers/writer and
ID: 3909633 • Letter: R
Question
Resources Given:
Both of the problems described later (i.e., readers/writer and meetup) will involve
the reading and writing of “resources”. Normally I would allow you to declare
character arrays for this, but for this assignment we need to introduce a bit of
latency into our read to memory and writes from memory. Therefore for Tasks 1
and 2 below you will be required to use a new type provided to you named
resource_t for all shared data (besides Pthreads synchronization constructs):
• init_resource(resource_t *, char *) accepts an address to a resource_t instance
plus a character array (i.e., string) to be used as a label for that resource.
• read_resource(resource_t *, char *, int) accepts an address to a resource_t
instance and a character array, where the contents of the resource are to be
copied into the character array. There is a delay before the read is completed.
The last parameter is the size of the character array.
• write_resource(resource_t *, char *, int) accepts an address to a resource_t
instance and a character array, where the contents of the character array are
to be copied into the resource. There is a delay before the write is completed.
• print_stats() accepts an address to a resource_t instance and outputs statistics
such as the number of reads and writes performed on the resource instance.
The last parameter is the size of the character array.
Within rw.c you are to complete the following three functions.
1. rw_read(char *value, int len): As long as there is no thread writing the resource, this function will read the resource variable data and copy it into value via a call to read_resource(). (The len parameter is the size of the character array passed in as the argument value.) If there are threads writing, then the thread calling rw_read must be blocked until the writer is finished.
2. rw_write(char *value, int len): As long as there are no threads reading the resource, this function will write the value into the resource variable data. via a call to write_resource(). (The len field is the size of the character array passed in as the first argument.) If there are threads reading, then the thread that has called rw_write must block until the readers are finished.
3. initialize_readers_writer(): Any code for initializing synchronization constructs or other variables needed for the correct operation of your readers/writer solution must appear in this function. It is called from within myserver.c. You can assume that there is a single variable in rw.c that will be read and written, and that it is named data (i.e., an instance of resource_t named data in rw.c has file scope).
Explanation / Answer
rw.h
#ifndef _RW_H_
#define _RW_H_
#define MAX_VALUE_LEN 80
void initialize_readers_writer();
void rw_read(char *, int);
void rw_write(char *, int);
void read_resource();
#endif
rw.c
/*Required Headers*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include "rw.h"
#include "resource.h"
/*
* Declarations for reader-writer shared variables -- plus concurrency-control
* variables -- must START here.
*/
static resource_t data;
static pthread_mutex_t m;
int writers = 0;
int readers = 0;
/*
* Initialize the shared structures, including those used for
* synchronization.
*/
void initialize_readers_writer() {
pthread_mutex_init(&m, NULL);
}
void rw_read(char *value, int len) {
pthread_once(&m, initialize_readers_writer);
pthread_mutex_lock(&m);
/*
while( !(writers == 0)){
pthread_cond_wait()
// work here
pthread_mutex_unlock(&m);
}
*/
}
void rw_write(char *value, int len) {
printf("NOTHING IMPLEMENTED YET FOR rw_write ");
}
resource.c
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "resource.h"
void init_resource(resource_t *r, char *l) {
assert( r != NULL );
memset(r->value, 0, MAX_RESOURCE_SIZE);
memset(r->label, 0, MAX_LABEL_LEN);
strncpy(r->label, l, MAX_LABEL_LEN-1);
r->num_writes = 0;
r->num_reads = 0;
}
void read_resource(resource_t *r, char *v, int len) {
assert( r != NULL );
memcpy(v, r->value,
(MAX_RESOURCE_SIZE < len ? MAX_RESOURCE_SIZE : len));
sleep(DELAY_READ);
r->num_reads++;
}
void write_resource(resource_t *r, char *v, int len) {
assert( r != NULL );
memcpy(r->value, v,
(MAX_RESOURCE_SIZE < len ? MAX_RESOURCE_SIZE : len));
sleep(DELAY_WRITE);
r->num_writes++;
}
void print_stats(resource_t *r) {
assert( r != NULL );
printf("Resource: %s; # reads: %d; # writes: %d ",
r->label, r->num_reads, r->num_writes);
}
resource.h
#ifndef _RESOURCE_H_
#define _RESOURCE_H_
#define DELAY_READ 2 /* two seconds */
#define DELAY_WRITE 4 /* five seconds */
#define MAX_RESOURCE_SIZE 1000
#define MAX_LABEL_LEN 80
typedef struct resource {
char value[MAX_RESOURCE_SIZE];
char label[MAX_LABEL_LEN];
int num_reads;
int num_writes;
} resource_t;
void init_resource(resource_t *, char *);
void read_resource(resource_t *, char*, int);
void write_resource(resource_t *, char *, int);
void print_stats(resource_t *);
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.