#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次
未完待续
本文通过C语言程序测试了不同操作系统环境下每秒可以进行的进程fork操作的最大次数。实验结果显示,在CentOS虚拟机中每秒约能fork 2800-3000次,在Ubuntu系统中则为4800-5600次。
427

被折叠的 条评论
为什么被折叠?



