Commit c3382784 authored by Samarth Joshi's avatar Samarth Joshi

Fix bug in LRU, cache_put copies pointer of key to array->key instead of key value

parent ddb7f72c
No preview for this file type
...@@ -36,16 +36,18 @@ int main(int argc, char const *argv[]) ...@@ -36,16 +36,18 @@ int main(int argc, char const *argv[])
printf("\nConnection Failed \n"); printf("\nConnection Failed \n");
return -1; return -1;
} }
char key[256]="abc"; char key1[256]="abc";
char value[256]="cde"; char value1[256]="cde";
char error[256]; char error1[256];
char key2[256]="test";
char value2[256]="cdde";
char error2[256];
//printf("%d",(int)put(sock,key, value,error)); //printf("%d",(int)put(sock,key, value,error));
//printf("%d",(int)del(sock,key,error)); //printf("%d",(int)del(sock,key,error));
//printf("%d",(int)get(sock,key, value,error)); //printf("%d",(int)get(sock,key, value,error));
//while (1) { //while (1) {
for(int i=0; i<10; i++) { printf("%d",(int)put(sock, key1, value1, error1));
printf("%d",(int)put(sock, key, value, error)); printf("%d",(int)put(sock, key2, value2, error2));
}
return 0; return 0;
} }
\ No newline at end of file
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
#include "LRU.h" #include "LRU.h"
#include "KVMessageFormat.h" #include "KVMessageFormat.h"
#define MAX_EVENTS 10 #define MAX_EVENTS 10
#define DEBUG (1) #define DEBUG (0)
void gen_random(char *s, const int len) { void gen_random(char *s, const int len) {
static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
...@@ -38,9 +38,8 @@ void *worker(void *args) { ...@@ -38,9 +38,8 @@ void *worker(void *args) {
if DEBUG printf("[%s] Thread started!\n", name); if DEBUG printf("[%s] Thread started!\n", name);
epollfd = epoll_create1(0); epollfd = epoll_create1(0);
if (epollfd == -1) { while (epollfd == -1) {
perror("epoll_create1"); epollfd = epoll_create1(0);
exit(EXIT_FAILURE);
} }
/* Add the pipe which we get from main thread */ /* Add the pipe which we get from main thread */
...@@ -121,7 +120,6 @@ void *worker(void *args) { ...@@ -121,7 +120,6 @@ void *worker(void *args) {
break; break;
} }
if DEBUG print_cache();
requestMessage->status=200; requestMessage->status=200;
...@@ -144,12 +142,12 @@ int main (int argc, int argv) { ...@@ -144,12 +142,12 @@ int main (int argc, int argv) {
int i; int i;
int next_thread_to_assign = 0; int next_thread_to_assign = 0;
pthread_t *worker_threads; pthread_t *worker_threads;
int pool_thread_size = 5; // TODO: get pool thread size from config file int pool_thread_size = 1; // TODO: get pool thread size from config file
int sockfd, newsockfd, portno, clilen, n; int sockfd, newsockfd, portno, clilen, n;
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
//int *pipes = (int*) malloc(pool_thread_size * 2 * sizeof(pipes)); //int *pipes = (int*) malloc(pool_thread_size * 2 * sizeof(pipes));
int pipes[5][2]; // TODO: initialize pipes dynamically int pipes[1][2]; // TODO: initialize pipes dynamically
worker_threads = malloc(pool_thread_size * sizeof(pthread_t)); worker_threads = malloc(pool_thread_size * sizeof(pthread_t));
for( i=0; i < pool_thread_size; i++ ) { for( i=0; i < pool_thread_size; i++ ) {
......
...@@ -85,17 +85,22 @@ int cache_del(char *key) ...@@ -85,17 +85,22 @@ int cache_del(char *key)
void cache_put(char *key, char *value) void cache_put(char *key, char *value)
{ {
printf("Cache before PUT: \n");
print_cache();
int indx=-1; int indx=-1;
for(int i=0;i<MAX_SIZE;i++) for(int i=0;i<MAX_SIZE;i++)
{ {
if(array[i]->key == key) if(array[i]->key!=NULL) {
{ if(strcmp(array[i]->key , key) == 0)
indx = i; {
remove_element_from_deque(key); indx = i;
break; remove_element_from_deque(key);
break;
}
} }
} }
printf("key is present at index: %d\n", indx);
if(indx == -1) if(indx == -1)
{ {
indx = find_empty_location(); indx = find_empty_location();
...@@ -104,12 +109,14 @@ void cache_put(char *key, char *value) ...@@ -104,12 +109,14 @@ void cache_put(char *key, char *value)
} }
while(ATOMIC_TEST_AND_SET(&(array[indx]->lock),1) == 1); while(ATOMIC_TEST_AND_SET(&(array[indx]->lock),1) == 1);
array[indx]->key = key; memcpy(array[indx]->key, key, KEY_SIZE);
array[indx]->value = value; memcpy(array[indx]->value, value, VAL_SIZE);
array[indx]->valid = TRUE; array[indx]->valid = TRUE;
array[indx]->modified = TRUE; array[indx]->modified = TRUE;
insert_into_queue(key); insert_into_queue(key);
CLEAR(&(array[indx]->lock),0); CLEAR(&(array[indx]->lock),0);
printf("Cache after PUT: \n");
print_cache();
} }
char* cache_get(char *key) char* cache_get(char *key)
...@@ -140,6 +147,6 @@ void init_cache() { ...@@ -140,6 +147,6 @@ void init_cache() {
void print_cache() { void print_cache() {
for(int j=0;j<MAX_SIZE;j++) for(int j=0;j<MAX_SIZE;j++)
{ {
printf("(%s) : (%s)\n", array[j]->key, array[j]->value); printf("[%d] (%s) : (%s)\n", array[j]->valid, array[j]->key, array[j]->value);
} }
} }
\ No newline at end of file
#define MAX_SIZE 10
#define KEY_SIZE 256
#define VAL_SIZE 256
#define MAX_SIZE 10
typedef enum{ typedef enum{
FALSE,TRUE FALSE,TRUE
}bool; }bool;
struct KV{ struct KV{
char *key; char key[KEY_SIZE];
char *value; char value[VAL_SIZE];
bool valid; bool valid;
bool modified; bool modified;
unsigned _Atomic lock; unsigned _Atomic lock;
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment