# include # include # include # include # include # include # include # include #include int fib(int n) { if ( n > 1 ) return fib(n-1) + fib(n-2); else return 1; } # define REQ "GET / HTTP/1.1\r\n\r\n" int get() { int s; int n; struct sockaddr_in sa; struct in_addr ia; char buf[10000]; sa.sin_family = AF_INET; if ( !inet_pton(AF_INET, "127.0.0.1", &ia) ) { fprintf(stderr, "inet_pton\n"); exit(1); } else { sa.sin_addr.s_addr = ia.s_addr; } sa.sin_port = htons(80); s = socket(AF_INET, SOCK_STREAM, 0); n = connect(s, (struct sockaddr*)&sa, sizeof(sa)); send(s, REQ, strlen(REQ), 0); while ( (n = recv(s, buf, 10000, 0)) > 0 ); //printf("%d\n", fib(38)); close (s); return 0; } void* nget(void* p){ int i; int n = *(int*)p; for ( i = 0; i < n; i++ ) get(); return NULL; } int main(int argc, char* argv[]) { int c; int n; int p; int i; double run_time; struct timeval start; struct timeval end; c = strtol(argv[1], NULL, 10); n = strtol(argv[2], NULL, 10); p = n/c; pthread_t *thread_ids; thread_ids = (pthread_t*)malloc(sizeof(pthread_t*) * c); gettimeofday(&start, NULL); for (i = 0; i < c; i++) { pthread_create(&thread_ids[i], NULL, nget, &p); } for (i = 0; i < c; i++) { pthread_join(thread_ids[i], NULL); } gettimeofday(&end, NULL); run_time = end.tv_sec - start.tv_sec + 1e-6 * (end.tv_usec - start.tv_usec); printf("%d %f\n", c, run_time); return 0; }