看看代码如何完善

请教大家看看代码怎么完善
运行环境:linux
代码功能:申请一个u32 buffer[8192]个元素的数组,数组使用 ‘a’填充,
  使用while循环不停的写,每次写32K的数据,count用来记数,写多少次
  然后来统计总共写入的数据量,最后跟据时间间隔和写入的总数据量计算写入速度
  代码中使用一个线程handler()函数,每隔5秒打印一次统计信息
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<time.h>
#include <sys/time.h>
#include<pthread.h>
#include<signal.h>
#define u32 unsigned int 

int rc;
int i;
int count=0;
  struct timeval start,end;
const float change=0.03125;//buffer的大小是32K,算成MB,即除以1024=0。03125MB/S
time_t usetime,run_time1,run_time2;//用来计时
float speed;//写入速度

u32 buffer[8192];//u32的数组,大小为8192 * 4字节 再除以 1024即32K
float totalData;//写入的总数据
int fd;
void handler()
{

signal(SIGALRM,handler);
alarm(10);

rc=gettimeofday(&end,NULL);
run_time2=end.tv_sec*1000000+end.tv_usec;

printf("******write result*******\n");
printf("count is %d\n",count);
printf("run_time1 is:%u\n",run_time1);
printf("run_time2 is:%u\n",run_time2);

//写入总数据量
totalData=change*count*1000000;
printf("tolData =%f\n",totalData);
usetime=run_time2-run_time1;
speed=totalData/usetime;
printf( "Write time used %u usec\n",usetime);
printf( "Write speed is: %0.1f MB/S\n",speed);


}


int main(void)
{
int ret;
pthread_t id1,id2;
for(i=0;i<8192;i++){
buffer[i]='a';
}

fd=open("1.txt",O_RDWR);
rc=gettimeofday(&start,NULL);
run_time1=start.tv_sec*1000000+start.tv_usec;
ret=pthread_create(&id1,NULL,(void *) handler,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
while(1){
lseek(fd,0,SEEK_SET);
write(fd,buffer,8192*sizeof(u32));
count++;


pthread_join(id1,NULL);
close(fd);
exit(0);
return 0;
}
 


  运行结果:
******write result*******
count is 0
run_time1 is:3208762584
run_time2 is:3208771057
tolData =0.000000
Write time used 8473 usec
Write speed is: 0.0 MB/S
******write result*******
count is 66815
run_time1 is:3208762584
run_time2 is:3218771208
tolData =2087968768.000000
Write time used 10008624 usec
Write speed is: 208.6 MB/S
******write result*******
count is 135189
run_time1 is:3208762584
run_time2 is:3228771277
tolData =4224656128.000000
Write time used 20008693 usec
Write speed is: 211.1 MB/S
******write result*******
count is 203057
run_time1 is:3208762584
run_time2 is:3238771825
tolData =6345531392.000000
Write time used 30009241 usec
Write speed is: 211.5 MB/S
第一次的结果写的数据是0,都没写数据就执行了,
以后的每次统计都是把前面的总count加起来,时间也是累加的,统计的结果用总时间除以总数据,
(我的想法是用每隔10秒统计这10秒的时间和写入的数据,)
我试过在handler()里面把count ,run_time都初始化为0,也不行。要该怎么弄呢?

还一个问题,我的时间用秒*1000000+微妙的表示对吗?我知道只用秒表示大概是133开头的10位数,但是用秒+微妙就是32开头的10位了,不知道怎么来判断这种表示对不对?
麻烦大家指点下,谢谢

------解决方案--------------------
mark一下
------解决方案--------------------
还一个问题,我的时间用秒*1000000+微妙的表示对吗?
是表示微秒的