示例代码:
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <pthread.h>
int
main (int argc, char **argv)
{
unsigned char buffer[128];
char buf[0x4000];
setvbuf (stdout, buf, _IOFBF, 0x4000);
fork ();
fork ();
pthread_t this_thread = pthread_self ();
struct sched_param params;
params.sched_priority = sched_get_priority_max (SCHED_RR);
pthread_setschedparam (this_thread, SCHED_RR, ¶ms);
while (1)
{
fwrite (&buffer, 128, 1, stdout);
}
}
Run Code Online (Sandbox Code Playgroud)
该程序打开 4 个线程,并在 stdout 上输出“buffer”的内容,该内容在 64 位 cpu 上为 128 字节或 16 个长整型。
如果我然后运行:
./写测试| pv -ptebaSs 800G >/dev/null
我得到的速度约为 7.5 GB/s。
顺便说一句,如果我这样做的话,这与我得到的速度是一样的:
$ …Run Code Online (Sandbox Code Playgroud)