2

Here is the scenario , My parent process has a range (a,b) and a number N.I want to compute all the primes between (a,b). But the processing part is done by N child processes.

So , I create a pipe and then divide the range in N parts.(a1,b1) ... (an,bn) .Then I create N child process.Then in the parent I write each range sequentially to the write end.

Here comes the problem , If there were only one child process I could've followed this answer. If multiple process were trying to write to the pipe , this answer would be acceptable. But Here multiple process is trying to read and a process is trying to read from the pipe should read exactly two integers one after another so this part should be atomic. May be I can try to use mutex , but don't know how to use mutex for interprocess.How can I achieve this ?

Community
  • 1
  • 1
Tamim Addari
  • 7,591
  • 9
  • 40
  • 59
  • Why not create N pipes, one for each child? But wait, if you create the ranges before the children, then they already know the ranges. What do you need the pipe for? – melpomene Sep 05 '15 at 13:22
  • I have to use pipe and signals to communicate ... – Tamim Addari Sep 05 '15 at 14:12
  • No, you don't. (proof by assertion) – melpomene Sep 05 '15 at 14:16
  • It's a pipe and signals assignment , I am learning how to use these things . – Tamim Addari Sep 05 '15 at 14:25
  • Why not simply? An integer "nextToHandle", common to all threads, mutex protected. – pasaba por aqui Sep 05 '15 at 14:46
  • Here it is multiple process , not thread , that is the problem – Tamim Addari Sep 05 '15 at 14:53
  • And if I use execve then I must use pipe. – Tamim Addari Sep 05 '15 at 16:40
  • 1
    If you write each pair of `int` values on the pipe in a single `write()` operation, and if each child reads a pair of `int` values from the pipe in a single `read()` operation, then there are no problems for you to worry about. If you split either the reads or the writes into two operations (one for each member of the pair), then you run into atomicity problems. But not if you are careful as described. – Jonathan Leffler Sep 05 '15 at 18:04
  • I was assuming 'binary data' being written. If you work with character data, then as long as the strings are all the same length and the children all read the correct length, there'll be no problems. – Jonathan Leffler Sep 05 '15 at 18:15
  • a pipe knows nothing about format of data. all it knows is a string of characters. This means that the protocol implemented in the code is the only area that knows anything about contents of the pipe. Suggest a separate pipe for each child process. – user3629249 Sep 06 '15 at 07:54

1 Answers1

0

How to lock a PIPE (or FIFO) with multiple reading processes?

You can use file locks on pipe descriptor:

  • flock(2) (specified in POSIX);
  • POSIX record locks, see "Advisory record locking" section in fcntl(2) and also lockf(3) wrapper (both specified in POSIX);
  • Open file description locks, see fcntl(2) (Linux-specific, available in recent kernels).

You can also use POSIX semaphores or some other IPC, but then you need separate file descriptor.


However you really don't need this in your scenario, since you can write and read two integers in one call which will be atomic, as mentioned in comments.

Writes and reads are atomic on pipe if buffer size is not greater than PIPE_BUF. See write(2):

An attempt to write to a pipe or FIFO has several major characteristics:

  • Atomic/non-atomic: A write is atomic if the whole amount written in one operation is not interleaved with data from any other process. This is useful when there are multiple writers sending data to a single reader. Applications need to know how large a write request can be expected to be performed atomically. This maximum is called {PIPE_BUF}.
Community
  • 1
  • 1
gavv
  • 4,649
  • 1
  • 23
  • 40