From e1e97f1f98baf82efcd5825d7c7a7b4b1b2f2e40 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期一, 13 七月 2020 19:03:21 +0800 Subject: [PATCH] udpate --- queue/libshm_queue.a | 0 test2/test_queue_wrapper | 0 test2/client.c | 46 ++++++ test2/server | 0 test/test_timeout | 0 test2/test_queue_wrapper.c | 11 queue/include/shm_queue_wrapper.h | 39 +++-- test/multiple_queue_consumer | 0 test2/Makefile | 4 test/multiple_queue_productor | 0 Make.defines.linux | 2 queue/hashtable.c | 72 +++++++++ queue/sem_util.c | 10 + test/test_queue | 0 queue/include/mem_pool.h | 16 ++ queue/sem_util.h | 2 test2/client | 0 test/test_lostdata | 0 test/single_consumer | 0 test/single_productor | 0 queue/hashtable.h | 6 queue/shm_queue_wrapper.c | 117 +++++++++++----- test2/server.c | 43 ++++++ queue/mem_pool.c | 26 +++ 24 files changed, 327 insertions(+), 67 deletions(-) diff --git a/Make.defines.linux b/Make.defines.linux index 5be4d47..3b61627 100755 --- a/Make.defines.linux +++ b/Make.defines.linux @@ -2,7 +2,7 @@ # Definitions required in all program directories to compile and link # C programs using gcc. - +DEBUG=y CC = g++ COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) -c LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) diff --git a/queue/hashtable.c b/queue/hashtable.c index 9b66bb3..35530df 100755 --- a/queue/hashtable.c +++ b/queue/hashtable.c @@ -1,7 +1,7 @@ #include "usg_common.h" #include "hashtable.h" #include "mm.h" - +#include "sem_util.h" typedef struct tailq_entry_t { @@ -14,6 +14,9 @@ TAILQ_ENTRY(tailq_entry_t) joint; } tailq_entry_t; +static int hashtable_mutex; + +#define START_KEY 1000 typedef TAILQ_HEAD(tailq_header_t, tailq_entry_t) tailq_header_t; @@ -21,11 +24,16 @@ void hashtable_init(hashtable_t *hashtable ) { + memset(hashtable, 0, sizeof(hashtable_t)); + hashtable->mutex = SemUtil::get(IPC_PRIVATE, 1); + hashtable->wlock = SemUtil::get(IPC_PRIVATE, 1); + hashtable->cond = SemUtil::get(IPC_PRIVATE, 1); + hashtable->readcnt = 0; } -void *hashtable_get(hashtable_t *hashtable, int key) +void *_hashtable_get(hashtable_t *hashtable, int key) { size_t code = hashcode(key); tailq_entry_t *item; @@ -47,7 +55,7 @@ } -void* hashtable_put(hashtable_t *hashtable, int key, void *value) +void* _hashtable_put(hashtable_t *hashtable, int key, void *value) { size_t code = hashcode(key); void *oldvalue; @@ -158,3 +166,61 @@ return key % MAPSIZE; /*printf("hashfun = %ld\n", code);*/ } + + +int hashtable_alloc_key(hashtable_t *hashtable) { + int key = START_KEY; + SemUtil::dec(hashtable->wlock); + + while(_hashtable_get(hashtable, key) != NULL) { + key++; + } + + //_hashtable_put(hashtable, key, (void *)1); + SemUtil::inc(hashtable->wlock); + return key; +} + +void *hashtable_get(hashtable_t *hashtable, int key) { + SemUtil::dec(hashtable->mutex); + hashtable->readcnt++; + if (hashtable->readcnt == 1) { + //鑾峰彇璇诲啓閿� + SemUtil::dec(hashtable->wlock); + } + SemUtil::inc(hashtable->mutex); + + void * res = _hashtable_get(hashtable, key); + + SemUtil::dec(hashtable->mutex); + hashtable->readcnt--; + if(hashtable->readcnt == 0) { + //閲婃斁璇诲啓閿� + SemUtil::inc(hashtable->wlock); + //閫氱煡鍐� + SemUtil::set(hashtable->cond, 1); + } + SemUtil::inc(hashtable->mutex); + return res; +} + +void* hashtable_put(hashtable_t *hashtable, int key, void *value) { + SemUtil::dec(hashtable->mutex); + while (hashtable->readcnt > 0) + { + SemUtil::set(hashtable->cond, 0); + SemUtil::inc(hashtable->mutex); + //绛夊緟鍐欓�氱煡 + SemUtil::dec(hashtable->cond); + + SemUtil::dec(hashtable->mutex); + + } + SemUtil::inc(hashtable->mutex); + + //鑾峰彇璇诲啓閿� + SemUtil::dec(hashtable->wlock); + _hashtable_put(hashtable, key, value); + //閲婃斁璇诲啓閿� + SemUtil::inc(hashtable->wlock); +} diff --git a/queue/hashtable.h b/queue/hashtable.h index 73c1729..bbe2aa6 100755 --- a/queue/hashtable.h +++ b/queue/hashtable.h @@ -9,6 +9,10 @@ typedef struct hashtable_t { struct tailq_header_t* array[MAPSIZE]; + int mutex; + int wlock; + int cond; + size_t readcnt; } hashtable_t; @@ -20,4 +24,6 @@ void hashtable_printall(hashtable_t *hashtable); + +int hashtable_alloc_key(hashtable_t *hashtable); #endif diff --git a/queue/include/mem_pool.h b/queue/include/mem_pool.h new file mode 100644 index 0000000..045e972 --- /dev/null +++ b/queue/include/mem_pool.h @@ -0,0 +1,16 @@ +#ifndef _MEM_POOL_H_ +#define _MEM_POOL_H_ +#include "mm.h" + + +extern void mem_pool_init(size_t heap_size); +extern void mem_pool_destroy(void); + +extern void *mem_pool_malloc (size_t size); +extern void mem_pool_free (void *ptr); +extern void *mem_pool_realloc(void *ptr, size_t size); +extern hashtable_t * mem_pool_get_hashtable(); +// extern int mm_checkheap(int verbose); + + +#endif \ No newline at end of file diff --git a/queue/include/shm_queue_wrapper.h b/queue/include/shm_queue_wrapper.h index 4ab857c..fadc6ae 100644 --- a/queue/include/shm_queue_wrapper.h +++ b/queue/include/shm_queue_wrapper.h @@ -11,62 +11,67 @@ #endif /** - * 鍒濆鍖� + * 鍒涘缓闃熷垪 * @ shmqueue - * @ key 鏍囪瘑鍏变韩闃熷垪鐨勫敮涓�key - * @ queue_size 闃熷垪澶у皬 , 杩欎釜鍊煎繀椤绘槸2鐨勬寚鏁板嵆 1, 2, 4, 8, 16 绛� - * @ ele_size 闃熷垪涓厓绱犲ぇ灏�, 杩欎釜鍊间笉鑳借秴杩�512锛屽綋鐒跺鏋滈渶瑕佸彲浠ヨ皟鏁磋繖涓渶澶ч檺鍒� + * @ key 鏍囪瘑鍏变韩闃熷垪鐨勫敮涓�key, 濡傛灉key涓虹┖浼氳嚜鍔ㄥ垎閰嶄竴涓猭ey锛屽鏋渒ey涓嶄細绌轰細妫�鏌ユ槸鍚︽湁閲嶅缁戝畾鐨勬儏鍐�, 鐒跺悗缁戝畾key. + * @ queue_size 闃熷垪澶у皬 + * @ ele_size 闃熷垪涓厓绱犲ぇ灏� */ -void* shmqueue_init(int key, int queue_size, int ele_size); +void* shmqueue_create( int * key, int queue_size); + +/** + * 缁戝畾key鍒伴槦鍒楋紝浣嗘槸骞朵笉浼氬垱寤洪槦鍒椼�傚鏋滄病鏈夊搴旀寚瀹歬ey鐨勯槦鍒楁彁绀洪敊璇苟杩斿洖绌哄�� + */ +void* shmqueue_attach(int key); /** * 閿�姣� */ -void shmqueue_destroy(void *shmqueue); +void shmqueue_drop(void * _shmqueue) ; + /** * 闃熷垪鍏冪礌鐨勪釜鏁� */ -uint32_t shmqueue_size(void *shmqueue); +int shmqueue_size(void * _shmqueue); /** * 鏄惁宸叉弧 */ -int shmqueue_full(void *shmqueue); +int shmqueue_full(void * _shmqueue); /** * 鏄惁涓虹┖ */ -int shmqueue_empty(void *shmqueue); +int shmqueue_empty(void * _shmqueue) ; /** * 鍏ラ槦, 闃熷垪婊℃椂绛夊緟 */ -int shmqueue_push(void *shmqueue, void *src_ele); +int shmqueue_push(void * _shmqueue, void *src_ele, int ele_size); /** * 鍏ラ槦, 闃熷垪婊℃椂绔嬪嵆杩斿洖 */ -int shmqueue_push_nowait(void *shmqueue, void *src_ele); +int shmqueue_push_nowait(void * _shmqueue, void *src_ele, int ele_size) ; /** * 鍏ラ槦, 鎸囧畾鏃堕棿鍐呭叆闃熶笉鎴愬姛灏辫繑鍥� + * timespec {sec绉�, nsec绾崇} */ -int shmqueue_push_timeout(void *shmqueue, void *src_ele, struct timespec * timeout); +int shmqueue_push_timeout(void * _shmqueue, void *src_ele, int ele_size, void * _timeout); /** * 鍑洪槦, 闃熷垪绌烘椂绛夊緟 */ -int shmqueue_pop(void *shmqueue, void *dest_ele); +int shmqueue_pop(void * _shmqueue, void *dest_ele); /** * 鍑洪槦, 闃熷垪绌烘椂绔嬪嵆杩斿洖 */ -int shmqueue_pop_nowait(void *shmqueue, void *dest_ele); +int shmqueue_pop_nowait(void * _shmqueue, void *dest_ele) ; /** * 鍑洪槦, 鎸囧畾鏃堕棿鍐呭嚭闃熶笉鎴愬姛灏辫繑鍥� */ -int shmqueue_pop_timeout(void *shmqueue, void *dest_ele, struct timespec * timeout); - - +int shmqueue_pop_timeout(void * _shmqueue, void *dest_ele, void * _timeout); #ifdef __cplusplus } diff --git a/queue/libshm_queue.a b/queue/libshm_queue.a index 8ddc940..0cad336 100644 --- a/queue/libshm_queue.a +++ b/queue/libshm_queue.a Binary files differ diff --git a/queue/mem_pool.c b/queue/mem_pool.c new file mode 100644 index 0000000..e35763c --- /dev/null +++ b/queue/mem_pool.c @@ -0,0 +1,26 @@ +#include "mem_pool.h" + +void mem_pool_init(size_t heap_size) { + mm_init(heap_size); +} + +void mem_pool_destroy(void) { + mm_destroy(); +} + +void *mem_pool_malloc (size_t size) { + return mm_malloc(size); +} + +void mem_pool_free (void *ptr) { + mm_free(ptr); +} + +void *mem_pool_realloc (void *ptr, size_t size) { + return mm_realloc(ptr, size); +} + +hashtable_t * mem_pool_get_hashtable() { + return mem_get_hashtable(); + +} \ No newline at end of file diff --git a/queue/sem_util.c b/queue/sem_util.c index d7819a2..ea23f7c 100644 --- a/queue/sem_util.c +++ b/queue/sem_util.c @@ -136,3 +136,13 @@ } + +void SemUtil::set(int semId, int val) +{ + union semun arg; + arg.val = val; + if (semctl(semId, 0, SETVAL, arg) == -1) + err_exit(errno, "SemUtil::set semctl"); +} + + diff --git a/queue/sem_util.h b/queue/sem_util.h index c4af638..0d673de 100644 --- a/queue/sem_util.h +++ b/queue/sem_util.h @@ -13,6 +13,8 @@ int inc(int semId); void remove(int semid); + void set(int semId, int val); + } #endif diff --git a/queue/shm_queue_wrapper.c b/queue/shm_queue_wrapper.c index daa3f1e..03cc97f 100644 --- a/queue/shm_queue_wrapper.c +++ b/queue/shm_queue_wrapper.c @@ -1,48 +1,75 @@ #include "shm_queue_wrapper.h" -#define MAX_ELE_SIZE 512 + +#include "mm.h" +#include "hashtable.h" typedef struct ele_t { - char buf[MAX_ELE_SIZE]; + size_t size; + void * buf; } ele_t; typedef struct shmqueue_t { - size_t ele_size; void *mqueue; } shmqueue_t; /** - * 鍒濆鍖� + * 鍒涘缓闃熷垪 * @ shmqueue - * @ key 鏍囪瘑鍏变韩闃熷垪鐨勫敮涓�key + * @ key 鏍囪瘑鍏变韩闃熷垪鐨勫敮涓�鏍囪瘑, key鏄竴涓寚閽堥噷闈㈠瓨鍌ㄤ簡key鐨勫�硷紝 濡傛灉key鐨勫�间负-1绯荤粺浼氳嚜鍔ㄥ垎閰嶄竴涓猭ey鍊煎苟鎶婅key鐨勫�艰祴缁檏ey鎸囬拡銆傚鏋渒ey鐨勫�间笉浼氱┖浼氭鏌ユ槸鍚︽湁閲嶅缁戝畾鐨勬儏鍐�, 鏈夐噸澶嶅氨鎶ラ敊娌℃湁灏卞垱寤洪槦鍒楀苟缁戝畾key. * @ queue_size 闃熷垪澶у皬 - * @ ele_size 闃熷垪涓厓绱犲ぇ灏� + * @ size 闃熷垪涓厓绱犲ぇ灏� */ -void* shmqueue_init( int key, int queue_size, int ele_size) { - if(ele_size > MAX_ELE_SIZE) { - err_exit(0, "shmqueue_init : the element size is supper than max element buffer size"); +void* shmqueue_create( int * key, int queue_size) { + int mkey; + hashtable_t *hashtable = get_mm_hashtable(); + if(*key == -1) { + mkey = hashtable_alloc_key(hashtable); + *key = mkey; + } else { + mkey = *key; + if(hashtable_get(hashtable, mkey)!= NULL) { + err_msg(0, "key %d has already been in used!", mkey); + return NULL; + } } + shmqueue_t *shmqueue = (shmqueue_t*)malloc(sizeof(shmqueue_t)); - SHMQueue<ele_t> *queue = new SHMQueue<ele_t>(key, queue_size); + SHMQueue<ele_t> *queue = new SHMQueue<ele_t>(mkey, queue_size); shmqueue->mqueue = (void *)queue; - shmqueue->ele_size = ele_size; + return (void *)shmqueue; +} + +/** + * 缁戝畾key鍒伴槦鍒楋紝浣嗘槸骞朵笉浼氬垱寤洪槦鍒椼�傚鏋滄病鏈夊搴旀寚瀹歬ey鐨勯槦鍒楁彁绀洪敊璇苟杩斿洖绌哄�� + */ +void* shmqueue_attach(int key) { + hashtable_t *hashtable = get_mm_hashtable(); + if(hashtable_get(hashtable, key)== NULL) { + err_msg(0, "shmqueue_attach锛歛ttach failed, The queue binding on key %d has not been created!", key); + return NULL; + } + + shmqueue_t *shmqueue = (shmqueue_t*)malloc(sizeof(shmqueue_t)); + SHMQueue<ele_t> *queue = new SHMQueue<ele_t>(key, 0); + shmqueue->mqueue = (void *)queue; return (void *)shmqueue; } /** * 閿�姣� */ -void shmqueue_destroy(void * _shmqueue) { +void shmqueue_drop(void * _shmqueue) { shmqueue_t * shmqueue = (shmqueue_t *)_shmqueue; delete (SHMQueue<ele_t> *)shmqueue->mqueue; - delete _shmqueue; + delete shmqueue; } /** * 闃熷垪鍏冪礌鐨勪釜鏁� */ -uint32_t shmqueue_size(void * _shmqueue) { +int shmqueue_size(void * _shmqueue) { shmqueue_t * shmqueue = (shmqueue_t *)_shmqueue; - return ((SHMQueue<ele_t> *)(shmqueue->mqueue))->size(); + return (int)(((SHMQueue<ele_t> *)(shmqueue->mqueue))->size()) ; } /** @@ -64,42 +91,51 @@ /** * 鍏ラ槦, 闃熷垪婊℃椂绛夊緟 */ -int shmqueue_push(void * _shmqueue, void *src_ele) { +int shmqueue_push(void * _shmqueue, void *src, int size) { shmqueue_t * shmqueue = (shmqueue_t *)_shmqueue; - ele_t dest_ele; - memcpy(&dest_ele.buf, src_ele, shmqueue->ele_size); - return ((SHMQueue<ele_t> *)(shmqueue->mqueue))->push(dest_ele); + ele_t dest; + dest.size = size; + dest.buf = mm_malloc(size); + memcpy(dest.buf, src, size); + return ((SHMQueue<ele_t> *)(shmqueue->mqueue))->push(dest); } /** * 鍏ラ槦, 闃熷垪婊℃椂绔嬪嵆杩斿洖 */ -int shmqueue_push_nowait(void * _shmqueue, void *src_ele) { +int shmqueue_push_nowait(void * _shmqueue, void *src, int size) { shmqueue_t * shmqueue = (shmqueue_t *)_shmqueue; - ele_t dest_ele; - memcpy(&dest_ele.buf, src_ele, shmqueue->ele_size); - return ((SHMQueue<ele_t> *)(shmqueue->mqueue))->push_nowait(dest_ele); + ele_t dest; + dest.size = size; + dest.buf = mm_malloc(size); + memcpy(dest.buf, src, size); + return ((SHMQueue<ele_t> *)(shmqueue->mqueue))->push_nowait(dest); } /** * 鍏ラ槦, 鎸囧畾鏃堕棿鍐呭叆闃熶笉鎴愬姛灏辫繑鍥� + * timespec {sec绉�, nsec绾崇} */ -int shmqueue_push_timeout(void * _shmqueue, void *src_ele, struct timespec * timeout) { +int shmqueue_push_timeout(void * _shmqueue, void *src, int size, void * _timeout) { + struct timespec *timeout = (struct timespec *)_timeout; shmqueue_t * shmqueue = (shmqueue_t *)_shmqueue; - ele_t dest_ele; - memcpy(&dest_ele.buf, src_ele, shmqueue->ele_size); - return ((SHMQueue<ele_t> *)(shmqueue->mqueue))->push_timeout(dest_ele, timeout); + ele_t dest; + dest.size = size; + dest.buf = mm_malloc(size); + memcpy(dest.buf, src, size); + return ((SHMQueue<ele_t> *)(shmqueue->mqueue))->push_timeout(dest, timeout); } /** * 鍑洪槦, 闃熷垪绌烘椂绛夊緟 */ -int shmqueue_pop(void * _shmqueue, void *dest_ele) { +int shmqueue_pop(void * _shmqueue, void *dest) { shmqueue_t * shmqueue = (shmqueue_t *)_shmqueue; - ele_t src_ele; - bool rv = ((SHMQueue<ele_t> *)(shmqueue->mqueue))->pop(src_ele); + ele_t src; + bool rv = ((SHMQueue<ele_t> *)(shmqueue->mqueue))->pop(src); if (rv) { - memcpy(dest_ele, &src_ele.buf, shmqueue->ele_size); + memcpy(dest, src.buf, src.size); + mm_free(src.buf); return 1; } else { return 0; @@ -110,12 +146,13 @@ /** * 鍑洪槦, 闃熷垪绌烘椂绔嬪嵆杩斿洖 */ -int shmqueue_pop_nowait(void * _shmqueue, void *dest_ele) { +int shmqueue_pop_nowait(void * _shmqueue, void *dest) { shmqueue_t * shmqueue = (shmqueue_t *)_shmqueue; - ele_t src_ele; - bool rv = ((SHMQueue<ele_t> *)(shmqueue->mqueue))->pop_nowait(src_ele); + ele_t src; + bool rv = ((SHMQueue<ele_t> *)(shmqueue->mqueue))->pop_nowait(src); if (rv) { - memcpy(dest_ele, &src_ele.buf, shmqueue->ele_size); + memcpy(dest, src.buf, src.size); + mm_free(src.buf); return 1; } else { return 0; @@ -125,12 +162,14 @@ /** * 鍑洪槦, 鎸囧畾鏃堕棿鍐呭嚭闃熶笉鎴愬姛灏辫繑鍥� */ -int shmqueue_pop_timeout(void * _shmqueue, void *dest_ele, struct timespec * timeout) { +int shmqueue_pop_timeout(void * _shmqueue, void *dest, void * _timeout) { + struct timespec *timeout = (struct timespec *)_timeout; shmqueue_t * shmqueue = (shmqueue_t *)_shmqueue; - ele_t src_ele; - bool rv = ((SHMQueue<ele_t> *)(shmqueue->mqueue))->pop_timeout(src_ele, timeout); + ele_t src; + bool rv = ((SHMQueue<ele_t> *)(shmqueue->mqueue))->pop_timeout(src, timeout); if (rv) { - memcpy(dest_ele, &src_ele.buf, shmqueue->ele_size); + memcpy(dest, src.buf, src.size); + mm_free(src.buf); return 1; } else { return 0; diff --git a/test/multiple_queue_consumer b/test/multiple_queue_consumer index bf19ef4..dd32783 100755 --- a/test/multiple_queue_consumer +++ b/test/multiple_queue_consumer Binary files differ diff --git a/test/multiple_queue_productor b/test/multiple_queue_productor index d7d0557..5cc5de8 100755 --- a/test/multiple_queue_productor +++ b/test/multiple_queue_productor Binary files differ diff --git a/test/single_consumer b/test/single_consumer index 257d96a..15e24d6 100755 --- a/test/single_consumer +++ b/test/single_consumer Binary files differ diff --git a/test/single_productor b/test/single_productor index f77c733..2245ec6 100755 --- a/test/single_productor +++ b/test/single_productor Binary files differ diff --git a/test/test_lostdata b/test/test_lostdata index 5aef5f7..a88dd0f 100755 --- a/test/test_lostdata +++ b/test/test_lostdata Binary files differ diff --git a/test/test_queue b/test/test_queue index 306966d..a9d71a0 100755 --- a/test/test_queue +++ b/test/test_queue Binary files differ diff --git a/test/test_timeout b/test/test_timeout index aeb9ff3..bcfb6e9 100755 --- a/test/test_timeout +++ b/test/test_timeout Binary files differ diff --git a/test2/Makefile b/test2/Makefile index 072837c..9743df5 100644 --- a/test2/Makefile +++ b/test2/Makefile @@ -13,8 +13,8 @@ PLATFORM=$(shell $(ROOT)/systype.sh) include $(ROOT)/Make.defines.$(PLATFORM) - -PROGS = test_queue_wrapper + +PROGS = test_queue_wrapper server client build: $(PROGS) diff --git a/test2/client b/test2/client new file mode 100755 index 0000000..a68ea7e --- /dev/null +++ b/test2/client Binary files differ diff --git a/test2/client.c b/test2/client.c new file mode 100644 index 0000000..09b0ceb --- /dev/null +++ b/test2/client.c @@ -0,0 +1,46 @@ +#include "shm_queue_wrapper.h" +#include "mm.h" + +typedef struct msg_t +{ + int key; + char buf[100]; + +} msg_t; + +void client() { + int key = -1; + size_t qsize = 16; + void * remote_queue = shmqueue_attach( 1); + void * local_queue = shmqueue_create( &key, qsize); + // message_t item; + msg_t msg; + msg.key = key; + + msg_t rec_msg; + //鍏ラ槦 + while(true) { + printf("=====>say some thing:\n"); + scanf("%s", msg.buf); + shmqueue_push(remote_queue, (void *)&msg, sizeof(msg)); + //printf("send: %s\n", msg.buf); + shmqueue_pop(local_queue, (void *)&rec_msg ); + printf("=====>peer : %s\n", rec_msg.buf); + + + } + + //閿�姣侀槦鍒� + shmqueue_drop(local_queue); + shmqueue_drop(remote_queue); +} + + +int main () { + mm_init(512); + client(); + + //鏁翠釜杩涚▼閫�鍑烘椂闇�瑕佹墽琛岃繖涓柟娉曪紝璇ユ柟娉曢鍏堜細妫�鏌ユ槸鍚﹁繕鏈夊叾浠栬繘绋嬪湪浣跨敤璇ュ叡浜唴瀛橈紝濡傛灉杩樻湁鍏朵粬杩涚▼鍦ㄤ娇鐢ㄥ氨鍙槸detach,濡傛灉娌℃湁鍏朵粬杩涚▼鍦ㄤ娇鐢ㄥ垯閿�姣佹暣鍧楀唴瀛樸�� + mm_destroy(); + return 0; +} \ No newline at end of file diff --git a/test2/server b/test2/server new file mode 100755 index 0000000..259ee6c --- /dev/null +++ b/test2/server Binary files differ diff --git a/test2/server.c b/test2/server.c new file mode 100644 index 0000000..0f8b446 --- /dev/null +++ b/test2/server.c @@ -0,0 +1,43 @@ +#include "shm_queue_wrapper.h" +#include "mm.h" + +typedef struct msg_t +{ + int key; + char buf[100]; + +} msg_t; + +void server() { + msg_t msg; + msg_t send_msg; + int key = 1; + size_t qsize = 16; + void * local_queue = shmqueue_create( &key, qsize); + + struct timespec timeout = {1, 0}; + + while(shmqueue_pop(local_queue, (void *)&msg) ) { + void * remote_queue = shmqueue_attach(msg.key); + printf("received: %s\n", msg.buf); + // send_msg.key = 1; + sprintf(send_msg.buf, "hello, I have received: %s!", msg.buf); + shmqueue_push(remote_queue, (void *)&send_msg, sizeof(send_msg)); + shmqueue_drop(remote_queue); + // cout << item.pic << endl; + // i++; + } + + //閿�姣侀槦鍒� + shmqueue_drop(local_queue); +} + + +int main () { + mm_init(512); + server(); + + //鏁翠釜杩涚▼閫�鍑烘椂闇�瑕佹墽琛岃繖涓柟娉曪紝璇ユ柟娉曢鍏堜細妫�鏌ユ槸鍚﹁繕鏈夊叾浠栬繘绋嬪湪浣跨敤璇ュ叡浜唴瀛橈紝濡傛灉杩樻湁鍏朵粬杩涚▼鍦ㄤ娇鐢ㄥ氨鍙槸detach,濡傛灉娌℃湁鍏朵粬杩涚▼鍦ㄤ娇鐢ㄥ垯閿�姣佹暣鍧楀唴瀛樸�� + mm_destroy(); + return 0; +} \ No newline at end of file diff --git a/test2/test_queue_wrapper b/test2/test_queue_wrapper index 1b63779..afce509 100755 --- a/test2/test_queue_wrapper +++ b/test2/test_queue_wrapper Binary files differ diff --git a/test2/test_queue_wrapper.c b/test2/test_queue_wrapper.c index efcc881..7ae3e9c 100644 --- a/test2/test_queue_wrapper.c +++ b/test2/test_queue_wrapper.c @@ -10,21 +10,21 @@ void test1() { unsigned int i = 0; - int key = 2; + int key = -1; size_t qsize = 16; - void * queue = shmqueue_init( key, qsize, sizeof(message_t)); + void * queue = shmqueue_create( &key, qsize); message_t item; for(i = 0; i < qsize; i++) { sprintf(item.method, "hello"); item.code = i ; //鍏ラ槦 - if(shmqueue_push(queue, (void *)&item)) { + if(shmqueue_push(queue, (void *)&item, sizeof(message_t))) { printf("push:%d %s\n", item.code, item.method ); } } - + printf("%d\n", key); struct timespec timeout = {1, 0}; i = 0; @@ -36,11 +36,12 @@ } //閿�姣侀槦鍒� - shmqueue_destroy(queue); + shmqueue_drop(queue); } int main () { + mm_init(512); test1(); //鏁翠釜杩涚▼閫�鍑烘椂闇�瑕佹墽琛岃繖涓柟娉曪紝璇ユ柟娉曢鍏堜細妫�鏌ユ槸鍚﹁繕鏈夊叾浠栬繘绋嬪湪浣跨敤璇ュ叡浜唴瀛橈紝濡傛灉杩樻湁鍏朵粬杩涚▼鍦ㄤ娇鐢ㄥ氨鍙槸detach,濡傛灉娌℃湁鍏朵粬杩涚▼鍦ㄤ娇鐢ㄥ垯閿�姣佹暣鍧楀唴瀛樸�� -- Gitblit v1.8.0