一、关于各种IO的执行情况分析
涉及的IO类型:
基本堵塞IO版本、
线程化版本。
服务器核心代码:
dg_echo.c
#include "unp.h"
void
str_echo(int sockfd)
{
ssize_t n;
char buf[MAXLINE];
int counter = 0;
again:
while ( (n = read(sockfd, buf, MAXLINE)) > 0){
Writen(sockfd, buf, n);
//printf("counter = %d\n", ++counter);
}
if (n < 0 && errno == EINTR)
goto again;
else if (n < 0)
err_sys("str_echo: read error");
}
本次试验是基于TCP回射服务器分析, 通过发送100MB的数据, 然后回射接受100MB, 并且计算其运行时间, 服务器程序将会统一运行,以此来分析,在堵塞IO, select下堵塞IO, 非堵塞IO的select版本, 以及堵塞IO的fork版本的运行时间和代码分析
1、基本堵塞IO
源代码如下所示:
dg_cli01.c
#include "unp.h"
#define NUM 1048576
#define LENGTH 1024
void
str_cli(FILE *fp, int sockfd)
{
char sendline[MAXLINE], recvline[MAXLINE];
struct timeval start, end;
gettimeofday(&start, NULL);
int n = 0 ;
int total = 0;
int i;
for(i = 0; i < NUM; ++i){
write(sockfd, sendline, LENGTH);
if ((n = read(sockfd, recvline, MAXLINE)) == 0)
err_quit("str_cli: server terminated prematurely");
if (n >= 0) total += n;
//Fputs(recvline, stdout);
}
gettimeofday(&end, NULL);
printf("use