So I'm trying to write a program that adds items to a bounded buffer:
Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bbuffer.h"
int count;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_cond_t cv;
int bounded_buffer[BOUNDED_BUFFER_SIZE];
int pthread_cond_signal(pthread_cond_t *cv);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
void initialize_bounded_buffer() {
int status;
count = 0;
status = pthread_mutex_init(&mutex, NULL);
if (status != 0) {
fprintf(stderr, "Error creating buffer_mutex\n");
}
}
void add_to_buffer(int value) {
printf("adding\n");
pthread_mutex_lock(&mutex);
while (count == BOUNDED_BUFFER_SIZE){
printf("waiting");
pthread_cond_wait(&cond, &mutex);
}
printf("no error\n");
bounded_buffer[count] = value;
count++;
pthread_cond_signal(&cv);
printf("added\n");
printf("%d\n", count);
pthread_mutex_unlock(&mutex);
}
A problem I've been having with this is that I'm running this on a server and when I try to run the add method from this server, the value isn't the correct one and I don't know why. Here's the method which calls the bounded buffer methods:
Code:
void *server_action(void *arg) {
int comm_fd = *(int *)arg;
char str[OUTPUT_BUFFER_SIZE];
char operand[OUTPUT_BUFFER_SIZE];
char result_message[OUTPUT_BUFFER_SIZE];
char temp_result[OUTPUT_BUFFER_SIZE];
char *separator = "";
int value;
int i, sum;
pc_op_t operation;
bzero(str, OUTPUT_BUFFER_SIZE);
read(comm_fd, str, OUTPUT_BUFFER_SIZE);
operation = parse_request(str, operand);
sprintf(result_message, "%s\n", "GET OFF MY LAWN!");
switch(operation) {
case ADD:
printf("server: ADD\n");
printf("server: parameter is %s\n", operand);
/* TO DO something here for ADD */
printf("the value is %d\n", value);
add_to_buffer(value);
break;
case DEBUG:
printf("server: DEBUG\n");
return;
break;
case UNKNOWN:
default:
printf("server: UNKNOWN\n");
break;
}
send_http_response(comm_fd, result_message);
close(comm_fd);
return_thread_index(comm_fd);
}
int main() {
/* Vars for setting up heartbeat */
pthread_t heart;
int interval = 5;
/* Vars for setting up server threads */
char str[OUTPUT_BUFFER_SIZE];
int listen_fd, comm_fd;
int i;
int thread_index;
int num_threads = 0;
int bind_result;
void *result;
int status;
initialize();
/*
* First start the heartbeat -- just to convince ourselves that
* "accept()" is not blocking the whole process...
*
*/
if (pthread_create(&heart, 0, heartbeat, (void *)&interval) < 0) {
fprintf(stderr, "Could not create heartbeat thread\n");
return 1;
}
/*
* Now set up server threads as needed -- i.e., as separate clients
* connect to the server.
*/
listen_fd = setup_listener();
while (comm_fd = accept(listen_fd, (struct sockaddr*) NULL, NULL)) {
thread_index = grab_thread_index();
if (thread_index == -1) {
fprintf(stderr, "No more threads available.\n");
exit(1);
}
set_thread_fd(thread_index, comm_fd);
if (pthread_create(&thread_pool[thread_index], 0,
server_action, &comm_fd) < 0)
{
fprintf(stderr, "Could not create thread %d\n", num_threads);
exit(1);
}
printf("Created server thread %d\n", num_threads);
}
}
Now if I run the curl command on this server then no matter what I put in as the value (for example curl "localhost (port number)/?op=add&val=167") then the value is always 0 for some reason and I'm not sure what int I have to pass into my add method so that the correct value is added.