操作系统实验时候整理的一些知识点。有小错请见谅哦。
(1)Linux中time命令是用来计算某个程序的运行耗时(real),用户态cpu耗时(user),系统态cpu耗时(sys)。
(2)time命令最常用的使用方式就是在其后面直接跟上命令和参数:time <command> [<arguments...>]。
(3)函数:ctime ;功能:把日期和时间转换为字符串;用法: char *ctime(const time_t *time)。
(4)sleep:将进程挂起,不再占用CPU(自动放弃CPU)。在linux里,sleep 的单位是秒,即sleep 1000就是睡眠了1000 秒,而在windows中,如果调用sleep函数单位是毫秒,sleep(1000)则是睡眠了1秒。
(5)exit:exit命令同于退出shell,并返回给定值。在shell脚本中可以终止当前脚本执行。执行exit可使shell以指定的状态值退出。若不设置状态值参数,则shell以预设值退出。状态值0代表执行成功,其他值代表执行失败。语法:exit(参数)。
·调试方法描述:(各种调用的调试源代码)
① time:
#include<stdio.h>
#include<unistd.h>
#include<time.h>
#include<iostream>
using namespace std;
int main()
{
while(1)
{
char timebuf[100];
time_t t;
time(&t);
strftime(timebuf,sizeof(timebuf),"%Y年%m月 %d 日 %H:%M:%S",localtime(&t));
cout<<timebuf<<endl;
fflush(stdout);
sleep(1);
}
return 0;
}
② ctime:
#include<time.h>
#include<stdio.h>
void main()
{
time_t timep;
time (&timep);
printf("%s",ctime(&timep));
}
③sleep:
#include<unistd.h>
#include<stdio.h>
int main()
{
int a;
a = 1;
printf("Hello");
sleep(a);
printf(" world\n");
return 0;
}
④ exit:
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Hello\n");
exit(0);
}