Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
key-value-store
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Samarth Joshi
key-value-store
Commits
f01d4875
Commit
f01d4875
authored
Nov 08, 2020
by
Roshan Rabinarayan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code for file
parent
ddb7f72c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
182 additions
and
79 deletions
+182
-79
temp.c
temp.c
+182
-79
No files found.
temp.c
View file @
f01d4875
#include<semaphore.h>
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include "KVMessageFormat.h"
struct
message
*
get
(
char
m
,
char
*
key
)
{
if
(
!
m
||
key
==
NULL
)
{
printf
(
"Invalid parameters in get()"
);
return
NULL
;
}
struct
message
*
request
=
malloc
(
sizeof
(
struct
message
));
request
->
status
=
m
;
memcpy
(
request
->
key
,
key
,
256
);
//copied the complete key
if
(
strlen
(
request
->
key
)
<
256
)
{
request
->
key
[
strlen
(
request
->
key
)]
=
'\0'
;
#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
;
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
request
;
return
rem
;
}
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
));
request
->
status
=
m
;
memcpy
(
request
->
key
,
key
,
256
);
//copied the complete key
if
(
strlen
(
request
->
key
)
<
256
)
if
(
strstr
(
line
,
key
)
!=
NULL
&&
strstr
(
strstr
(
line
,
key
),
":"
)
!=
NULL
)
{
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
;
}
struct
message
*
del
(
char
m
,
char
*
key
){
if
(
!
m
||
key
==
NULL
)
fp
=
fdopen
(
fds
[
index
],
"r"
);
if
(
fp
==
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()"
);
return
NULL
;
snprintf
(
value
,
sizeof
((
strchr
(
line
,
':'
)
+
1
)),
"%s"
,(
strchr
(
line
,
':'
)
+
1
));
}
struct
message
*
request
=
malloc
(
sizeof
(
struct
message
));
request
->
status
=
m
;
memcpy
(
request
->
key
,
key
,
256
);
//copied the complete key
if
(
strlen
(
request
->
key
)
<
256
)
}
fclose
(
fp
);
if
(
line
)
free
(
line
);
sem_wait
(
&
readerLocks
[
index
]);
readercount
--
;
if
(
readercount
==
0
)
{
request
->
key
[
strlen
(
request
->
key
)]
=
'\0'
;
sem_post
(
&
mutex
[
index
])
;
}
request
->
value
[
0
]
=
'\0'
;
return
request
;
sem_post
(
&
readerLocks
[
index
]);
}
struct
message
*
request
(
char
status
,
char
*
key
,
char
*
value
)
void
file_put
(
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()"
);
return
NULL
;
if
(
key
[
i
]
!=
'\0'
)
{
line
[
i
]
=
key
[
i
];
}
struct
message
*
requestMessage
=
malloc
(
sizeof
(
struct
message
));
requestMessage
->
status
=
status
;
memcpy
(
requestMessage
->
key
,
key
,
256
);
//copied the complete key
if
(
strlen
(
requestMessage
->
key
)
<
256
)
else
{
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
++
];
}
if
(
strlen
(
requestMessage
->
value
)
<
256
)
//to pad with \0
else
{
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'
;
request
(
'2'
,
message
,
message
);
char
c
=
'2'
;
int
status
=
(
int
)
c
;
printf
(
"%d"
,
status
);
sem_init
(
&
readerLocks
[
i
],
0
,
1
);
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);
}
*/
//creating a message
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment