打印共享内存的物理地址空间

发布于 2024-10-20 21:37:04 字数 2061 浏览 4 评论 0原文

在 C 程序中,我有两个程序,一个程序将字符串存储在共享内存中,另一个程序是通过访问共享内存来打印相同的字符串。

program 1->

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  
I am having the same text body in program 2 . **BUT IT IS PRINTING THE LOGICAL ADDRESS. HENCE IN THE OUTPUT IAM GETTING 2 DIFFERENT LOGICAL ADRESS. BUT INSTEAD OF THAT IF I WANT TO HAVE SAME PHYSICAL ADDRESS(TO SATISFY THE COLLEGE PROFFESSOR)HOW CAN I DO.** 
PLEASE HELP....

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 helloworldtamil@ubuntu:~/myc$ cc shmget2.c
    tamil@ubuntu:~/myc$ ./a.out

    IPC SHARED MEMORY
     SENDER ADDRESS
    **THE ADDRESSS IS **0xb7706000****
    MESSAGE STORED IN **0xb7706000** IS helloworldtamil@ubuntu:~/myc$ 

In C program I got 2 programs one to store a string in a shared memory and the other program is to print the same string via accessing the shared memory.

program 1->

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  
I am having the same text body in program 2 . **BUT IT IS PRINTING THE LOGICAL ADDRESS. HENCE IN THE OUTPUT IAM GETTING 2 DIFFERENT LOGICAL ADRESS. BUT INSTEAD OF THAT IF I WANT TO HAVE SAME PHYSICAL ADDRESS(TO SATISFY THE COLLEGE PROFFESSOR)HOW CAN I DO.** 
PLEASE HELP....

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 helloworldtamil@ubuntu:~/myc$ cc shmget2.c
    tamil@ubuntu:~/myc$ ./a.out

    IPC SHARED MEMORY
     SENDER ADDRESS
    **THE ADDRESSS IS **0xb7706000****
    MESSAGE STORED IN **0xb7706000** IS helloworldtamil@ubuntu:~/myc$ 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无名指的心愿 2024-10-27 21:37:04

在 Linux 上转储 2 MB 共享内存。

dd if=/dev/shm/stuff_here of=myshm.dmp bs=1024 count=2048

可能必须先在 fstab 中安装它。

Dumping 2 MB of shared mem on Linux.

dd if=/dev/shm/stuff_here of=myshm.dmp bs=1024 count=2048

Might have to mount it first in fstab.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文