在 ac 程序中打印相同的物理地址

发布于 2024-10-21 21:06:36 字数 1770 浏览 1 评论 0原文

有没有办法在这些程序中打印相同的物理地址(同时使用共享内存概念)而不是打印不同的逻辑地址?

我打印相同物理地址的原因:...

/*阅读此内容是可选的,因为我提供了很多不是核心的信息*/

在我的实验室中,我有两个程序:一个用于存储通过共享内存概念在物理内存中生成一个字符串,并通过访问共享内存来打印相同的字符串。

程序 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2024-10-28 21:06:36
  • 您的程序很可能已经完全按照教授要求您做的事情进行。
  • 您完全不理解物理地址与虚拟地址的概念。在任何使用虚拟内存的操作系统上,常规应用程序(而不是操作系统本身)根本无法知道任何物理地址。
  • 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文