我的目标是简单地比较创建 100 万个进程与线程(按顺序)的性能。每个进程/线程什么都不做,它们只能在当前进程/线程完成后运行。
下面是我的代码。当我编译它时,我得到了几个错误实例:
-bash: fork: retry: Resource temporarily unavailable
到底是怎么回事?
这是我的代码:
int main(int argc, char* argv[]){
int numberOfActions = 0;
int i;
if(argc != 3){
printf("usage: <thread/process> <number_of_threads/processes>\n");
exit(1);
}
numberOfActions = atoi(argv[2]); //number of threads/processes
if(strcmp(argv[1], "p") == 0){
printf("process\n");
int pid;
int status;
for(i = 0; i < numberOfActions; i++){
pid = fork();
if(pid < 0){//fork failed
printf("fork failed\n");
exit(0);
}
else{//fork succeeded
if(pid == 0){//child
continue;
}
else{//parent
waitpid(pid, &status, WUNTRACED);
exit(0);
}
exit(0);
}
}
}
else{
printf("thread\n");
pthread_t tidp;
int thread0;
int parameter = 0;
for(i = 0; i < numberOfActions; i++){
thread0 = pthread_create(&tidp, NULL, &continueThread, (void*) ¶meter);
if(thread0 != 0) printf("\ncan't create thread\n");
pthread_join(tidp, NULL);
}
}
return 1;
}