gpt4 book ai didi

c++ - 关闭 AF_PACKET 与 AF_INET 的时间差?

转载 作者:太空狗 更新时间:2023-10-29 12:22:30 31 4
gpt4 key购买 nike

关闭创建为AF_PACKETAF_INET 的套接字之间存在这种时间差异的原因是什么?如何减少 AF_PACKET 的关闭时间?

sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
close(sockfd); // 60000 μs

sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
close(sockfd); // 30 μs

重现行为的代码:

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

#include <sys/socket.h>
#include <net/if.h>

int main() {
struct timeval time_start, time_end;
int sockfd;

if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("socket error");
}

gettimeofday(&time_start, NULL);
close(sockfd);
gettimeofday(&time_end, NULL);
printf("close AF_PACKET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));


if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("socket error");
}

gettimeofday(&time_start, NULL);
close(sockfd);
gettimeofday(&time_end, NULL);
printf("close AF_INET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));

return 0;
}

最佳答案

逻辑上这两个关闭系统调用没有显着差异

如果我像下面这样更改你的程序,

`

int main() {
struct timeval time_start, time_end;
int sockfd;

if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("socket error");
}

gettimeofday(&time_start, NULL);
close(sockfd);
gettimeofday(&time_end, NULL);
printf("close AF_INET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));

if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("socket error");
}

gettimeofday(&time_start, NULL);
close(sockfd);
gettimeofday(&time_end, NULL);
printf("close AF_PACKET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));

return 0;
}`

我看到的结果是

shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 3471
close AF_PACKET: 4
shahul@shahul-VirtualBox:~/test$ ./a.out
close AF_INET: 8
close AF_PACKET: 7
shahul@shahul-VirtualBox:~/test$ ./a.out
close AF_INET: 8
close AF_PACKET: 6
shahul@shahul-VirtualBox:~/test$ ./a.out
close AF_INET: 8
close AF_PACKET: 5
shahul@shahul-VirtualBox:~/test$ ./a.out
close AF_INET: 9
close AF_PACKET: 5

类似的,如果我改成下面这样,

int main() {
struct timeval time_start, time_end;
int sockfd;

if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("socket error");
}

gettimeofday(&time_start, NULL);
close(sockfd);
gettimeofday(&time_end, NULL);
printf("close AF_INET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));

if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("socket error");
}

gettimeofday(&time_start, NULL);
close(sockfd);
gettimeofday(&time_end, NULL);
printf("close AF_INET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));

if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("socket error");
}

gettimeofday(&time_start, NULL);
close(sockfd);
gettimeofday(&time_end, NULL);
printf("close AF_PACKET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));



return 0;
}

结果如下,

shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 6
close AF_INET: 3
close AF_PACKET: 3
shahul@shahul-VirtualBox:~/test$ ./a.out
close AF_INET: 8
close AF_INET: 4
close AF_PACKET: 5
shahul@shahul-VirtualBox:~/test$ ./a.out
close AF_INET: 8
close AF_INET: 4
close AF_PACKET: 4
shahul@shahul-VirtualBox:~/test$ ./a.out
close AF_INET: 8
close AF_INET: 4
close AF_PACKET: 4

结论:基本上,第一次近距离通话需要更多时间,可能需要时间来映射第一次关闭系统调用函数

关于c++ - 关闭 AF_PACKET 与 AF_INET 的时间差?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56236545/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com