for example, i want to get an array of 4 ints from child to parent. parent calls
read(apipe, buf, sizeof(int)*4);
child calls
for(int i=0; i<4;i++)
write(bpipe, &array[i], sizeof(int));
does this do what I intend (getting 4 ints to the parent) or does the parent simply get the first integer?
I tried searching for this answer elsewhere, but either I don’t know how to search or this is too subtle (or on the other hand too seemingly obvious) for literature to dwell on it much.
EDIT: To clarify further, I was trying to write a 4-part message and read all 4 parts in one read(). See comments on accepted answer.
readandwritework with bytes, not messages. For details of how they behave with pipes, see the documentation in POSIX:In your code, I think the
readshould always get 4 ints, due to:There will always be 4 ints available for reading, because
4*sizeof(int) < PIPE_BUFand thus writes of this size are atomic.It’s possible that the allowance for
readto return a short read when interrupted by a signal could come into play, but that should not be able to happen (in the real world, at least) when sufficiently many bytes are available immediately.