测量 Linux 中进程上下文切换需要的时间
测量 Linux 中进程上下文切换需要的时间
源代码仓库:https://gitcode.com/opensourceworld/context_switch_cost,在 master
分支上。
Measuring the cost of a contest switch is a little trickier. The lmbench benchmark does so by runnig two processes on a single CPU, and setting up two UNIX pipes between them; a pipe is just one of many ways processes in a UNIX system can communicate with one another. The first process then issues a write to the first pipe, and waits for a read on the second; upon seeing the first process waiting for something to read from the second pipe, the OS puts the first process in the blocked state, and switches to the other process, which reads from the first pipe and then writes to the second. When the second process tries to read from the first pipe again, it blocks, and thus the back-and-forth cycle of communication continues. By measuring the cost of comm