- 論壇徽章:
- 0
|
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
int main()
{
int fd = 0;
size_t len = 0;
void* abc = NULL;
size_t empLen = 0;
const char* buf = "abcdefghij";
int i = 0;
while(i < 100)
{
fd = open("./b.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
ftruncate(fd, len += 1024);
abc = mmap(NULL, 1024, PROT_WRITE | PROT_READ, MAP_SHARED, fd, len-1024);
if(abc == NULL)
{
printf("error");
break;
}
empLen = 1024;
while(1)
{
if(10 <= empLen)
{
memcpy((char*)abc + 1024 - empLen, buf, 10);
empLen -= 10;
}
else
{
memcpy((char*)abc + 1024 - empLen, buf, empLen);
empLen = 0;
break;
}
}
msync(abc, 1024, MS_SYNC);
munmap(abc, 1024);
close(fd);
i += 1;
}
if(abc != NULL)
{
munmap(abc, 1024);
}
close(fd);
}
今天好像代碼功能不能用,大家?guī)兔纯催@段程序,那個地方出了問題?
產(chǎn)生”段錯誤“發(fā)生在紅色的那一行
[ 本帖最后由 onmeiei 于 2009-9-11 17:39 編輯 ] |
|