Download c source code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0

#define _DEFAULT_SOURCE

#include <assert.h>
#include <ctype.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct shared_data {
  size_t counter;
} shared_data_t;

void* reader(void* data);
void* writer(void* data);
int create_threads(shared_data_t* shared_data);

int main() {
  int error = EXIT_SUCCESS;
  shared_data_t* shared_data = (shared_data_t*)calloc(1, sizeof(shared_data_t));
  if (shared_data) {
    if (error == EXIT_SUCCESS) {
      error = create_threads(shared_data);
    } else {
      fprintf(stderr, "error: could not create rwlock\n");
      error = 11;
    }
    free(shared_data);
  } else {
    fprintf(stderr, "error: could not allocated shared memory\n");
    error = 12;
  }
  return error;
}

int resize(void** array, size_t* capacity, const size_t count, size_t elem_sz) {
  if (count < *capacity) {
    return EXIT_SUCCESS;
  }
  const size_t new_capacity = 10 * (*capacity ? *capacity : 1);
  void* new_arr = realloc(*array, new_capacity * elem_sz);
  if (new_arr == NULL) {
    return EXIT_FAILURE;
  }
  *capacity = new_capacity;
  *array = new_arr;
  return EXIT_SUCCESS;
}

int create_threads(shared_data_t* shared_data) {
  int error = EXIT_SUCCESS;
  size_t capacity = 0;
  size_t count = 0;
  pthread_t* threads = NULL;
  char ch = '\0';
  while (scanf("%c", &ch) == 1) {
    error = resize((void**)&threads, &capacity, count, sizeof(pthread_t));
    if (error) {
      fprintf(stderr, "error: could not allocate memory for threads\n");
      error = 22;
      break;
    }
    if (toupper(ch) == 'R') {
      error = pthread_create(&threads[count++], NULL, reader, shared_data);
    } else if (toupper(ch) == 'W') {
      error = pthread_create(&threads[count++], NULL, writer, shared_data);
    }  // Silently ignoring other chars
    if (error) {
      fprintf(stderr, "error: could not create thread %zu\n", count);
      error = 21;
      break;
    }
  }
  for (size_t index = 0; index < count; ++index) {
    pthread_join(threads[index], NULL);
  }
  free(threads);
  return error;
}

void* reader(void* data) {
  shared_data_t *shared_data = (shared_data_t*)data;
  const size_t value = shared_data->counter;
  printf("Reader got %zu\n", value);
  return NULL;
}

void* writer(void* data) {
  shared_data_t *shared_data = (shared_data_t*)data;
  const size_t value = ++shared_data->counter;
  printf("Writer increased to %zu\n", value);
  return NULL;
}