发现很多文章都没有把pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1) 详细计算过程写出来,自己专门计算了一遍,附在文末。本例为man mmap中的原样实例程序,加上了一些额外的无关代码及打印输出信息 让总字节数超过一页大小sysconf(_SC_PAGE_SIZE)以方便查看调试信息
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char *argv[])
{
char *addr;
int fd;
struct stat sb;
off_t offset, pa_offset;
size_t length;
ssize_t s;
if (argc < 3 || argc > 4) {
fprintf(stderr, "%s file offset [length]\n", argv[0]);
exit(EXIT_FAILURE);
}
fd = open(argv[1], O_RDONLY);
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,
MAP_PRIVATE, fd, pa_offset);
if (addr == MAP_FAILED)
handle_error("mmap");
printf("length = %ld\n", length);
printf("offset = %ld\n", offset);
printf("pa_offset = %ld\n", pa_offset);
printf("length + offset - pa_offset = %ld\n", length + offset - pa_offset);
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);
}
//**额外增加的代码开始
//**加上了一些额外的无关代码及打印输出信息 让总字节数超过一页大小sysconf(_SC_PAGE_SIZE)以方便查看调试信息**开始
/*
#include "head.h"
struct msgbuff{
long mtype;
char mtext[512];
};
void send_msg(int qid, int type, char *m_msg, int size){
struct msgbuff msg;
msg.mtype = type;
strcpy(msg.mtext, m_msg);
if(msgsnd(qid, (void *)&msg, sizeof(msg.mtext), IPC_NOWAIT) == -1){
perror("msgsnd");
exit(1);
}
}
void get_msg(int qid, int type){
while (1){
struct msgbuff msg;
bzero(&msg, sizeof(msg));//清空缓冲区,也可用memmet实现
if (msgrcv(qid, (void *)&msg, sizeof(msg.mtext),type, MSG_NOERROR) == -1) {
perror("msgrcv");
exit(1);
}
printf("抢到了资源: <%d> <%s>\n", type, msg.mtext);
}
}
int main(int argc, char **argv){
int opt, mode = 2, msgtype = 1;
int msgq_id;
char mtext[512] = {0};
//mode == 1-> send
while((opt = getopt(argc, argv, "st:rm:")) != -1){
switch(opt){
case 's':
mode = 1;
break;
case 'r':
mode = 2;
break;
case 't':
msgtype = atoi(optarg);
break;
case 'm':
strcpy(mtext, optarg);
break;
default:
fprintf(stderr, "Usage: %s -[s|r] -t type -m msg\n", argv[0]);
exit(1);
}
}
//int msgget(key_t key, int msgflg);
if((msgq_id = msgget(2021, IPC_CREAT | 0666)) < 0){
perror("msgget");
exit(1);
}
if (mode == 1){
send_msg(msgq_id, msgtype, mtext, sizeof(mtext));
} else {
get_msg(msgq_id, msgtype);
}
return 0;
}
// ./a.out -s -t 1 -m "I'm the 1th!"
// ./a.out -r -t 1.msgq.c
// #include "head.h"
struct msgbuff{
long mtype;
char mtext[512];
};
void send_msg(int qid, int type, char *m_msg, int size){
struct msgbuff msg;
msg.mtype = type;
strcpy(msg.mtext, m_msg);
if(msgsnd(qid, (void *)&msg, sizeof(msg.mtext), IPC_NOWAIT) == -1){
perror("msgsnd");
exit(1);
}
}
void get_msg(int qid, int type){
while (1){
struct msgbuff msg;
bzero(&msg, sizeof(msg));//清空缓冲区,也可用memmet实现
if (msgrcv(qid, (void *)&msg, sizeof(msg.mtext),type, MSG_NOERROR) == -1) {
perror("msgrcv");
exit(1);
}
printf("抢到了资源: <%d> <%s>\n", type, msg.mtext);
}
}
int main(int argc, char **argv){
int opt, mode = 2, msgtype = 1;
int msgq_id;
char mtext[512] = {0};
//mode == 1-> send
while((opt = getopt(argc, argv, "st:rm:")) != -1){
switch(opt){
case 's':
mode = 1;
break;
case 'r':
mode = 2;
break;
case 't':
msgtype = atoi(optarg);
break;
case 'm':
strcpy(mtext, optarg);
break;
default:
fprintf(stderr, "Usage: %s -[s|r] -t type -m msg\n", argv[0]);
exit(1);
}
}
//int msgget(key_t key, int msgflg);
if((msgq_id = msgget(2021, IPC_CREAT | 0666)) < 0){
perror("msgget");
exit(1);
}
if (mode == 1){
send_msg(msgq_id, msgtype, mtext, sizeof(mtext));
} else {
get_msg(msgq_id, msgtype);
}
return 0;
}
// ./a.out -s -t 1 -m "I'm the 1th!"
// ./a.out -r -t 1.msgq.c#include "head.h"
struct msgbuff{
long mtype;
char mtext[512];
};
void send_msg(int qid, int type, char *m_msg, int size){
struct msgbuff msg;
msg.mtype = type;
strcpy(msg.mtext, m_msg);
if(msgsnd(qid, (void *)&msg, sizeof(msg.mtext), IPC_NOWAIT) == -1){
perror("msgsnd");
exit(1);
}
}
void get_msg(int qid, int type){
while (1){
struct msgbuff msg;
bzero(&msg, sizeof(msg));//清空缓冲区,也可用memmet实现
if (msgrcv(qid, (void *)&msg, sizeof(msg.mtext),type, MSG_NOERROR) == -1) {
perror("msgrcv");
exit(1);
}
printf("抢到了资源: <%d> <%s>\n", type, msg.mtext);
}
}
int main(int argc, char **argv){
int opt, mode = 2, msgtype = 1;
int msgq_id;
char mtext[512] = {0};
//mode == 1-> send
while((opt = getopt(argc, argv, "st:rm:")) != -1){
switch(opt){
case 's':
mode = 1;
break;
case 'r':
mode = 2;
break;
case 't':
msgtype = atoi(optarg);
break;
case 'm':
strcpy(mtext, optarg);
break;
default:
fprintf(stderr, "Usage: %s -[s|r] -t type -m msg\n", argv[0]);
exit(1);
}
}
//int msgget(key_t key, int msgflg);
if((msgq_id = msgget(2021, IPC_CREAT | 0666)) < 0){
perror("msgget");
exit(1);
}
if (mode == 1){
send_msg(msgq_id, msgtype, mtext, sizeof(mtext));
} else {
get_msg(msgq_id, msgtype);
}
return 0;
}
// ./a.out -s -t 1 -m "I'm the 1th!"
// ./a.out -r -t 1.msgq.c#include "head.h"
struct msgbuff{
long mtype;
char mtext[512];
};
void send_msg(int qid, int type, char *m_msg, int size){
struct msgbuff msg;
msg.mtype = type;
strcpy(msg.mtext, m_msg);
if(msgsnd(qid, (void *)&msg, sizeof(msg.mtext), IPC_NOWAIT) == -1){
perror("msgsnd");
exit(1);
}
}
void get_msg(int qid, int type){
while (1){
struct msgbuff msg;
bzero(&msg, sizeof(msg));//清空缓冲区,也可用memmet实现
if (msgrcv(qid, (void *)&msg, sizeof(msg.mtext),type, MSG_NOERROR) == -1) {
perror("msgrcv");
exit(1);
}
printf("抢到了资源: <%d> <%s>\n", type, msg.mtext);
}
}
int main(int argc, char **argv){
int opt, mode = 2, msgtype = 1;
int msgq_id;
char mtext[512] = {0};
//mode == 1-> send
while((opt = getopt(argc, argv, "st:rm:")) != -1){
switch(opt){
case 's':
mode = 1;
break;
case 'r':
mode = 2;
break;
case 't':
msgtype = atoi(optarg);
break;
case 'm':
strcpy(mtext, optarg);
break;
default:
fprintf(stderr, "Usage: %s -[s|r] -t type -m msg\n", argv[0]);
exit(1);
}
}
//int msgget(key_t key, int msgflg);
if((msgq_id = msgget(2021, IPC_CREAT | 0666)) < 0){
perror("msgget");
exit(1);
}
if (mode == 1){
send_msg(msgq_id, msgtype, mtext, sizeof(mtext));
} else {
get_msg(msgq_id, msgtype);
}
return 0;
}
// ./a.out -s -t 1 -m "I'm the 1th!"
// ./a.out -r -t 1.msgq.c#include "head.h"
struct msgbuff{
long mtype;
char mtext[512];
};
void send_msg(int qid, int type, char *m_msg, int size){
struct msgbuff msg;
msg.mtype = type;
strcpy(msg.mtext, m_msg);
if(msgsnd(qid, (void *)&msg, sizeof(msg.mtext), IPC_NOWAIT) == -1){
perror("msgsnd");
exit(1);
}
}
void get_msg(int qid, int type){
while (1){
struct msgbuff msg;
bzero(&msg, sizeof(msg));//清空缓冲区,也可用memmet实现
if (msgrcv(qid, (void *)&msg, sizeof(msg.mtext),type, MSG_NOERROR) == -1) {
perror("msgrcv");
exit(1);
}
printf("抢到了资源: <%d> <%s>\n", type, msg.mtext);
}
}
int main(int argc, char **argv){
int opt, mode = 2, msgtype = 1;
int msgq_id;
char mtext[512] = {0};
//mode == 1-> send
while((opt = getopt(argc, argv, "st:rm:")) != -1){
switch(opt){
case 's':
mode = 1;
break;
case 'r':
mode = 2;
break;
case 't':
msgtype = atoi(optarg);
break;
case 'm':
strcpy(mtext, optarg);
break;
default:
fprintf(stderr, "Usage: %s -[s|r] -t type -m msg\n", argv[0]);
exit(1);
}
}
//int msgget(key_t key, int msgflg);
if((msgq_id = msgget(2021, IPC_CREAT | 0666)) < 0){
perror("msgget");
exit(1);
}
if (mode == 1){
send_msg(msgq_id, msgtype, mtext, sizeof(mtext));
} else {
get_msg(msgq_id, msgtype);
}
return 0;
}
// ./a.out -s -t 1 -m "I'm the 1th!"
// ./a.out -r -t 1.msgq.c#include "head.h"
struct msgbuff{
long mtype;
char mtext[512];
};
void send_msg(int qid, int type, char *m_msg, int size){
struct msgbuff msg;
msg.mtype = type;
strcpy(msg.mtext, m_msg);
if(msgsnd(qid, (void *)&msg, sizeof(msg.mtext), IPC_NOWAIT) == -1){
perror("msgsnd");
exit(1);
}
}
void get_msg(int qid, int type){
while (1){
struct msgbuff msg;
bzero(&msg, sizeof(msg));//清空缓冲区,也可用memmet实现
if (msgrcv(qid, (void *)&msg, sizeof(msg.mtext),type, MSG_NOERROR) == -1) {
perror("msgrcv");
exit(1);
}
printf("抢到了资源: <%d> <%s>\n", type, msg.mtext);
}
}
int main(int argc, char **argv){
int opt, mode = 2, msgtype = 1;
int msgq_id;
char mtext[512] = {0};
//mode == 1-> send
while((opt = getopt(argc, argv, "st:rm:")) != -1){
switch(opt){
case 's':
mode = 1;
break;
case 'r':
mode = 2;
break;
case 't':
msgtype = atoi(optarg);
break;
case 'm':
strcpy(mtext, optarg);
break;
default:
fprintf(stderr, "Usage: %s -[s|r] -t type -m msg\n", argv[0]);
exit(1);
}
}
//int msgget(key_t key, int msgflg);
if((msgq_id = msgget(2021, IPC_CREAT | 0666)) < 0){
perror("msgget");
exit(1);
}
if (mode == 1){
send_msg(msgq_id, msgtype, mtext, sizeof(mtext));
} else {
get_msg(msgq_id, msgtype);
}
return 0;
}
// ./a.out -s -t 1 -m "I'm the 1th!"
// ./a.out -r -t 1.msgq.c
*/
//**额外增加的结束
//加上了一些额外的无关代码及打印输出信息 让总字节数超过一页大小sysconf(_SC_PAGE_SIZE)以方便查看调试信息** 结束
编译命令:
gcc mmap.c
程序执行格式: ./a.out file offset [length]
格式解析:./a.out +file 文件名 + offset偏移量 +length可选长度
./a.out mmap.c 10000
//执行结果,示例
length = 2266
offset = 10000
pa_offset = 8192
假设文件mmap.c文件大小为12266 ,
length =文件总大小(12266) - 偏移量offset(1000) = 2266
4096(sysconf(_SC_PAGE_SIZE)一页大小)
0001 0000 0000 0000 = 4096(sysconf(_SC_PAGE_SIZE)一页大小)
0000 1111 1111 1111 = (sysconf(_SC_PAGE_SIZE) - 1) =4095
~(0000 1111 1111 1111)
= 1111 0000 0000 0000 = ~ (sysconf(_SC_PAGE_SIZE) - 1) =61440
pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1)
=10000 & 61440
=(0010 0111 0001 0000) & (1111 0000 0000 0000)
=(0010 0000 0000 0000)
=8192 //pa_offset即内存中的页偏移量