linux下模拟shell的C程序--操作系统作业

发表于:2013-12-24 10:57

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未水    来源:51Testing软件测试网采编

/*%U %S %E %P %F %R %w %W*/
/*the process resource information is written in "temp.txt" with format as above*/
void print()
{
FILE *fp;
fp = fopen("temp.txt", "r");
if(fp==NULL)
{
printf("open error\n");
return ;
}
//get information from "temp.txt"
double user_time, sys_time, per,elapsed;
int maj_flt, min_flt, sa, sw;
if(fscanf(fp, "%lf %lf 0:%lf", &user_time, &sys_time, &elapsed)!=3) printf("wrong 1\n");
if(fscanf(fp, "%d %d %d %d %lf", &maj_flt, &min_flt, &sw, &sa, &per)!=5) printf("wrong 2\n");
printf("CPU time %.1lfms (user time %.1lfms, system time %.1lfms)\n", (user_time+sys_time)*1000.0, user_time*1000.0, sys_time*1000.0);
printf("elapsed time %.1lfms\nmajor fault %d, minor fault %d\nswap %d\ncontext switch %d\n", elapsed*1000.0,maj_flt, min_flt, sa, sw);
}
int solve(char *s)
{
/*make format like: /usr/bin/time -o temp.txt -f "%U %S %E %P %F %R %w %W" command*/
strcpy(tmp, s);
strcpy(s, pre);
int j=strlen(s);
s[j++]=' ';
s[j++]='\"';
strcpy(s+j, sp);
j=strlen(s);
s[j++]='\"';
s[j++]=' ';
strcpy(s+j, tmp);
/*output system command*/
printf("command is %s\n", s);
return system(s);
}
int main ()
{
int child_sta=0;
rusage usage;
time_t ep_t1, ep_t2;
while(gets(input))
{
int j = getToken(input);
if(j<0)  printf("input not obeying format\n");
else
{
strcpy(cmd, input+j);
int p = strlen(cmd)-1;
while(cmd[p]==' '&&p>=0) p--;
cmd[p+1] = '\0'; // delete white space after command
solve(cmd);
print();
}
}
return 0;
}
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号