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;
pthread_t tid;
pthread_t writerthreads[100],readerthreads[100];
int readercount = 0;
int modulus(char *num, int size, int divisor) {
int rem = 0;
......@@ -29,35 +30,18 @@ int modulus(char *num, int size, int divisor) {
}
return rem;
}
void file_del(char *key)
void file_del(off_t offset, char *key)
{
int index=modulus(key,256,setSize);
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));
fprintf(fp, "%s", "\0");
break;
}
position=ftell(fp);
printf("\n%d ", position);
char blankspace[512];
for(int i=0; i<512; i++) {
blankspace[i] = 0;
}
if (line)
free(line);
lseek(fds[index], offset, SEEK_SET);
write(fds[index], blankspace, 512);
sem_wait(&readerLocks[index]);
readercount--;
if(readercount==0)
......@@ -66,34 +50,22 @@ void file_del(char *key)
}
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);
sem_wait(&readerLocks[index]);
readCounters[index]++;
if(readCounters[index]==1)
sem_wait(&mutex[index]);
sem_post(&readerLocks[index]);
FILE * fp;
char * line = NULL;
size_t len = 0;
int read;
fp = fdopen(fds[index], "r");
if (fp == NULL)
printf("\n[Invalid file]\n");
lseek(fds[index], offset, SEEK_SET);
read(fds[index], key, 256);
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]);
readercount--;
if(readercount==0)
......@@ -102,21 +74,22 @@ void file_get(char *key,char *value)
}
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 bytes, rembytes, i;
off_t position;
sem_wait(&mutex[index]);
printf("\n[Write to File: %d]\n",index);
char line[514];
memcpy(line,key,256);
line[strlen(key)]=':';
memcpy(line+strlen(line),value,256);
line[strlen(line)]='\n';
if(write(fds[index],line,strlen(line))<0)
{
printf("\n[Unable to Write to File]\n");
}
printf("[Write to File: %d]\n",index);
position = lseek(fds[index], 0, SEEK_END);
write(fds[index], key, 256);
write(fds[index], value, 256);
printf("bytes : %d\n",bytes);
sem_post(&mutex[index]);
return position;
}
int main()
{
......@@ -128,8 +101,10 @@ int main()
each write should return the line number
*/
int n2=0;
char key[256]="24";
char value[256]="value";
char prevkey[256]="24";
char key[256]="24sadasdasdasdasdsad";
char value[256]="valueasdasdad";
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);
......@@ -150,33 +125,12 @@ int main()
sem_init(&mutex[i],0,1);
readCounters[i]=0;
}
//file_put(key,value);
value[0]='\0';
// file_get(key,value);
/* for(i=0;i<setSize-1;i++)
{
close(fds[i]);
} */
// 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);
}
*/
offset = file_put(prevkey, value);
offset = file_put(key, value);
file_get(0, prevkey, value); // Doesnot depend on key arg, returns key and value at offset 0
printf("%s\n", value);
file_del(0, prevkey);
}
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