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>
printf("Invalid parameters in get()"); #include<string.h>
return NULL; #include<sys/stat.h>
} #include <sys/types.h>
struct message *request= malloc(sizeof(struct message));
request->status=m; int *fds;
memcpy(request->key,key,256); //copied the complete key int setSize=2;
if(strlen(request->key)<256) int *readCounters;
{ sem_t *mutex;
request->key[strlen(request->key)]='\0'; 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++;
} }
request->value[0]='\0'; return rem;
return request;
} }
struct message* put(char m,char *key,char *value) void file_del(char *key)
{ {
if(!m || (key==NULL && value==NULL)) 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 put()");
return NULL;
}
struct message *request= malloc(sizeof(struct message)); if(strstr(line,key)!=NULL && strstr(strstr(line,key),":")!=NULL)
request->status=m;
memcpy(request->key,key,256); //copied the complete key
if(strlen(request->key)<256)
{ {
request->key[strlen(request->key)]='\0';
fseek(fp,position,SEEK_SET);
break;
}
else
{
position+=len;
} }
memcpy(request->value,value,256); //copied the complete value }
if(strlen(request->key)<256) //to pad with \0 //fclose(fp);
if (line)
free(line);
sem_wait(&readerLocks[index]);
readercount--;
if(readercount==0)
{ {
request->key[strlen(request->value)]='\0'; sem_post(&mutex[index]);
} }
sem_post(&readerLocks[index]);
}
void file_get(char *key,char *value)
{
return request; 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");
struct message *del(char m,char *key){ if (fp == NULL)
if(!m || key==NULL ) printf("\n[Invalid file]\n");
while ((read = getline(&line, &len, fp)) != -1)
{
if(strstr(line,key)!=NULL && strstr(strstr(line,key),":")!=NULL)
{ {
printf("Invalid parameters in del()"); snprintf(value,sizeof((strchr(line,':')+1)),"%s",(strchr(line,':')+1));
return NULL;
} }
struct message *request= malloc(sizeof(struct message)); }
request->status=m; fclose(fp);
memcpy(request->key,key,256); //copied the complete key if (line)
if(strlen(request->key)<256) free(line);
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;
} }
void file_put(char *key,char *value)
struct message *request(char status,char* key,char* value)
{ {
if(!status ||(key==NULL && value==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 request()"); if(key[i]!='\0')
return NULL; {
line[i]=key[i];
} }
struct message *requestMessage= malloc(sizeof(struct message)); else
requestMessage->status=status;
memcpy(requestMessage->key,key,256); //copied the complete key
if(strlen(requestMessage->key)<256)
{ {
requestMessage->key[strlen(requestMessage->key)]='\0'; break;
} }
if(value!=NULL) }
int k=0;
line[i++]=':';
for(;i<512;i++)
{
if(value[k]!='\0')
{ {
memcpy(requestMessage->value,value,256); //copied the complete value line[i]=value[k++];
} }
else
if(strlen(requestMessage->value)<256) //to pad with \0
{ {
requestMessage->value[strlen(requestMessage->value)]='\0'; break;
} }
printf("[Message Generated at Client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value); }
return requestMessage; line[i]='\n';
if(write(fds[index],line,strlen(line))<0)
{
printf("\n[Unable to Write to File]\n");
}
sem_post(&mutex[index]);
} }
void main()
int main()
{ {
char message[256]; /*
for(int i=0;i<30;i++) 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 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++)
{
snprintf(fileName,sizeof(fileName),"File%d.txt",i);
fds[i]=open(fileName, O_CREAT|O_RDWR|O_APPEND,S_IRWXU);
if(fds[i]<0)
{ {
message[i]='a'; printf("\n[Unable to Open File%d.txt]\n",i);
} }
message[sizeof(message)]='\0'; sem_init(&readerLocks[i],0,1);
request('2',message,message); sem_init(&mutex[i],0,1);
char c ='2'; readCounters[i]=0;
int status=(int)c; }
//file_put(key,value);
printf("%d",status); 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);
}
*/
//creating a message
} }
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