我正在编写一个带有多个读取器和多个写入器的命名管道的程序。这个想法是使用该命名管道来创建读取器/写入器对。那是:
- A读取管道
- B 在管道中写入(反之亦然)
- 对 AB 已创建!
为了保证只有一个进程在读,一个在写,我用了2个锁和flock。像这样。
读者代码:
echo "[JOB $2, Part $REMAINING] Taking next machine..."
VMTAKEN=$((
flock -x 200;
cat $VMPIPE;
)200>$JOINQUEUELOCK)
echo "[JOB $2, Part $REMAINING] Machine $VMTAKEN taken..."
编写器代码:
((
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"
我有时会遇到以下问题:
[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...
如您所见,另一位作家在读者阅读完之前就写了。我能做些什么来摆脱这个问题?我应该使用 ACK 管道还是什么?
先感谢您