wangzhengquan
2020-12-07 d7d05fb334db91e3ecbf009ca2650d88f9ed7b46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "shm_socket.h"
#include "usg_common.h"
#include "shm_mm_wraper.h"
typedef struct Targ {
    int port;
    int id;
}Targ;
 
void * precess_client(void *_socket) {
    pthread_detach(pthread_self());
    shm_socket_t *socket = (shm_socket_t *)_socket;
    int size;
    void *recvbuf;
    char sendbuf[512];
    while (shm_recv(socket, &recvbuf, &size) == 0 ) {
        sprintf(sendbuf, "SERVER RECEIVED: %s", recvbuf);
        puts(sendbuf);
        shm_send(socket, sendbuf, strlen(sendbuf)+1);
        free(recvbuf);
    }
    shm_close_socket(socket);
}
 
void server(int port) {
    pthread_t tid;
    shm_socket_t *socket = shm_open_socket();
    shm_socket_bind(socket, port);
    shm_listen(socket);
    shm_socket_t *client_socket;
    while(true) {
        client_socket = shm_accept(socket);
// printf("server messageQueue = %p\n", client_socket->messageQueue);
        pthread_create(&tid, NULL, precess_client , (void *)client_socket);
    }
 
    
}
 
void client(int port) {
    shm_socket_t *socket = shm_open_socket();
    shm_connect(socket, port);
    int size;
    void *recvbuf;
    char sendbuf[512];
    while(true) {
        printf("request: ");
        scanf("%s", sendbuf);
        shm_send(socket, sendbuf, strlen(sendbuf)+1) ;
        shm_recv(socket, &recvbuf, &size);
        printf("reply: %s\n", (char *)recvbuf);
        free(recvbuf);
 
    }
    shm_close_socket(socket);
}
 
void client_send(shm_socket_t *socket, char *sendbuf) {
    
    int size;
    void *recvbuf;
    printf("requst:%s\n", sendbuf);
    shm_send(socket, sendbuf, strlen(sendbuf)+1) ;
    shm_recv(socket, &recvbuf, &size);
    printf("reply: %s\n", (char *)recvbuf);
    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, "thread(%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 = 4;
    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_mm_wrapper_init(512);
  int port;
  if (argc < 3) {
       fprintf(stderr, "Usage: reqrep %s|%s <PORT> ...\n", "server", "client");
       return 1;
  }
 
  port = atoi(argv[2]);
 
  if (strcmp("server", argv[1]) == 0 ) {
     server(port);
  }
 
  if (strcmp("client", argv[1]) == 0)
     client(port);
 
 if (strcmp("mclient", argv[1]) == 0)
     multyThreadClient(port);
 
  shm_mm_wrapper_destroy();
 // fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", "server", "client");
  return 0;
}