Is read / recv safe (MSG_PEEK)

I have one thread that blocks read:

rec = read(socket, data, size);

I am curious to know if it is safe or not for me to have another thread, doing recv(MSG_PEEK)in the background on the same socket:

while (true)
{
   if (recv(socket, byte, size, MSG_DONTWAIT | MSG_PEEK) > 0)
   {
       printf("peeked %d", byte);
   }
   sleep(1);
}
+4
source share
1 answer

Since syscalls work in kernel space, they are thread safe (they must be, otherwise the kernel data may be damaged and destroy your entire system) in the sense that your program will not crash - HOWEVER, as Jeremy Friesner noted in the comments:

  • The execution order of your system calls is not necessarily guaranteed.

  • syscalls . , recv ().


FIFO. , read (), , recv ().

, , read (), ( , , ?) FIFO. , sleep () recv () read 1 FIFO printf () .

, ​​ recv () , . ( ), / .

+1

Source: https://habr.com/ru/post/1584095/


All Articles