Commit d34a27c9 authored by Samarth Joshi's avatar Samarth Joshi

Adding blank space as padding

parent 1ba74366
No preview for this file type
File added
...@@ -20,6 +20,7 @@ sem_t x,y; ...@@ -20,6 +20,7 @@ sem_t x,y;
pthread_t tid; pthread_t tid;
pthread_t writerthreads[100],readerthreads[100]; pthread_t writerthreads[100],readerthreads[100];
int readercount = 0; int readercount = 0;
int modulus(char *num, int size, int divisor) { int modulus(char *num, int size, int divisor) {
int rem = 0; int rem = 0;
...@@ -29,35 +30,18 @@ int modulus(char *num, int size, int divisor) { ...@@ -29,35 +30,18 @@ int modulus(char *num, int size, int divisor) {
} }
return rem; return rem;
} }
void file_del(char *key) void file_del(off_t offset, char *key)
{ {
int index=modulus(key,256,setSize); int index=modulus(key,256,setSize);
sem_wait(&mutex[index]); sem_wait(&mutex[index]);
FILE * fp;
char * line = malloc(sizeof(char)*514);
size_t len = 0;
int read;
fp = fdopen(fds[index], "r+b");
if (fp == NULL)
printf("\n[Invalid file]\n");
int position=ftell(fp);
while (fscanf(fp,"%s",line) != EOF )
{
if(strstr(line,key)!=NULL && strstr(strstr(line,key),":")!=NULL)
{
//printf("%s\n",line);
printf("%d", fseek(fp, position, SEEK_SET)); char blankspace[512];
fprintf(fp, "%s", "\0"); for(int i=0; i<512; i++) {
break; blankspace[i] = 0;
}
position=ftell(fp);
printf("\n%d ", position);
} }
if (line) lseek(fds[index], offset, SEEK_SET);
free(line); write(fds[index], blankspace, 512);
sem_wait(&readerLocks[index]); sem_wait(&readerLocks[index]);
readercount--; readercount--;
if(readercount==0) if(readercount==0)
...@@ -66,8 +50,10 @@ void file_del(char *key) ...@@ -66,8 +50,10 @@ void file_del(char *key)
} }
sem_post(&readerLocks[index]); sem_post(&readerLocks[index]);
} }
void file_get(char *key,char *value) void file_get(off_t offset, char *key, char *value)
{ {
/* Gets the value stored at offset */
/* Does not depend on key argument */
int index=modulus(key,256,setSize); int index=modulus(key,256,setSize);
sem_wait(&readerLocks[index]); sem_wait(&readerLocks[index]);
...@@ -75,25 +61,11 @@ void file_get(char *key,char *value) ...@@ -75,25 +61,11 @@ void file_get(char *key,char *value)
if(readCounters[index]==1) if(readCounters[index]==1)
sem_wait(&mutex[index]); sem_wait(&mutex[index]);
sem_post(&readerLocks[index]); sem_post(&readerLocks[index]);
FILE * fp;
char * line = NULL;
size_t len = 0;
int read;
fp = fdopen(fds[index], "r"); lseek(fds[index], offset, SEEK_SET);
if (fp == NULL) read(fds[index], key, 256);
printf("\n[Invalid file]\n"); read(fds[index], value, 256);
while ((read = getline(&line, &len, fp)) != -1)
{
if(strstr(line,key)!=NULL && strstr(strstr(line,key),":")!=NULL)
{
snprintf(value,sizeof((strchr(line,':')+1)),"%s",(strchr(line,':')+1));
}
}
fclose(fp);
if (line)
free(line);
sem_wait(&readerLocks[index]); sem_wait(&readerLocks[index]);
readercount--; readercount--;
if(readercount==0) if(readercount==0)
...@@ -102,21 +74,22 @@ void file_get(char *key,char *value) ...@@ -102,21 +74,22 @@ void file_get(char *key,char *value)
} }
sem_post(&readerLocks[index]); sem_post(&readerLocks[index]);
} }
void file_put(char *key,char *value)
{ off_t file_put(char *key,char *value) {
int index=modulus(key,256,setSize); int index=modulus(key,256,setSize);
int bytes, rembytes, i;
off_t position;
sem_wait(&mutex[index]); sem_wait(&mutex[index]);
printf("\n[Write to File: %d]\n",index); printf("[Write to File: %d]\n",index);
char line[514]; position = lseek(fds[index], 0, SEEK_END);
memcpy(line,key,256); write(fds[index], key, 256);
line[strlen(key)]=':'; write(fds[index], value, 256);
memcpy(line+strlen(line),value,256); printf("bytes : %d\n",bytes);
line[strlen(line)]='\n';
if(write(fds[index],line,strlen(line))<0)
{
printf("\n[Unable to Write to File]\n");
}
sem_post(&mutex[index]); sem_post(&mutex[index]);
return position;
} }
int main() int main()
{ {
...@@ -128,8 +101,10 @@ int main() ...@@ -128,8 +101,10 @@ int main()
each write should return the line number each write should return the line number
*/ */
int n2=0; int n2=0;
char key[256]="24"; char prevkey[256]="24";
char value[256]="value"; char key[256]="24sadasdasdasdasdsad";
char value[256]="valueasdasdad";
off_t offset;
bzero(key+strlen(key),sizeof(key)-strlen(key)); bzero(key+strlen(key),sizeof(key)-strlen(key));
bzero(value+strlen(value),sizeof(value)-strlen(value)); bzero(value+strlen(value),sizeof(value)-strlen(value));
fds=(int *)malloc(sizeof(int)*setSize); fds=(int *)malloc(sizeof(int)*setSize);
...@@ -150,33 +125,12 @@ int main() ...@@ -150,33 +125,12 @@ int main()
sem_init(&mutex[i],0,1); sem_init(&mutex[i],0,1);
readCounters[i]=0; readCounters[i]=0;
} }
//file_put(key,value);
value[0]='\0';
// file_get(key,value); offset = file_put(prevkey, value);
/* for(i=0;i<setSize-1;i++) offset = file_put(key, value);
{ file_get(0, prevkey, value); // Doesnot depend on key arg, returns key and value at offset 0
close(fds[i]); printf("%s\n", value);
} */ file_del(0, prevkey);
// printf("main vala hai ye!!");
// puts(value);
file_del(key);
/*
printf("%u",modulus(key,256,3));
//scanf("%d",&n2);
printf("\n");
int n1[n2];
sem_init(&x,0,1);
sem_init(&y,0,1);
for(i=0;i<n2;i++)
{
pthread_create(&writerthreads[i],NULL,reader,NULL);
pthread_create(&readerthreads[i],NULL,writer,NULL);
}
for(i=0;i<n2;i++)
{
pthread_join(writerthreads[i],NULL);
pthread_join(readerthreads[i],NULL);
}
*/
} }
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