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

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

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

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

  在shell中time函数可以输出调试信息,在terminal输入man time了解用法
  用自己定义的格式写入一个文件,然后从这个文件读取数据输出
  一个模拟shell的supershell就完成了
  system()函数
  time函数在linux里有2个,一个在batch直接输入time,一个在usr/bin/time,后者可以得到更多信息
  程序的输入可以是 % supershell command 单词之间可以有任意空格
  command必须是符合要求的输入,不然统计结果会出错
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
#include <unistd.h>
#include <ctime>
#define N 1000
char input[N];
char cmd[N],tmp[N];
const char sp[]="%U %S %E %F %R %w %W %P";
const char pre[]="/usr/bin/time -o temp.txt -f";
/*get the command from string s, format "% supershell command"*/
int getToken(char *s)
{
int len = strlen(s);
int i=0;
while(s[i] == ' ' && i<len) i++;
if(s[i]!='%'||i>=len) return -1; //not obey format
i++;
while(s[i] == ' '&& i<len) i++;
if(i>=len) return -1;
char temp[]="supershell";
for(int j=0;j<strlen(temp);++j,++i)
if(s[i]!=temp[j])
return -1;
while(s[i]==' '&& i<len) i++;
if(i>=len) return -1;
return i;
}
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号