Shared memory! Any idea on how I can make the child and parent processes take turns?

Hi guys so i'm writing a shared memory program and currently, I can create the shared memory, but I want the parent to write to the shared memory, and then I want the child to read from that shared memory, then the parent write to the shared memory and the child again read from it.

The shared memory is only going to be 32 bytes big, and i'm gareteened the fiels i'm reading in wont' be bigger than 32 bytes.

Currently my program is able to write 1 line of the file to shared memory then the other process is able to read that line, and write it to a file.

But the issue comes is now I want to read a whole file, sending 1 line of the file at a time.

Is there a way for me to tell the reading process to wait for the other process to write its line to the shared memory, and then tell the writing process, that yes the child has read that line of shared memory? So the writing process knows when to write the next line to shared memory?

I could use sch_yeild() but the problem is, I want them to take turns, not 1 process to completely write the file, and when its done writing the file, tell the other process to start reading, because if thats the case the read process will only see the last line of the file because the writing process is only sending 1 line at a time to the shared memory as there is only 32 bytes of storage .

To properly take turns, you need a synchronizatin object. sch_yield() or sleep() operations will absolutely not guarantee that YOUR other process gets to run - it's just telling the OS that "I don't need to run for a bit, so go do some other process", but it's not saying "Wait here until X is done". To do that, you need a pair of semaphores or other "waitable objects" that you can use to wait for the other side to finish what it needs to do.

--
Mats

Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.

Can you think of a better way of sharing a file through shared memory? Could I perhaps increase the shared memory size and send the whole file and then have the reading process just read the whole file from shared memory? I'm not familiar with semaphores or waitable objects so I'm not sure if going that path will cause more issues for me at this time.

Can you think of a better way of sharing a file through shared memory? Could I perhaps increase the shared memory size and send the whole file and then have the reading process just read the whole file from shared memory? I'm not familiar with semaphores or waitable objects so I'm not sure if going that path will cause more issues for me at this time.

Assuming the file-size isn't ginormous, then yes, you could (try) to allocate shared memory that is big enough to share the entire file. The crux of the matter comes if the file-size is larger than the available (virtual) memory - then you won't be able to create your shared memory, or you will have to go back to a process that loads parts of the file at a time.

--
Mats

Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.

In your situation a pipe feels like the better IPC primitive than shared memory, but whatever.

You can use a simple token passing mechanism. There is one logical token. Initially the parent process has it. When it has written a line, it passes the token to the child process. The child reads the line, and passes the token back.
There are many ways to implement such a mechanism, but the simplest is probably message queues. Another is condition variables, but since you only get a pthreads implementation within a single process, you'd have to find a library or write your own. (Boost.Interprocess has one. It also has a nice C++ wrapper for MQs.)

All the buzzt! CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law