Skip to content

Commit

Permalink
Change busy wait to blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
uoo723 committed Jun 22, 2017
1 parent 34cf150 commit be7f46f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
4 changes: 4 additions & 0 deletions include/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#define VALUESIZE 100
#define KEYID 2817

#define TYPE_SERVER 1
#define TYPE_CLIENT 2

#define TYPE_REQ_PUT 1
#define TYPE_REQ_GET 2
#define TYPE_REQ_REMOVE 3
Expand All @@ -14,6 +17,7 @@

typedef struct msgbuf {
long mtype;
long type;
unsigned int key;
char value[VALUESIZE];
} msgbuf_t;
Expand Down
14 changes: 9 additions & 5 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,27 @@ void menu_remove() {

void menu_quit() {
printf("Bye\n");
msg.mtype = TYPE_QUIT;
msg.mtype = TYPE_SERVER;
msg.type = TYPE_QUIT;
snd_msg();
exit(0);
}

void req_put(unsigned int key, char *value) {
msg.mtype = TYPE_REQ_PUT;
msg.mtype = TYPE_SERVER;
msg.type = TYPE_REQ_PUT;
msg.key = key;
strcpy(msg.value, value);
snd_msg();
}

void req_get(unsigned int key) {
msg.mtype = TYPE_REQ_GET;
msg.mtype = TYPE_SERVER;
msg.type = TYPE_REQ_GET;
msg.key = key;
snd_msg();

if (msgrcv(key_id, &msg, MSGSIZE, TYPE_RES_GET, 0) < 0) {
if (msgrcv(key_id, &msg, MSGSIZE, TYPE_CLIENT, 0) < 0) {
perror("msgrcv error ");
return;
}
Expand All @@ -124,7 +127,8 @@ void req_get(unsigned int key) {
}

void req_remove(unsigned int key) {
msg.mtype = TYPE_REQ_REMOVE;
msg.mtype = TYPE_SERVER;
msg.type = TYPE_REQ_REMOVE;
msg.key = key;
snd_msg();
}
Expand Down
39 changes: 6 additions & 33 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,12 @@ void server() {
}

while (1) {
msg.mtype = 0;

if (msgrcv(key_id, &msg, MSGSIZE, TYPE_REQ_PUT, IPC_NOWAIT) < 0) {
if (errno != ENOMSG) {
perror("msgrcv error ");
return;
}
}

if (msgrcv(key_id, &msg, MSGSIZE, TYPE_REQ_GET, IPC_NOWAIT) < 0) {
if (errno != ENOMSG) {
perror("msgrcv error ");
return;
}
}

if (msgrcv(key_id, &msg, MSGSIZE, TYPE_REQ_REMOVE, IPC_NOWAIT) < 0) {
if (errno != ENOMSG) {
perror("msgrcv error ");
return;
}
if (msgrcv(key_id, &msg, MSGSIZE, TYPE_SERVER, 0) < 0) {
perror("msgrcv error ");
return;
}

if (msgrcv(key_id, &msg, MSGSIZE, TYPE_QUIT, IPC_NOWAIT) < 0) {
if (errno != ENOMSG) {
perror("msgrcv error ");
return;
}
}

switch (msg.mtype) {
case 0:
break;

switch (msg.type) {
case TYPE_REQ_PUT:
rcv_put(msg.key, msg.value);
break;
Expand Down Expand Up @@ -106,7 +78,8 @@ void rcv_get(unsigned int key) {
strcpy(msg.value, node->value);
}

msg.mtype = TYPE_RES_GET;
msg.mtype = TYPE_CLIENT;
msg.type = TYPE_RES_GET;

if (msgsnd(key_id, &msg, MSGSIZE, 0) < 0) {
perror("msgsnd error ");
Expand Down

0 comments on commit be7f46f

Please sign in to comment.