What is the best way to send linked list structure between processes via pipe in linux programming?

You can also use System V shared memory (look at functions like shmat ) or mmap to have memory shared between the processes. Boost. Interprocess has a C++ wrapper around those calls such that you can create a linked list directly in shared memory without copying.

You can also use System V shared memory (look at functions like shmat) or mmap to have memory shared between the processes. Boost. Interprocess has a C++ wrapper around those calls such that you can create a linked list directly in shared memory without copying.

I am assuming you are trying to implement a Sieve of Eratothenes using multiprocessing. Unless I am misintepreting what you are describing, then your algorithm can be improved. What you describe is a system where a list of numbers is passed to a child process that checks ALL of the values before handing the rest of the list to the next child.

I would implement the algorithm as a pipeline of processes Generator -> Prime2 -> Prime3 -> Prime5 -> Prime7 -> ... PrimeP where the next Prime process is created by last process on the chain. So when 8 is generated, it gets filtered out by Prime2 and dropped. When 9 is sent, it is passed to Prime2, which passes it to Prime3 which drops it.10 is then dropped by 2, and 11 is passed through the whole chain, and because Prime7 has no link in the chain after it, it forks a new process with 11 as the argument.

Prime2 should be consuming values as fast as they are being generated. When Prime 2 is calculating 20, Prime3 is calculating 19 and so on.(Optimization: Prime2 is largely unnecessary from an implementation view). When a new process PrimeN is created by process PrimeP, the parent creates 2 pipes, one for writing to the new process, and the other for reading from the new process.

Each non-terminal process node will then have a total of 4 pipes: two are leading to/from the successor node, and two are leading to/from the predecessor node. The unused end of each node can be closed, but this isn't strictly necessary. This algorithm is nice because the pipes block on read until a value can be read.

The numbers can be sent as binary data through pipes: read(parent, &value, sizeof(value)) to read data from the previous process and write(stdout, &value, sizeof(value)) to write data to the next link. When the last number is generated, the chain can be halted in several ways: A PoisonPill (e.g. The value 0 or some other invalid number) is passed down. This works nicely with blocks while reading from pipes.

A SIGTERM is sent to all of the processes in the Process Group. A SIGTERM is passed down the chain like the PoisonPill (probably not as efficient as 2). If the generator needs to collect all of the values then they could be sent back up the chain using the other set of pipes when a PoisonPill is sent; A second PoisonPill could also be used as some sort of control as well.

There are some issues with this: A lot of the action is handled by the first few primes. There is a lot of waiting. There is an upper limit to the number of real processes a system can handle.

If you are using something like Haskell, this isn't a problem, but it is for C. There are a lot of pipes being used. The second set of pipes for value return could be shmem, or a shared fifo, etc. Most machines have a small number of processors... Only a few can run at the same time.

One way around this is to cap the number of threads, but have each process manage several numbers.In this case, you would want to send a block array of numbers at once to each PrimeProcessor. The processor nulls out all of the non-prime values, and returns the remaining list (array) back to the parent. The parent then assigns these primes to the PrimeProcessor (the returned values are not guaranteed to be prime at this point; further analysis may have to be done) and a new batch of integers are sent out.To answer your question, sending a linked list doesn't make sense as far as I can tell.

You are going to send them down the chain one by one, or at the very least, as an array.

Either send indexes into the list instead of pointers, or squash the list into an array and send that instead. (Sounds like you're implementing the Sieve of Eratothenes, which works faster on arrays anyway. ).

If you are using pipes, you "delete" entries by not writing that entry to the pipe. So don't worry about deleting data from a list, just think in terms of whether you should write the entry you read from the input pipe to the output pipe.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions