示例代码:
#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);
}
}
该程序打开 4 个线程,并在 stdout 上输出“缓冲区”的内容,该内容在 64 位 cpu 上为 128 字节或 16 个长整型。
如果我然后运行:
./写测试| pv -ptebaSs 800G >/dev/null
我得到的速度约为 7.5 GB/s。
顺便说一句,如果我这样做的话,这与我得到的速度是一样的:
$ mkfifo out
$ dd if=/dev/zero bs=16384 >out &
$ dd if=/dev/zero bs=16384 >out &
$ dd if=/dev/zero bs=16384 >out &
$ dd if=/dev/zero bs=16384 >out &
pv <out -ptebaSs 800G >/dev/null
有什么办法可以让它更快吗?
笔记。
实际程序中的缓冲区未填充零。
我的好奇心是了解单个程序(多线程或多进程)可以输出多少数据
看来4个人都没听懂这个简单的问题。
我什至把问题的原因加粗了。