• linux时间片测试


    如果在时间片结束时进程还在运行,则CPU将被剥夺并分配给另一个进程。如果进程在时间片结束前阻塞或结束,则CPU 当即进行切换

    测试条件
    单核CPU

    示例代码

    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <sys/time.h>
    
    void time_print()
    {
            struct timeval tv; 
            gettimeofday(&tv,NULL);
            printf("microsecond:%ld
    ",tv.tv_sec*1000000 + tv.tv_usec);  //微秒
    }
    
    void *fun(void *arg)
    {
            printf("I'm thread, Thread ID = %lu
    ", pthread_self());
            while(1)
            {   
                    time_print();
                    usleep(10);
            }   
            return NULL;
    }
    
    void *fun1(void *arg)
    {
            printf("I'm thread 1, Thread ID = %lu
    ", pthread_self());
            while(1)
            {   
                    sleep(1);
            }   
            return NULL;
    }
    
    int main()
    {
            pthread_t tid;
            pthread_t tid1;
    
            pthread_create(&tid, NULL, fun, NULL);
            pthread_create(&tid, NULL, fun1, NULL);
    
            void *status;
            pthread_join(tid, &status);
    
            return 0;
    }
    

    效果

    microsecond:1281790443
    microsecond:1281790572
    microsecond:1281790939
    microsecond:1281791501
    microsecond:1281791891
    microsecond:1281792407
    microsecond:1281792877
    microsecond:1281793379
    microsecond:1281793868
    microsecond:1281794393
    microsecond:1281794901
    microsecond:1281795347
    microsecond:1281795866
    microsecond:1281796399
    microsecond:1281796968
    microsecond:1281797427
    microsecond:1281797955
    microsecond:1281798403
    microsecond:1281798513
    microsecond:1281798708
    microsecond:1281798862
    microsecond:1281799380
    microsecond:1281799859
    microsecond:1281800359
    microsecond:1281800835
    microsecond:1281801364
    microsecond:1281801822
    microsecond:1281802345
    microsecond:1281802835
    microsecond:1281803318
    microsecond:1281803832
    microsecond:1281804282
    microsecond:1281804809
    microsecond:1281805349
    

    代码修改

    void *fun1(void *arg)
    {
            printf("I'm thread 1, Thread ID = %lu
    ", pthread_self());
            while(1)
            {   
                    //sleep(1);
            }   
            return NULL;
    }
    

    效果

    microsecond:2104360508
    microsecond:2104360736
    microsecond:2104360960
    microsecond:2104361436
    microsecond:2104361992
    microsecond:2104362669
    microsecond:2104366038
    microsecond:2104366282
    microsecond:2104367009
    microsecond:2104367406
    microsecond:2104367803
    microsecond:2104368249
    microsecond:2104368569
    microsecond:2104369056
    microsecond:2104369396
    microsecond:2104369848
    microsecond:2104370382
    microsecond:2104371851
    microsecond:2104375930
    microsecond:2104376353
    microsecond:2104376543
    microsecond:2104377816
    

    结论
    通过第二个测试,可以看出时间片切换是ms级别的

    如有错误,欢迎指正

  • 相关阅读:
    [转]C#里 泛型Where和 new()的使用
    测试
    C#中的static、readonly与const的比较
    将字符串格式化变为两位
    在VS中对字段进行包装
    安装mysql的心得
    关于mysql数据库的乱码问题
    timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
    解决向数据库mysql插入double数据小数点不显示问题
    JDBOOK
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/14746002.html
Copyright © 2020-2023  润新知