Multi-threaded C program much slower in OS X than Linux

前端 未结 3 1495
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 19:15

I wrote this for an OS class assignment that I\'ve already completed and handed in. I posted this question yesterday, but due to \"Academic Honesty\" regulations I took it off u

3条回答
  •  半阙折子戏
    2021-02-19 20:01

    The OP does not mention/show any code that indicates the thread(s) sleep, wait, give up execution, etc and all the threads are at the same 'nice' level.  
    

    so an individual thread may well get the CPU and not release it until it has completed all 2mil executions.

    This would result in a minimal amount of time performing context switches, on linux.

    However, on the MAC OS, a execution is only given a 'time slice' to execute, before another 'ready to execute' thread/process is allowed to execute.

    This means many many more context switches.

    Context switches are performed in 'sys' time.

    The result is the MAC OS will take much longer to execute.

    To even the playing field, you could force context switches, by inserting a nanosleep() or a call to release execution via

    #include 
    
    then calling
    
    int sched_yield(void);
    

提交回复
热议问题