Results 1 to 2 of 2
Hi,
I have a small board that has a static ram board attached to it. I have two different programs that each write to a byte of memory in the ...
- 07-09-2010 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 11
O_SYNC parameter in open command
Hi,
I have a small board that has a static ram board attached to it. I have two different programs that each write to a byte of memory in the static ram board.
I get a file descriptor to the device memory with the following code:
int phymemfd;
phymemfd=open("dev/mem, O_RDWR | O_SYNC);
This opens the physical memory for both reading and writing. Next I get a pointer to the beginning of my ram board.
volatile unsigned char *novram;
novram = (unsigned char *) mmap(0, getpagesize() * 31,
PROT_READ|PROT_WRITE, MAP_SHARED, phymemfd, 0x11AA0000);
I do this same code in two processes that are both running and this works fine.
Now, if I take out the O_SYNC parameter, one of the programs gets into some sort of weird state.
My understanding of the O_SYNC parameter is that it cause the process accessing the memory location to block, not allow another process to run, until the first process has finished writing to it.
I can see that if my program doesn't block, I wouldn't really know what was in the memory location, but would I can't see how it would cause any other type of system problem.
Does this all make sense to anyone?
Thanks in advance,
mhuman1
- 07-12-2010 #2
Your understanding is partly right. When the write blocks, it doesn't mean that no other process can run (what an easy way to kill a system...). It means that it does not return to execute the rest of your program. This is done for as long as it takes for the driver to finish writing to the physical media.
Even though your media is just RAM, the driver obeys the usual filesystem semantics, and caches the data written to it. Once the cache is flushed to the real end destination, your program regains execution.
If you have multiple processes accessing shared memory, you need to use some form of Interprocess Communication (IPC) to synchronize access to the memory by the different processes. My usual recommendation for a tutorial on the subject of IPC is
Beej's Guide to Unix Interprocess Communication
--- rod.Stuff happens. Then stays happened.


Reply With Quote
