Commit c43a4bba authored by Samarth Joshi's avatar Samarth Joshi

Deleting temp files and setting setsize in storagehandler to 4

parent 47235b37
File deleted
File deleted
File deleted
......@@ -192,13 +192,18 @@ int main(int argc, char const *argv[])
{
int sock;
int max_concurrent = 100;
int n = 5;
int n = 2;
pthread_t *seige_threads;
int *requestcompleted;
int *requestsent;
int sum;
int seconds = 1;
int i;
int seige_mode = 0;
if(argc == 2) {
seige_mode = 1;
}
settings = (struct config *) malloc(sizeof(struct config));
if(read_config(settings)<0) {
......@@ -206,7 +211,7 @@ int main(int argc, char const *argv[])
return -1;
}
if(1) {
if(seige_mode==0) {
sock = client_init();
interactive(sock);
return 0;
......
File deleted
......@@ -12,7 +12,7 @@
#include <sys/types.h>
int *fds;
int setSize=2;
int setSize=4;
int *readCounters;
sem_t *mutex;
sem_t *readerLocks;
......
max_threads=5
no_of_dsads=10
adsasaddsa=22223
listening_port=80
listening_port=8000
cache_size=15
thread_pool_size=10
File deleted
#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include <stdint.h>
#include <limits.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>
#include <sys/types.h>
int *fds;
int setSize=2;
int *readCounters;
sem_t *mutex;
sem_t *readerLocks;
sem_t x,y;
pthread_t tid;
pthread_t writerthreads[100],readerthreads[100];
int readercount = 0;
unsigned modulus( unsigned char *num, size_t size, unsigned divisor) {
unsigned rem = 0;
int i=0;
while(i<divisor)
{
rem+=num[i]%divisor;
i++;
}
return rem%divisor;
}
void file_del( char *key)
{
int index=modulus(key,256,setSize);
fflush(stdout);
int length=0;
sem_wait(&mutex[index]);
char blankspace[512];
char ch;
int k=-1;
lseek(fds[index], 0, SEEK_SET);
memset(blankspace,0,512);
char *line;
char temp[514];
size_t len=0;
off_t position=0;
FILE *fp=fdopen(fds[index],"rb+");
while ((getline(&line, &len, fp)) != -1)
{
memcpy(temp,line,strlen(line)-1);
char *fkey=strtok(line,":");
char *fvalue=strtok(NULL,":");
if(strcmp(key,fkey)==0){
lseek(fds[index],position,SEEK_SET);
write(fds[index],blankspace,strlen(temp));
break;
}
position=ftell(fp);
}
printf("length %d",length);
sem_post(&mutex[index]);
}
int file_get(char *key, char *value)
{
/* Gets the value stored at offset */
/* Does not depend on key argument */
int found =0;
int index=modulus(key,strlen(key),setSize);
lseek(fds[index],0,SEEK_SET);
sem_wait(&readerLocks[index]);
readCounters[index]+=1;
if(readCounters[index]==1)
sem_wait(&mutex[index]);
sem_post(&readerLocks[index]);
char *line;
size_t len=0;
FILE *fp=fdopen(fds[index],"rb+");
while ((getline(&line, &len, fp)) != -1)
{
char *fkey=strtok(line,":");
char *fvalue=strtok(NULL,":");
if(strcmp(key,fkey)==0){
memcpy(value,fvalue,strlen(fvalue)-1);
found=1;
break;
}
}
sem_wait(&readerLocks[index]);
readCounters[index]-=1;
if(readCounters[index]==0)
{
sem_post(&mutex[index]);
}
sem_post(&readerLocks[index]);
return found;
}
off_t file_put(char *key,char *value)
{
/*
if found then ask the offset where it is present and if the value noy matches with the present value ,update the given line
if not present then search for empty line and insert there!
*/
unsigned int index=modulus(key,256,setSize);
off_t position;
//sem_wait(&mutex[index]);
printf("[Write to File: %d]\n",index);
char *line;
char lin[514];
char blankspace[513];
memset(blankspace, 0, sizeof(blankspace));
size_t len=0;
FILE *fp=fdopen(fds[index],"rb+");
while ((getline(&line, &len, fp)) != -1)
{
int temp =strlen(line);
char *fkey=strtok(line,":");
if(strcmp(key,fkey)==0)
{
fseek(fp,position,SEEK_SET);
fwrite(blankspace,sizeof(blankspace),temp,fp);
break;
}
position=ftell(fp);
}
snprintf(lin,sizeof(lin),"%s:%s\n",key,value);
fwrite(lin,sizeof(lin),strlen(lin),fp);
//sem_post(&mutex[index]);
return position;
}
int main()
{
/*
define the array of file descriptors depending on the prefix
define the array of readCount as well as the semaphore (read x and write y) for the same
PUT,DEL would use write lock
GET would use read lock
each write should return the line number
*/
/* int n2=0;
char prevkey[256];
memset(prevkey,0,sizeof(prevkey));
memcpy(prevkey,"w",1);
char newkey[256];
memset(newkey,0,sizeof(newkey));
memcpy(newkey,"x",1);
printf("key:%s\n",prevkey);
char key[256]="25";
char value[256]="value2";
off_t offset;
bzero(key+strlen(key),sizeof(key)-strlen(key));
bzero(value+strlen(value),sizeof(value)-strlen(value));
fds=(int *)malloc(sizeof(int)*setSize);
readCounters=(int *)malloc(sizeof(int)*setSize);
readerLocks=(sem_t *)malloc(sizeof(sem_t)*setSize);
mutex=(sem_t *)malloc(sizeof(sem_t)*setSize);
int i=0;
char fileName[setSize+20];
for(i=0;i<setSize;i++)
{
snprintf(fileName,sizeof(fileName),"File%d.txt",i);
fds[i]=open(fileName, O_CREAT|O_RDWR,S_IRWXU);
if(fds[i]<0)
{
printf("\n[Unable to Open File%d.txt]\n",i);
}
sem_init(&readerLocks[i],0,1);
sem_init(&mutex[i],0,1);
readCounters[i]=0;
}
i=0;
*/
//printf("%d",modulus(prevkey,256,2));
//printf("%d",modulus(newkey,256,2));
//offset = file_put("24", "value245");
//offset = file_put("24", "value1");
//file_put("23","update1");
//file_put("24","update6969");
//file_get(prevkey, value); // Doesnot depend on key arg, returns key and value at offset 0
//printf("(%s)", value);
//file_del("29");
//file_get("24",value);
// printf("(%s)\n", value);
//file_del("21");
//printf("(%s)\n", value);
FILE *fp = fopen("config.txt","r");
char *line;
off_t len=0;
char* arrParam[20];
int arrValue[20];
int k=0;
while(getline(&line,&len,fp)!=-1)
{
char *param=strtok(line,"=");
char *value =strtok(NULL,"=");
arrParam[k]=param;
arrValue[k++]=atoi(value);
printf("%s:%d\n",arrParam[k-1],arrValue[k-1]);
}
}
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