rm -f large.txt; dd if=/dev/zero of=./large.txt count=10240 bs=1MB oflag=direct dd:…
- User
rm -f large.txt; dd if=/dev/zero of=./large.txt count=10240 bs=1MB oflag=direct dd: error writing './large.txt': Invalid argument 1+0 records in 0+0 records out 0 bytes copied, 0.000467066 s, 0.0 kB/s
- Assistant
Based on the error you're encountering, it seems that the issue lies with the
oflag=directoption you're using withdd. Thedirectflag attempts to avoid any buffering, directly writing to disk, which might not be supported or might have specific requirements on your system.Let's break down a couple of your options:
-
Remove the
oflag=directoption: This should resolve your issue, but note that it will use the system cache which might not be desirable for your use case:shrm -f large.txt dd if=/dev/zero of=./large.txt count=10240 bs=1MB -
Use
conv=fdatasyncfor data safety: If you want to ensure data is physically written to the disk before exits without using , you can use :
-
- User
在golang中为啥goroutine的调度开销比线程低?
- Assistant
在 Go 语言中,goroutine 的调度开销比传统的操作系统线程低,主要原因可以归结为以下几个方面:
-
轻量级设计: Goroutine 是比操作系统线程更轻量级的抽象。一个 goroutine 在创建和管理上所需的资源远少于一个线程。Go 程序可以在同一时间拥有数十万甚至数百万个 goroutine,而操作系统线程的数量通常仅限于数千个。
-
用户态调度: Goroutine 的调度是由 Go 运行时库在用户态(user space)完成的,而非依赖于操作系统的内核态(kernel space)线程调度。操作系统的线程调度器需要进行上下文切换,这涉及到内核态和用户态之间的切换,代价较高。Go 运行时库的用户态调度避免了这个开销,通过更高效的算法来调度 goroutine。
-
栈的初始大小较小: 每个 goroutine 的栈初始大小非常小(通常是几 KB),而操作系统线程的栈通常是几 MB。Go 运行时会动态地增长和收缩 goroutine 的栈,从而有效利用内存资源。与传统线程的大初始栈大小相比,goroutine 的这种机制减少了内存开销。
-
抢占式调度: Go 实现了一种抢占式调度,这意味着长时间运行的 goroutine 可以被强制中断,让其他 goroutine 获得执行机会。Go 运行时可以在垃圾回收或者系统调用之后检查是否需要进行调度,确保调度的公平性和及时性。
-
工作窃取调度算法: Go 使用了工作窃取(work stealing)调度算法,这种算法让闲置的处理器主动从其他忙碌的处理器队列中窃取任务,最大化利用多核性能。这种高效的负载均衡策略能够减少调度开销。
-
避免锁开销: Goroutine 能够通过基于通信的方式(如 channels)进行同步与数据共享,而不是传统线程的锁机制,从而减少锁竞争带来的开销。
总结起来,goroutine 的调度开销低是由于它的轻量级设计、用户态调度、灵活的栈管理、抢占式调度、工作窃取算法以及高效的同步机制,这些优化使得 Go 在处理并发任务时可以更有效、更高效地利用系统资源。
-