C语言 为什么第一次使用后返回0?如何克服这个问题?

iswrvxsc  于 2022-12-22  发布在  其他
关注(0)|答案(1)|浏览(105)

我正在创建一个简单的Sender和Reciever,以检查使用里诺CC算法和Cubic CC算法之间的时间差。为了在它们之间进行准确比较,我使用了struct timeval而不是clock,并且我还使用sudo tc qdisc add dev lo root netem loss 10%添加了对数据包丢失的检查
我的代码发送文件的2个部分一个部分在立方第二部分在里诺中,有几次,我要求它这样做.由于某种原因,timeval返回0在第二个循环,我尝试检查与gdb,并没有发现为什么和如何修复它.

相关码

while (1)
    {
        struct timeval start_t_cubic, end_t_cubic, tval_result_cubic; // will use them to check the timing
        struct timeval start_t_reno, end_t_reno, tval_result_reno; // will use them to check the timing

        // set the algorithm to cubic
        char *cc = "cubic";
        if (setsockopt(client_socket, IPPROTO_TCP, TCP_CONGESTION, cc, strlen(cc)) != 0)
        {
            printf("setsockopt failed \n");
            return;
        }

        gettimeofday(&start_t_cubic, NULL); // start the time

        while (num_of_bytes < BUFSIZE / 2)
        {
            recv(client_socket, client_message, 1, 0);
            num_of_bytes++;
        }

        gettimeofday(&end_t_cubic, NULL); // finish count for first part of the file

        timersub(&end_t_cubic, &start_t_cubic, &tval_result_cubic); // the total time cubic

             printf("algo: cubic, time: %ld.%06ld, iter num: %d\n",
               (long int)tval_result_cubic.tv_sec,
               (long int)tval_result_cubic.tv_usec,
               iteration_number);
     
        // change the algorithm to reno
        char *cc_algo = "reno"; // the CC algorithm to use (in this case, "reno")
        check(setsockopt(server_socket, IPPROTO_TCP, TCP_CONGESTION, cc_algo, strlen(cc_algo)),
              "setsockopt failed");


        gettimeofday(&start_t_reno, NULL); // start the time

        // recive a file of half mega bytes
        while (num_of_bytes < BUFSIZE)
        {
            recv(client_socket, client_message, 1, 0);
            num_of_bytes++;
        }

        gettimeofday(&end_t_reno, NULL);                         // finish count for first part of the file
        timersub(&end_t_reno, &start_t_reno, &tval_result_reno); // the total time reno
        printf("algo: reno, time: %ld.%06ld, iter num: %d\n", (long int)tval_result_reno.tv_sec, (long int)tval_result_reno.tv_usec, iteration_number);
        // store the time elapsed in a variable
        long int time_elapsed_reno = tval_result_reno.tv_sec * 1000000 + tval_result_reno.tv_usec;

       
    }

如果有人遇到这个问题,我会很高兴为任何帮助或提示,请
完整源代码:https://github.com/dolev146/networking

qlfbtfca

qlfbtfca1#

问题是在第二次迭代中函数时间太快,因此输出为0
电脑不知怎么优化了代码
我将usleep(1000)添加到中,这样时间差就不会为0

相关问题