Skip to content

Commit

Permalink
Add message queue & Update
Browse files Browse the repository at this point in the history
  • Loading branch information
uoo723 committed Jun 23, 2017
1 parent e929248 commit d9194f1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
3 changes: 2 additions & 1 deletion include/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#define MSGTYPE_H

#define VALUESIZE 100
#define KEYID ftok(".", 'P')
#define SERVER_KEYID ftok(".", 'S')
#define CLIENT_KEYID ftok(",", 'C')

#define TYPE_SERVER 1
#define TYPE_CLIENT 2
Expand Down
14 changes: 10 additions & 4 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ static void menu_quit();

static void snd_msg();

static key_t key_id;
static key_t server_key_id;
static key_t client_key_id;

void client() {
if ((key_id = msgget(KEYID, IPC_CREAT|0666)) < 0) {
if ((server_key_id = msgget(SERVER_KEYID, IPC_CREAT|0666)) < 0) {
perror("msgget error in client ");
exit(0);
}

if ((client_key_id = msgget(CLIENT_KEYID, IPC_CREAT|0666)) < 0) {
perror("msgget error in client ");
exit(0);
}
Expand Down Expand Up @@ -50,7 +56,7 @@ void req_get(unsigned int key) {
msg.key = key;
snd_msg(&msg);

if (msgrcv(key_id, &msg, MSGSIZE, TYPE_CLIENT, 0) < 0) {
if (msgrcv(client_key_id, &msg, MSGSIZE, 0, 0) < 0) {
perror("msgrcv error in client ");
return;
}
Expand All @@ -75,7 +81,7 @@ void req_quit() {
}

static void snd_msg(msgbuf_t *msg) {
if (msgsnd(key_id, msg, MSGSIZE, 0) < 0) {
if (msgsnd(server_key_id, msg, MSGSIZE, 0) < 0) {
perror("msgsnd error in client ");
return;
}
Expand Down
20 changes: 15 additions & 5 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ typedef struct {
char value[VALUESIZE];
} args_t;

static key_t key_id;
static key_t server_key_id;
static key_t client_key_id;

static hashtable_t *hashtable;
static threadpool_t thpool;
Expand All @@ -37,17 +38,25 @@ static void rcv_get_internal(void *);
static void rcv_remove_internal(void *);

static void int_handler(int signo) {
msgctl(key_id, IPC_RMID, 0);
msgctl(server_key_id, IPC_RMID, 0);
msgctl(client_key_id, IPC_RMID, 0);
}

static int count;

void server() {
signal(SIGINT, int_handler);

if ((key_id = msgget(KEYID, IPC_CREAT|0666)) < 0) {
if ((server_key_id = msgget(SERVER_KEYID, IPC_CREAT|0666)) < 0) {
perror("msgget error in client ");
exit(0);
}

if ((client_key_id = msgget(CLIENT_KEYID, IPC_CREAT|0666)) < 0) {
perror("msgget error in client ");
exit(0);
}

if ((hashtable = ht_create(1000000)) == NULL) {
perror("hashtable is null ");
exit(0);
Expand All @@ -67,11 +76,12 @@ void server() {
msgbuf_t msg;

while (1) {
if (msgrcv(key_id, &msg, MSGSIZE, TYPE_SERVER, 0) < 0) {
if (msgrcv(server_key_id, &msg, MSGSIZE, 0, 0) < 0) {
perror("msgrcv error in server ");
return;
}

// printf("msgrcv %d\n", count++);
switch (msg.type) {
case TYPE_REQ_PUT:

Expand Down Expand Up @@ -175,7 +185,7 @@ void rcv_get(unsigned int key) {
msg.mtype = TYPE_CLIENT;
msg.type = TYPE_RES_GET;
msg.key = key;
if (msgsnd(key_id, &msg, MSGSIZE, 0) < 0) {
if (msgsnd(client_key_id, &msg, MSGSIZE, 0) < 0) {
perror("msgsnd error in server ");
return;
}
Expand Down
6 changes: 4 additions & 2 deletions test/client_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#define MAX_CONTENTS 100000
#define MAX_OPERATION 500000
#define MAX_OPERATION 1000000

#define THREAD 100

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -38,7 +40,7 @@ static time_t end_time;
void client_test() {
printf("client_test!!\n");

if ((thpool = thpool_init(4)) == NULL) {
if ((thpool = thpool_init(THREAD)) == NULL) {
perror("thpool is null ");
exit(0);
}
Expand Down

0 comments on commit d9194f1

Please sign in to comment.