On Wed, 2007-10-10 at 07:03 +0530, Himanshu Chauhan wrote: > Dear all, > > I have a question regarding two processes trying to read from same driver. > > Context: Suppose the driver maintains a circular buffer and when the > head and tail are same any read request will be blocked. When driver has > data, it asynchronously puts the data in the buffer. In the read system > call then calls wake_up_interruptible() copying the data in user space > buffer. > > Question: > If two processes are sleeping on the read() system call for a driver and > when driver has data will both of the processes will get the data or > will there be a race condition? When you call wake_up_interruptible() you will wake up *all* threads. Whether there is a race depends on what the driver does after it is woken up. > Is it like, the process which wakes up > earlier, reads the data and as a result head and tail become same again. > By the time the next process comes, it again finds there is nothing to > read. If this is the case, then what is the way to share the data? One > way I can think of if having a user space library and all read/writes > should be done only through that library. Yes, the first thread will read all available data and any other thread will have nothing to read. You have to split the data on your own. e.g. allocate a ringbuffer with x "cells" of 4096 (or whatever) bytes. Then allocate a per-cell read counter: After a cell is written the read counter is the number of threads. Every thread has its own read pointer. Before when a thread has fully read a cell it decrements the read counter of that cell. When the read counter is 0, the driver knows that it can put data there. If you don't want that latency, you can create a list of read pointers (one entry per thread) and go through the list when you want to know how much you can write to the ring buffer. Or you can create a per-thread ringbuffer. -- programing a layer 3+4 network protocol see http://michaelblizek.homelinux.net -- To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx Please read the FAQ at http://kernelnewbies.org/FAQ