问题描述
是否有一种方法可以在这些程序中(使用共享内存概念)打印相同的物理地址,而不是打印不同的逻辑地址?
Is there is a way to print the same physical address in these programs (while using the shared memory concept) rather than printing different logical addresses?
我打印相同物理地址的原因:...
The reason for me to print the same physical address :...
/*这是可选的,因为我提供了很多非核心信息*/
/*It's optional to read this, since I have provided a lot of information which is not to the core */
在我的实验室中,我有两个程序:一个通过共享内存概念将字符串存储在物理内存中,另一个通过访问共享内存打印相同的字符串.
In my lab, I have two programs: one to store a string in a physical memory via shared memory concept and one to print the same string via accessing the shared memory.
程序1:
#include<sys/types.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
main()
{
key_t key;
int shmid;
char* addr1;
key = ftok("/home/tamil/myc/pws.c",'T');
shmid = shmget(key,128*1024,IPC_CREAT|SHM_R|SHM_W);
addr1 = shmat(shmid,0,0);
printf("\nIPC SHARED MEMORY");
printf("\n SENDER ADDRESS");
printf("\nTHE ADDRESS IS %p",addr1);
printf("\nENTER THE MESSAGE:");
scanf("%s",addr1);
printf("\nMESSAGE STORED IN %p IS %s",addr1,addr1);
}
程序2:
#include<sys/types.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
main()
{
int shmid;
char* addr1;
key_t key;
key = ftok("/home/tamil/myc/pws.c",'T');
shmid = shmget(key,128*1024,SHM_R|SHM_W);
addr1 = shmat(shmid,0,0);
printf("\nIPC SHARED MEMORY");
printf("\n SENDER ADDRESS");
printf("\nTHE ADDRESSS IS %p",addr1);
printf("\nMESSAGE STORED IN %p IS %s",addr1,addr1);
}
输出:
tamil@ubuntu:~/myc$ cc shmget.c
tamil@ubuntu:~/myc$ ./a.out
IPC SHARED MEMORY
SENDER ADDRESS
THE ADDRESS IS 0xb786c000
ENTER THE MESSAGE:helloworld
MESSAGE STORED IN 0xb786c000 IS helloworld
tamil@ubuntu:~/myc$ cc shmget2.c
tamil@ubuntu:~/myc$ ./a.out
IPC SHARED MEMORY
SENDER ADDRESS
THE ADDRESSS IS 0xb7706000
MESSAGE STORED IN 0xb7706000 IS helloworld
tamil@ubuntu:~/myc$
这些程序在这里打印2个不同的逻辑地址.但是(满足大学教授的要求)是否可以打印相同的物理地址?请帮忙.
Here these programs are printing the 2 different logical address. But (to satisfy the college professor) is there is a way to print the same physical address? Please help..
推荐答案
- 您的课程很可能已经已经完全按照您的教授要求的去做.
- 您完全不了解物理地址和虚拟地址的概念.在使用虚拟内存的任何操作系统上,常规应用程序(相对于OS本身)根本不知道任何物理地址.
- Your programs are most likely already doing exactly what your professor asked you to do.
- You are totally not understanding the concept of physical vs. virtual addresses. On any operating system that uses virtual memory, a regular application (as opposed to the OS itself) can not know any physical addresses at all.
这篇关于在C程序中打印相同的物理地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!