2

I am writing a program with a named pipe with multiple readers and multiple writers. The idea is to use that named pipe to create pairs of reader/writer. That is:

  • A reads the pipe
  • B writes in the pipe (vice versa)
  • Pair A-B created!

In order to ensure that only one process is reading and one is writing, I have used 2 locks with flock. Just like this.

Reader Code:

echo "[JOB $2, Part $REMAINING] Taking next machine..."
    VMTAKEN=$((
    flock -x 200;
    cat $VMPIPE;
    )200>$JOINQUEUELOCK)

echo "[JOB $2, Part $REMAINING] Machine $VMTAKEN taken..."

Writer Code:

((
flock -x 200;
echo "[MACHINE $MACHINEID] I am inside the critical section"
echo "$MACHINEID" > $VMPIPE;
    echo "[MACHINE $MACHINEID] Going outside the critical section"
)200>$VMQUEUELOCK)

echo "[MACHINE $MACHINEID] Got new Job"

I sometimes get the following problem:

[MACHINE 3] I am inside the critical section
[JOB 1, Part 249] Taking next machine...
[MACHINE 3] Going outside the critical section
[MACHINE 1] I am inside the critical section
[MACHINE 1] Going outside the critical section
[MACHINE 1]: Got new Job
[MACHINE 3]: Got new Job
[JOB 1, Part 249] Machine 3
1 taken...

As you can see, Another writer wrote before the reader finished reading. What can I do to get rid of this problem? Should I use an ACK Pipe or something?

Thank you in advance

Mat
  • 202,337
  • 40
  • 393
  • 406
  • What are the semantics of the `flock` command? Which file does it lock? Who wrote it? Are you sure it does what it is supposed to do? (I can only find manual pages for the `flock()` function call, not for a command wrapping it.) – Jonathan Leffler Dec 29 '10 at 15:31
  • It looks like the problem is not with flock, but with writing in the named pipe. As you can see in the output, the two machines are not in the critical section at the same time. – Javier J. Salmeron Garcia Dec 29 '10 at 16:08

1 Answers1

0

This would be a typical use for semaphores:

  1. Create 2 semaphores - one for reading processed, the other one for writing processes. set each semaphore to value 1

  2. Reading processes sem_wait(2) on the semaphore for readers until semphore > 0 and lower it to zero if they get it.

  3. Writing processes will do the same with the semaphore intended for them

  4. A controlling process (which may also set up the semaphores initially) could check, if both semaphores are zero and assign the pair

  5. reader/writer release the semaphores (increasing them by 1 again) so next readr or writer will get the semaphore.

For passing informations between reader/writer shared memory may be used...

ktf
  • 6,865
  • 1
  • 13
  • 6