我想比较linux'下的进程和线程的创建、切换已经终止的时间开销,还有些问题

blues_guo 2003-08-07 11:44:35
我想比较linux'下的进程和线程的创建、切换已经终止的时间开销,现在对进程的切换还有一些问题,其他的也只有初步构想,不知道大虾们愿意给我一些提示吗?谢谢啦

====================================我是新手,请大家多多关照
...全文
62 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mynetpc 2003-08-25
  • 打赏
  • 举报
回复
最好加个&&pid!=0来限制
queshuiyu 2003-08-20
  • 打赏
  • 举报
回复
操作系统的实习作业居然放到这里了
无语。。。。。。
blues_guo 2003-08-13
  • 打赏
  • 举报
回复
忘说了,我在RedHatLinux Kernl2.2.14下调试的
blues_guo 2003-08-13
  • 打赏
  • 举报
回复
/************
gettimeofday(&tpstart,NULL);
for(i=0;i<1000;i++)
pid_t fork();
gettimeofday(&tpend,NULL);
****************/
//这一段会有些问题吧?你fork了1000个啊?后面不会乱?
//看看我的代码有没有问题
#include "sys/time.h"
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#define TIMES 1000 /* 试验的次数 */

main()
{
pid_t pid;
struct timeval tpstart,tpend;
double timeuse;
double timeall=0.0;
int i;

for(i=0;i<TIMES;i++)
{
gettimeofday(&tpstart,NULL);
pid = fork();
gettimeofday(&tpend,NULL);
if(!pid )
exit(0);
else if (pid > 0)
{
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
printf("creat a fork use time is %fms pid is %d\n",timeuse,pid);

timeall+=timeuse;
}
else
printf("Fork fail\n");
}
timeuse=timeall/i;
printf("creat %d forks use %fms \nevery fork time is %fms\n",i,timeall,timeuse);

}
deshun205 2003-08-10
  • 打赏
  • 举报
回复
/*进程创建*/
#include
#include
#include
#include
#include


struct timeval {
long tv_sec; /* 秒数 */
long tv_usec; /* 微秒数 */
};

int gettimeofday(struct timeval *tv,struct timezone *tz);


main()
{
struct timeval tpstart,tpend;
float timeuse;
unsigned int i;

gettimeofday(&tpstart,NULL);

for(i=0;i<1000;i++)
pid_t fork();

gettimeofday(&tpend,NULL);
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
printf("Used Timef\n",timeuse);
exit(0);
}

/*线程创建*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>


struct timeval {
long tv_sec; /* 秒数 */
long tv_usec; /* 微秒数 */
};

int gettimeofday(struct timeval *tv,struct timezone *tz);


main()
{
struct timeval tpstart,tpend;
float timeuse;
unsigned int i;
int rc;

int p1 = 0;

gettimeofday(&tpstart,NULL);
for(i=0;i<1000;i++)


rc = pthread_create(&sid, NULL, (void*)&threads, (void *)&p1))



gettimeofday(&tpend,NULL);
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
printf("Used Timef\n",timeuse);
exit(0);
}

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧