#include <sys/time.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
struct timeval start,end;
int forkcount;
int main(int argc, char *argv[])
{
pid_t pid, ppid, forkpid;
int i,timeuse,j;
gettimeofday(&start, NULL);
while(1){
forkpid = fork();
forkcount++;
if (forkpid > 0){
waitpid(forkpid, NULL, 0);
}
else if (forkpid == 0){
//printf("pid=%d ppid=%d\n",getpid(),getppid());
//sleep(10);
//break;
j++;
exit(0);
}
else{
printf("fork error\n");
}
gettimeofday(&end, NULL);
timeuse = 1000000*(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
if (timeuse >= 1000000)
break;
}
//gettimeofday(&end, NULL);
timeuse = 1000000*(end.tv_sec -start.tv_sec) + end.tv_usec - start.tv_usec;
printf("timeuse=%d forkcount=%d\n",timeuse,forkcount);
}
不知道这样测试电脑每秒能fork的次数 是否正确
虚拟机
系统:centos
内存:512M
每秒大概fork 2800-3000次
系统:ubuntu
内存:2G
fork/s 4800-5600次
未完待续