感谢您关注我的问题!
我正在尝试使用clock_gettime 获取发送数据报和接收数据报之间的时间。
for(optval = 0; optval < 10; optval++){
clock_gettime(CLOCK_MONOTONIC, &ts_1);
sendto(sd, packet, sizeof(struct icmphdr), 0, (struct sockaddr*)&addr_req, sizeof(struct sockaddr_in));
recvfrom(sd, bufer, sz_buf, 0, (struct sockaddr*)&from_req, &tolen);
clock_gettime(CLOCK_MONOTONIC, &ts_2);
t_eq = (ts_2.tv_nsec - ts_1.tv_nsec) / 1000000.0;
total = (ts_2.tv_sec -ts_1.tv_sec) * 1000.0 + t_eq;
printf("\tt_total = %Lf ms\n", total);
sleep(1);
}
结论
t_total = 69.268422 ms
t_total = 72.526032 ms
t_total = 69.225890 ms
t_total = 70.945620 ms
t_total = 69.188274 ms
t_total = 69.181105 ms
t_total = 69.295679 ms
t_total = 72.721442 ms
t_total = 69.633848 ms
t_total = 69.522615 ms
如果我在新的终端仿真器会话中运行到另一台主机的标准ping程序,那么我的程序的输出将如下所示
t_total = 69.686600 ms
t_total = 0.044133 ms
t_total = 0.053209 ms
t_total = 0.043486 ms
t_total = 0.038839 ms
t_total = 0.042971 ms
t_total = 0.042133 ms
t_total = 0.043499 ms
t_total = 0.045853 ms
t_total = 0.018189 ms
这和什么有关系呢?