I need to write in embedded Linux (2.6.37) the incoming DMA buffers in the HD partition as raw device / dev / sda1 as quickly as possible. Buffers are aligned as needed and have an equal length of 512 KB. The process can continue for a very long time and fill as much as, for example, 256 GB of data. I need to use memory mapping file technology (O_DIRECT is not applicable), but cannot figure out how to do this. So, in the pseudocode the "normal" entry:
fd=open(/dev/sda1",O_WRONLY);
while(1) {
p = GetVirtualPointerToNewBuffer();
if (InputStopped())
break;
write(fd, p, BLOCK512KB);
}
Now I will be very grateful for a similar example of pseudo / real code, how to use the memory mapping technique for this entry.
UPDATE2: Thanks to kestasx, the last working test code looks like this:
#define TSIZE (64*KB)
void* TBuf;
int main(int argc, char **argv) {
int fdi=open("input.dat", O_RDONLY);
int fdo=open("output.dat", O_RDWR);
int i, offs=0;
void* addr;
i = posix_memalign(&TBuf, TSIZE, TSIZE);
if ((fdo < 1) || (fdi < 1)) {
printf("Error in files\n");
return -1; }
while(1) {
addr = mmap((void*)TBuf, TSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fdo, offs);
if ((unsigned int)addr == 0xFFFFFFFFUL) {
printf("Error MMAP=%d, %s\n", errno, strerror(errno));
return -1; }
i = read(fdi, TBuf, TSIZE);
if (i != TSIZE) {
printf("End of data\n");
return 0; }
i = munmap(addr, TSIZE);
offs += TSIZE;
sleep(1);
};
}
Update3:
1. DMA, read() mmp(), DMA , , . , :
while(1) {
read(fdi, TBuf, TSIZE);
addr = mmap((void*)TBuf, TSIZE, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, fdo, offs);
munmap(addr, TSIZE);
offs += TSIZE; }
(!) - read() BAD ADDRESS TBuf.
, , munmap() msync(). .
, - unmapping addr TBuf?
2. DMA. , read() - , , DMA .
, , (!).
, Linux , () .
, read() - , .
, - Linux, , , !
!