update
Signed-off-by: Wenchao Hao <haowenchao2@huawei.com>
This commit is contained in:
parent
3bd8ea3958
commit
a8b58e1918
|
|
@ -0,0 +1,76 @@
|
|||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#define handle_error(msg) \
|
||||
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int fd;
|
||||
char *addr;
|
||||
off_t offset, pa_offset;
|
||||
size_t length;
|
||||
ssize_t s;
|
||||
struct stat sb;
|
||||
|
||||
if (argc < 3 || argc > 4) {
|
||||
fprintf(stderr, "%s file offset [length]\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
fd = open(argv[1], O_RDWR);
|
||||
if (fd == -1)
|
||||
handle_error("open");
|
||||
|
||||
if (fstat(fd, &sb) == -1) /* To obtain file size */
|
||||
handle_error("fstat");
|
||||
|
||||
offset = atoi(argv[2]);
|
||||
pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);
|
||||
/* offset for mmap() must be page aligned */
|
||||
|
||||
if (offset >= sb.st_size) {
|
||||
fprintf(stderr, "offset is past end of file\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (argc == 4) {
|
||||
length = atoi(argv[3]);
|
||||
if (offset + length > sb.st_size)
|
||||
length = sb.st_size - offset;
|
||||
/* Can't display bytes past end of file */
|
||||
|
||||
} else { /* No length arg ==> display to end of file */
|
||||
length = sb.st_size - offset;
|
||||
}
|
||||
|
||||
addr = mmap(NULL, length + offset - pa_offset, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, fd, pa_offset);
|
||||
if (addr == MAP_FAILED)
|
||||
handle_error("mmap");
|
||||
|
||||
printf("address is 0x%x\n", (unsigned long)addr);
|
||||
|
||||
//write(fd, "", 1);
|
||||
memcpy(addr, "haowenchao", 11);
|
||||
msync(addr, sb.st_size, MS_SYNC);
|
||||
|
||||
s = write(STDOUT_FILENO, addr + offset - pa_offset, length);
|
||||
if (s != length) {
|
||||
if (s == -1)
|
||||
handle_error("write");
|
||||
|
||||
fprintf(stderr, "partial write");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
munmap(addr, length + offset - pa_offset);
|
||||
close(fd);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int fd = open(argv[1], O_RDWR | O_CREAT, (mode_t)0600);
|
||||
const char *text = "hello";
|
||||
size_t textsize = strlen(text) + 1;
|
||||
char *map;
|
||||
|
||||
lseek(fd, textsize-1, SEEK_SET);
|
||||
write(fd, "", 1);
|
||||
|
||||
map = mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
printf("address is 0x%x\n", (unsigned long)map);
|
||||
|
||||
memcpy(map, text, strlen(text));
|
||||
|
||||
//while(1);
|
||||
|
||||
msync(map, textsize, MS_SYNC);
|
||||
munmap(map, textsize);
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue