Linux 3.5: Safe for `reading (2)` from the same fd `open (2)` / dev / urandom` from multiple threads?

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?

Under what circumstances will fire be approved? Will two streams receive the same data? What could go wrong?

+5
source share
2 answers

, . assert . () - (, , "" , , 100%).

/dev/urandom , , , , , . , , , , ( , , , ).

read/write ( undefined), / // . , , read/write . / .

, - , , - () .
, , readv, ( /, -). , / readv/writev, , ( , PIPE_BUF, rodrigo).

+6

read , , , . A M , B M .., A, , B , A .

write.

, , , / 1 , , , - , /

+2

All Articles