【发布时间】:2019-10-14 23:55:58
【问题描述】:
现在,我正在学习系统内核实践课程。但是,当我将系统调用与用户调用进行比较时,奇怪的是系统调用返回的时间计数为 0 us(有时返回 1)。但是我通过了 count=1e8,这是一个很大的数字。
我怀疑计算没有发生,因为没有使用结果。然后我改变 add as result = result + 1 并打印最终结果。但是,结果是对的,时间计数只是从 0 或 1 变为 2-6。
long yanpan_oper(int* result,int num1,int num2,char* op)
{
if(op)
{
if(*op == '+')
{
*result = num1 + num2;
}
else if(*op == '-')
{
*result = num1 - num2;
}
else if(*op == '*')
{
*result = num1*num2;
}
else if(*op == '\\')
{
if(num2!=0)
*result = num1/num2;
else
printk("divided number can't be zero!\n");
}else
printk("unrecongized operator %c\n", *op);
}else
{
printk("operation is empty.\n");
}
return 0;
}
SYSCALL_DEFINE1(yanpan_func, int, count)
{
printk("The count is %d.\n", count);
struct timeval tstart, tend;
do_gettimeofday(&tstart);
int i;
for(i=0;i<count;i++) // +
{
int result;
char op_add = '+';
yanpan_oper(&result, i, 10, &op_add);
}
for(i=0;i<count;i++) // -
{
int result;
char op_sub = '-';
yanpan_oper(&result, i, 10, &op_sub);
}
for(i=0;i<count;i++) // *
{
int result;
char op_mul = '*';
yanpan_oper(&result, i, 2, &op_mul);
}
for(i=0;i<count;i++) // '//'
{
int result;
char op_div = '\\';
yanpan_oper(&result, i, 10, &op_div);
}
do_gettimeofday(&tend);
long delta_time = 1000000*(tend.tv_sec - tstart.tv_sec) + (tend.tv_usec - tstart.tv_usec);
printk("The start time is %ld.\n", tstart.tv_sec*1000000+tstart.tv_usec);
printk("The end time is %ld.\n", tend.tv_sec*1000000+tend.tv_usec);
printk("Syscall time use:%ld usec", delta_time);
return delta_time;
}
我尝试了很多次,但结果没有改变。相同数量的用户调用计算大约需要1300毫秒,内核中的计算可以这样快吗?
【问题讨论】:
-
我缺少
main函数。请生成minimal reproducible example -
另外,在使其最小化的过程中,您可能可以从
SYSCALL_DEFINE1中删除三个循环,从yanpan_oper中删除三个else if
标签: c linux-kernel operating-system system-calls