关闭AF_PACKET与AF_INET的时差?

时间:2019-05-21 10:41:56

标签: 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;
}

1 个答案:

答案 0 :(得分:1)

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

如果我像下面那样更改程序,

`

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 

结论: 基本上,首次通话需要更多时间, 也许需要花时间第一次映射关闭系统调用功能