Commit f01d4875 authored by Roshan Rabinarayan's avatar Roshan Rabinarayan

code for file

parent ddb7f72c
#include<semaphore.h>
#include<stdio.h> #include<stdio.h>
#include <string.h> #include<stdlib.h>
#include <stdlib.h> #include<unistd.h>
#include "KVMessageFormat.h" #include<pthread.h>
struct message* get(char m,char *key) #include <stdint.h>
{ #include <limits.h>
if(!m || key==NULL ) #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;
int modulus(char *num, int size, int divisor) {
int rem = 0;
while (size-- > 0) {
rem = ((UCHAR_MAX + 1)*rem + *num) % divisor;
num++;
}
return rem;
}
void file_del(char *key)
{
int index=modulus(key,256,setSize);
sem_wait(&mutex[index]);
FILE * fp;
char * line = NULL;
size_t len = 0;
int read;
fp = fdopen(fds[index], "a+");
if (fp == NULL)
printf("\n[Invalid file]\n");
int position=0;
while ((read = getline(&line, &len, fp)) != -1)
{ {
printf("Invalid parameters in get()");
return NULL; if(strstr(line,key)!=NULL && strstr(strstr(line,key),":")!=NULL)
{
fseek(fp,position,SEEK_SET);
break;
}
else
{
position+=len;
}
} }
struct message *request= malloc(sizeof(struct message)); //fclose(fp);
request->status=m; if (line)
memcpy(request->key,key,256); //copied the complete key free(line);
if(strlen(request->key)<256) sem_wait(&readerLocks[index]);
readercount--;
if(readercount==0)
{ {
request->key[strlen(request->key)]='\0'; sem_post(&mutex[index]);
} }
request->value[0]='\0'; sem_post(&readerLocks[index]);
return request;
} }
struct message* put(char m,char *key,char *value) void file_get(char *key,char *value)
{ {
if(!m || (key==NULL && value==NULL))
{ int index=modulus(key,256,setSize);
printf("Invalid parameters in put()"); sem_wait(&readerLocks[index]);
return NULL; 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");
struct message *request= malloc(sizeof(struct message)); while ((read = getline(&line, &len, fp)) != -1)
request->status=m;
memcpy(request->key,key,256); //copied the complete key
if(strlen(request->key)<256)
{ {
request->key[strlen(request->key)]='\0'; if(strstr(line,key)!=NULL && strstr(strstr(line,key),":")!=NULL)
{
snprintf(value,sizeof((strchr(line,':')+1)),"%s",(strchr(line,':')+1));
}
} }
fclose(fp);
memcpy(request->value,value,256); //copied the complete value if (line)
if(strlen(request->key)<256) //to pad with \0 free(line);
sem_wait(&readerLocks[index]);
readercount--;
if(readercount==0)
{ {
request->key[strlen(request->value)]='\0'; sem_post(&mutex[index]);
} }
sem_post(&readerLocks[index]);
return request;
} }
struct message *del(char m,char *key){ void file_put(char *key,char *value)
if(!m || key==NULL ) {
int index=modulus(key,256,setSize);
sem_wait(&mutex[index]);
printf("\n[Write to File: %d]\n",index);
char line[514];
int i=0;
for(i=0;i<256;i++)
{ {
printf("Invalid parameters in del()"); if(key[i]!='\0')
return NULL; {
line[i]=key[i];
}
else
{
break;
}
} }
struct message *request= malloc(sizeof(struct message)); int k=0;
request->status=m; line[i++]=':';
memcpy(request->key,key,256); //copied the complete key for(;i<512;i++)
if(strlen(request->key)<256)
{ {
request->key[strlen(request->key)]='\0'; if(value[k]!='\0')
{
line[i]=value[k++];
}
else
{
break;
}
} }
request->value[0]='\0'; line[i]='\n';
return request; if(write(fds[index],line,strlen(line))<0)
}
struct message *request(char status,char* key,char* value)
{
if(!status ||(key==NULL && value==NULL) )
{ {
printf("Invalid parameters in request()"); printf("\n[Unable to Write to File]\n");
return NULL;
} }
struct message *requestMessage= malloc(sizeof(struct message));
requestMessage->status=status; sem_post(&mutex[index]);
}
memcpy(requestMessage->key,key,256); //copied the complete key 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
if(strlen(requestMessage->key)<256) */
int n2=0;
char key[256]="24";
char value[256]="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++)
{ {
requestMessage->key[strlen(requestMessage->key)]='\0'; snprintf(fileName,sizeof(fileName),"File%d.txt",i);
fds[i]=open(fileName, O_CREAT|O_RDWR|O_APPEND,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;
} }
if(value!=NULL) //file_put(key,value);
value[0]='\0';
// file_get(key,value);
/* for(i=0;i<setSize-1;i++)
{ {
memcpy(requestMessage->value,value,256); //copied the complete value close(fds[i]);
} } */
// printf("main vala hai ye!!");
if(strlen(requestMessage->value)<256) //to pad with \0 // 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++)
{ {
requestMessage->value[strlen(requestMessage->value)]='\0'; pthread_create(&writerthreads[i],NULL,reader,NULL);
pthread_create(&readerthreads[i],NULL,writer,NULL);
} }
printf("[Message Generated at Client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value); for(i=0;i<n2;i++)
return requestMessage;
}
void main()
{
char message[256];
for(int i=0;i<30;i++)
{ {
message[i]='a'; pthread_join(writerthreads[i],NULL);
pthread_join(readerthreads[i],NULL);
} }
message[sizeof(message)]='\0'; */
request('2',message,message);
char c ='2';
int status=(int)c;
printf("%d",status);
//creating a message }
}
\ No newline at end of file
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