Is it safe to do this:
int fd; void thread_main() { char buf[M]; ssize_t r = read(fd, buf, M); assert(r == M); ... } int main() { fd = open("/dev/urandom", O_RDONLY); for (int i = 0; i < N; i++) start_thread(i); for (int i = 0; i < N; i++) join_thread(i); }
That is: after open(2)ing "/dev/urandom"from the main thread, is it safe to read(2)unsynchronize from it from different thread contexts?
open(2)
"/dev/urandom"
read(2)
Under what circumstances will fire be approved? Will two streams receive the same data? What could go wrong?
, . assert . () - (, , "" , , 100%).
assert
/dev/urandom , , , , , . , , , , ( , , , ).
/dev/urandom
read/write ( undefined), / // . , , read/write . / .
read
write
, - , , - () ., , readv, ( /, -). , / readv/writev, , ( , PIPE_BUF, rodrigo).
readv
writev
PIPE_BUF
read , , , . A M , B M .., A, , B , A .
write.
, , , / 1 , , , - , /