linux下模拟shell的C程序--操作系统作业
作者:网络转载 发布时间:[ 2013/12/24 16:33:15 ] 推荐标签:linux shell 操作系统
/*%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
");
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
");
if(fscanf(fp, "%d %d %d %d %lf", &maj_flt, &min_flt, &sw, &sa, &per)!=5) printf("wrong 2
");
printf("CPU time %.1lfms (user time %.1lfms, system time %.1lfms)
", (user_time+sys_time)*1000.0, user_time*1000.0, sys_time*1000.0);
printf("elapsed time %.1lfms
major fault %d, minor fault %d
swap %d
context switch %d
", 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
", 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
");
else
{
strcpy(cmd, input+j);
int p = strlen(cmd)-1;
while(cmd[p]==' '&&p>=0) p--;
cmd[p+1] = ' |