测试创建和销毁进程开销于创建和销毁线程开销对比
2010-04-22 15:51
447 查看
//对比进程创建和线程创建的时间开销 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/time.h> #include <pthread.h> void* thr_fun(void* arg) { pthread_exit(NULL); } int main(int argc, char *argv[]) { struct timeval tv1; struct timeval tv2; unsigned long long t1, t2; int i; pid_t pid; pthread_t tid; if( gettimeofday(&tv1, NULL)!=0 ) { perror("gettimeofday"); exit(1); } for( i=0 ; i<10000 ; i++ ) { pid=fork(); if( pid<0 ) { perror("fork"); exit(1); } else if( 0==pid ) { exit(0); } else { wait(NULL); } } if( gettimeofday(&tv2, NULL)!=0 ) { perror("gettimeofday"); exit(1); } t1=(tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec); printf("生成和销毁10000个进程共耗时%Lu微妙/n", t1); if( gettimeofday(&tv1, NULL)!=0 ) { perror("gettimeofday"); exit(1); } for( i=0 ; i<10000 ; i++ ) { if( pthread_create(&tid, NULL, thr_fun, NULL)!=0 ) { perror("pthread_create"); exit(1); } pthread_join(tid, NULL); } if( gettimeofday(&tv2, NULL)!=0 ) { perror("gettimeofday"); exit(1); } t2=(tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec); printf("生成和销毁10000个线程共耗时%Lu微妙/n", t2); printf("进程是线程开销的%f倍/n", (double)t1/(double)t2); exit(0); }
进程的开销大概是线程的10倍,但不一定准。
相关文章推荐
- 进程与线程(二)——进程的管理、创建与销毁
- 通过hook Linux内核函数,监控进程/线程创建与销毁
- 内核层监控进程 线程 创建和销毁
- 【进程线程与同步】5.1 创建和销毁子进程
- 【进程线程与同步】5.1 创建和销毁子进程
- 内核层监控进程 线程 创建和销毁
- Linux 进程管理剖析: 创建、管理、调度和销毁进程
- 线程创建未回收导致的内存泄漏问题(进程中的堆栈内存,并不是malloc出来的内存)
- 【C++】Windows线程的创建、执行、互斥、同步、销毁
- 【Python】线程的创建、执行、互斥、同步、销毁
- MySQL连接与创建pthread开销对比
- 线程及与进程的对比
- 【深入Java开发】JVM源码分析之一个Java进程究竟能创建多少线程
- Dalvik虚拟机进程和线程的创建过程分析
- APUE学习之多线程编程(一):线程的创建和销毁
- 线程自动创建与销毁
- 【归纳总结】Unix/linux下的进程管理(二):创建进程的函数及其应用、对比
- Linux进程/线程协作 之 创建指定数量的进程
- java创建线程的三种方式及其对比
- Dalvik虚拟机进程和线程的创建过程分析