From 9e6ceaad059b2aec84df92c8750f6d87eab708c2 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期四, 16 七月 2020 20:46:31 +0800 Subject: [PATCH] udpate --- test/communication.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 95 insertions(+), 5 deletions(-) diff --git a/test/communication.c b/test/communication.c index a314dcb..16a9b05 100644 --- a/test/communication.c +++ b/test/communication.c @@ -1,5 +1,8 @@ #include "socket.h" - +typedef struct Targ { + int port; + int id; +}Targ; void * precess_client(void *_socket) { pthread_detach(pthread_self()); @@ -7,13 +10,11 @@ int size; void *recvbuf; char sendbuf[512]; - while(true) { - shm_recv(socket, &recvbuf, &size); + while (shm_recv(socket, &recvbuf, &size) == 0 ) { sprintf(sendbuf, "SERVER RECEIVED: %s", recvbuf); puts(sendbuf); - shm_send(socket, sendbuf, strlen(sendbuf)+1) ; + shm_send(socket, sendbuf, strlen(sendbuf)+1); shm_free(recvbuf); - } shm_close_socket(socket); } @@ -51,6 +52,92 @@ shm_close_socket(socket); } +void client_send(shm_socket_t *socket, char *sendbuf) { + + int size; + void *recvbuf; + shm_send(socket, sendbuf, strlen(sendbuf)+1) ; + shm_recv(socket, &recvbuf, &size); + printf("reply: %s\n", (char *)recvbuf); + shm_free(recvbuf); + + +} + + +void multyProcessorsClient(int port) { + + int status, i = 0, processors = 4, scale = 100000; + pid_t productors[processors]; + pid_t pid; + char sendbuf[512]; + for ( i = 0; i < processors; i++) { + if ((productors[i] = fork()) == 0) /* Child runs user job */ + { + shm_socket_t *socket = shm_open_socket(); + shm_connect(socket, port); + while( scale-- > 0) { + sprintf(sendbuf, "processor(%d) %d", i, scale); + client_send(socket, sendbuf); + } + shm_close_socket(socket); + exit(0); + } + } + + while ((pid = waitpid(-1, &status, 0)) > 0) { + if(WIFEXITED(status)) { + //fprintf(stderr, "child %d terminated normally with exit status=%d\n", pid, WEXITSTATUS(status)); + }else + fprintf(stderr, "child %d terminated abnormally\n", pid); + } + + if (errno != ECHILD) + perror("waitpid error"); +} + +void *threadrun(void *arg) { + Targ * targ = ( Targ * )arg; + int port = targ->port; + char sendbuf[512]; + int scale = 100000; + int i; + shm_socket_t *socket = shm_open_socket(); + + shm_connect(socket, port); + for( i = 0; i<scale; i++) { + sprintf(sendbuf, "processor(%d) %d", targ->id, i); + client_send(socket, sendbuf); + } + shm_close_socket(socket); + return (void*)i; +} + +void multyThreadClient(int port) { + + int status, i = 0, processors = 2; + void *res[processors]; + Targ *targs= (Targ*)calloc(processors, sizeof(Targ)); + pthread_t tids[processors]; + char sendbuf[512]; + for ( i = 0; i < processors; i++) { + targs[i].port = port; + targs[i].id = i; + pthread_create(&tids[i], NULL, threadrun, (void *)&targs[i]); + + } + + for (i = 0; i< processors; i++) { + if(pthread_join(tids[i], &res[i])!=0) { + perror("multyThreadClient pthread_join"); + } else { + fprintf(stderr, "client(%d) 鍐欏叆 %ld 鏉℃暟鎹甛n", i, (long)res[i]); + } + } + + +} + int main(int argc, char *argv[]) { shm_init(512); int port; @@ -67,6 +154,9 @@ if (strcmp("client", argv[1]) == 0) client(port); + + if (strcmp("mclient", argv[1]) == 0) + multyThreadClient(port); shm_destroy(); // fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", "server", "client"); return 0; -- Gitblit v1.8.0