Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Why is there no critical section in the pipe? For example, as in shared memory. In pipe have general data, which in common use in joint processes, but in the shared memory also have general data, which in common use in joint processes.

share|improve this question
    
What is "the pipa"? Do you mean pipe? –  Renan Nov 19 '12 at 12:17
    
Sorry, I made a mistake. Why is there no critical section in the pipE? –  user1823811 Nov 19 '12 at 13:30
    
@user1823811 You can edit your own posts to fix spelling mistakes. –  jw013 Nov 19 '12 at 15:19
    
@jw013 protip: if you write [edit], it will automatically turn into a link, like this: edit –  strugee Dec 9 '13 at 19:01
add comment

2 Answers 2

I'm not completely sure what you're asking, I think you're asking why its that if you used shared memory, you have to also use locking to ensure your writer and reader do not conflict, but you can write/read from a pipe without any locking. Of course, the extent of locking required depends on the data structure used; e.g., there are ring buffers that are mostly lock-free.

If so, the answer is fairly simple: a pipe is an abstraction. The locking for the pipe (to the extent the pipe data structures require it) is hidden in the kernel.

share|improve this answer
    
That's my guessing of the intent of the question as well. We could add that the critical section is actually in the kernel code for the write and read system calls. –  Stéphane Chazelas Nov 19 '12 at 20:50
    
Thanks for the answer! Sorry for my english! But I asked why is there no critical section in the pipe? Critical section - it is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution For example,critical section is used in shared memory. –  user1823811 Nov 20 '12 at 1:51
    
@user1823811 any critical sections are in kernel code—hidden behind the read and write syscalls—so you don't see them. –  derobert Nov 20 '12 at 16:23
    
Thanks for your answer! –  user1823811 Nov 21 '12 at 9:04
add comment

Pipe writes and reads below PIPE_BUF size (usually the same as a memory page size) are guaranteed to be atomic so there is no need for a userland synchronization mechanism to protect it - the kernel is already doing it. Writes/reads larger than PIPE_BUF would require a synchronization mechanism. In reality you don't often see large volumes of data pushed down a pipe in one swallow so you don't see mutexes and such being used very much but they are required when circumstances beyond PIPE_BUF arise.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.