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

标签 c++ c linux sockets network-programming

关闭创建为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/

相关文章:

c++ - 为什么 C++ 中的 "delete [][]... multiDimensionalArray;"运算符不存在

c++ - 如何用c编写编程语言和头文件

c - 使用 strtok() 分解字符串并将其放入数组中

c - Linux-MIPS 系统调用保存的寄存器?

c++ - 嵌套 opencl 的内核函数

c++ - 充满 QLineEdits 的 QTableWidget 不会触发信号

c - 为什么我的写入没有阻塞到这个管道?

linux - GUI报告工具可从Postgresql挖掘数据并创建报告

c++ - 通过接口(interface)完成 copy-and-swap

c - 访问设备的寄存器 i2c