在 ac 程序中打印相同的物理地址
有没有办法在这些程序中打印相同的物理地址(同时使用共享内存概念)而不是打印不同的逻辑地址?
我打印相同物理地址的原因:...
/*阅读此内容是可选的,因为我提供了很多不是核心的信息*/
在我的实验室中,我有两个程序:一个用于存储通过共享内存概念在物理内存中生成一个字符串,并通过访问共享内存来打印相同的字符串。
程序 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 个不同的逻辑地址。但是(为了让大学教授满意)有没有办法打印相同的物理地址?请帮忙..
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.
Program 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);
}
Program 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);
}
Output:
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$
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)