同一管道的多个读取过程都可以读取相同的消息 [英] multiple read processes of the same pipe can all read the same message

查看:922
本文介绍了同一管道的多个读取过程都可以读取相同的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对管道AP preciate帮助有些怀疑如果有谁知道

I have some doubts about pipes appreciate help if anyone knows

一个管可以通过多个进程同时允许这些进程交换​​信息互相共享。
当有同一管道的多个读取过程都可以读取相同的信息(只发送一次,而不是多个副本)?

A pipe can be shared by multiple processes simultaneously allowing these processes exchanging "messages" to each other. When there are multiple read processes of the same pipe can all read the same message (sent only once and not multiple copies)?

在多线程环境中,它写入管道时有可能被一个进程发送的消息已损坏?

In a multi-threaded environment it is possible that a message sent by a process is corrupted when writing to the pipe?

谢谢你能听

推荐答案

别再想信息。该管道接收字节序列,并且您的应用程序分配的结构序列负全部责任。写入管道的每个字节可以读取只有一次,而不是多次。如果每个写入到管道比系统规定量固定的大小和更小,那么写操作将是原子和写在数据将不会被破坏(与其他写入交错)。但是,如果任何的读者正在阅读一个不同尺寸的块,则数据可以被上读端交织。 (例如,写入器写入64字节为一个消息,但读者从管道中读取60个字节。然后读者期待一个64字节的序列将从另一个开始获取从一个和60的尾部的4个字节,并且您的数据可能会出现损坏)。这是很容易得到在多线程环境中损坏的数据。能够确保没有写入的交错,但它可以是很难得到它的权利。请将消息小有固定的大小和,并确保与写一个系统调用(比如,不要在FILE *使用fprintf中与管道作为底层的文件描述符。)

Stop thinking about "messages". The pipe accepts a sequence of bytes, and your application is entirely responsible for assigning any structure to the sequence. Each byte written to the pipe can be read exactly once, and not multiple times. If each write to the pipe is a fixed size and smaller than a system specified amount, then the write will be atomic and the data will not be "corrupted" (interleaved with other writes) during the write. However, if any of the readers are reading blocks of a different size, then data may be interleaved on the read end. (eg, a writer writes 64 bytes as one "message", but a reader reads 60 bytes from the pipe. The reader expecting a 64 byte sequence will then get 4 bytes from the tail of one and 60 from the start of another, and your data may appear "corrupted"). It is very easy to get "corrupted" data in a multi-threaded environment. It is possible to ensure that none of the writes are interleaved, but it can be difficult to get it right. Keep the messages small and of a fixed size, and make sure the are written with a single write system call (eg, do not use fprintf on a FILE* with the pipe as the underlying file descriptor.)

请注意,我使用这个词在引号损坏贯穿始终,因为我相信你真的是交错的。你写的字节序列不会从系统的角度损坏。你可能无法取回你所期望的,但事实并非数据损坏。相反,它是一个编程错误。

Note that I'm using the word "corrupted" in quotes throughout, because I believe you really mean interleaved. The byte sequence that you write will not be corrupt from the system's perspective. You may not get back what you expect, but that is not corruption of the data. Rather, it is a programming error.

这篇关于同一管道的多个读取过程都可以读取相同的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆