- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
<分区>
在共享内存中,据我了解,使用 shmat()
调用在附加到它的两个进程之间共享相同的逻辑地址。那么为什么我为下面的程序获得不同的内存地址(在输出中),即使它们共享相同的地址。
//Shm_Server.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXSIZE 27
void die(char *s)
{
perror(s);
exit(1);
}
int main()
{
char c;
int shmid;
key_t key = 5678;
char *shm_addr, *s;
if ((shmid = shmget(key, MAXSIZE, IPC_CREAT | 0666)) < 0)
die("shmget");
if ((shm_addr = (char *)shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
printf("\nServer shm_addr = %x\n",shm_addr);
s = shm_addr;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
while (*shm_addr != '*')
sleep(1);
if((shmctl(shmid, IPC_RMID, 0)) < 0)
die("shmctl");
exit(0);
}
//Shm_Client.C
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXSIZE 27
void die(char *s)
{
perror(s);
exit(1);
}
int main()
{
int shmid;
key_t key = 5678;
char *shm_addr, *s;
if ((shmid = shmget(key, 0, 0666)) < 0)
die("shmget");
if ((shm_addr = (char *) shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
printf("\nClient shm_addr = %x\n", shm_addr);
//reading what the server put in shared memory
for (s = shm_addr; *s != '\0'; s++)
putchar(*s);
putchar('\n');
//Writing in shared memory
*shm_addr = '*';
exit(0);
}
Output:
[xyz@xyz:Shm_ex] $ ./Shm_Server &
[1] 19489
[xyz@xyz:Shm_ex] $
Server shm_addr = d92b5000
./Shm_Client
Client shm_addr = eb3c4000
abcdefghijklmnopqrstuvwxyz
[xyz@xyz:Shm_ex] $
因此在这段代码中 puts 无法显示输出。 如果我删除 fgets 行,它会打印 lola 但如果我尝试在 shm 上读写,则不会发生任何事情。我该如何解决这个问题? #include #inclu
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有两个进程,每个进程都可以从 shmget() 获取相同的 shmid。 在这两个过程中,shmat()给出了不同的内存地址。 每个进程的地址总是相同的: Process1: 0x41b31000
当我浏览 shmat 的手册页时。它被描述为 API 的原始功能是将与 shmid 关联的内存段附加到调用进程的地址空间。 我的问题如下。 attach 这个词对我来说很普通。我发现很难理解附加所指的
我在运行代码时遇到问题。我的 shmat 失败并打印权限被拒绝。我在谷歌上搜索了如何解决它,但我不能。我的代码如下: #include #include #include #include #
我正在绞尽脑汁地想弄清楚为什么这在我的 Minix 系统上不起作用。 当我尝试编译 C 程序时,出现以下错误: #make link pm/pm program.o: In funct
当我到达routerInfo->areaID行时,我在该部分遇到了段错误:11。看来我没有成功分配内存。我只是不知道为什么。谁能解决这个问题吗? 我的标题是这样的: #include #includ
根据要求包含所有 header 。下面的这段代码可以工作,但问题在于 shmat(seg_id,NULL,0)。第二个参数中的 NULL 意味着操作系统将处理该位置代表用户。但是我们可以给出我们自己的
当尝试执行 shmat 时出现分配错误,告诉我无法访问内存,这在我之前没有发生过,我真的不知道该怎么做。这是错误: 0xffffffffffffffff error: Cannot access me
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
这个问题在这里已经有了答案: shmat() is returning a different "shmaddr" for same "shmkey" (3 个答案) 关闭 8 年前。 在共享内存中
使用mmap(),进程必须从父进程继承映射才能共享内存。有没有办法在不共享父进程的进程之间共享内存? shmat() 似乎是最好的解决方案,但如果进程在退出/死机时没有分离内存,则需要进行清理。 域套
我通过虚拟地址使用 shmat()。 第一个进程调用一个 shmat,shmaddr 为空,当它获得数据 block 的虚拟地址时,它存储在另一个共享内存位置。 第二个进程使用第一个进程存储在共享内存
我试图写入共享内存,但出于某种原因,在我调用 shmat() 和 strcpy 后出现段错误(核心已转储),这是为什么? 这是我的代码: int fd,shmid; key_t shmkey; cha
//TeamSize is an integer int Seg_id = shmget(SHM_KEY,sizeof(Word)*TeamSize,IPC_CREAT); void* Seg_p
shmat(int id , void * addr,int flg) 的第二个参数被推荐/约定为 NULL。 但是如果我想给它一个特定的地址(void* addr),该地址应该来自堆栈还是堆? 我的
我的程序有 2 个线程,我使用共享内存在两者之间进行通信。 每个线程都有一个“init”方法。在 init 方法中,我调用“shmget”和“shmat”来获取共享内存段并附加到局部变量。部分代码如下
在我的简单代码中: #include #include int main() { key_t key = ftok(".", 'b'); int shmid = shmget(ke
在生产者流程中,我有以下内容。 说 MEMKEY 为 key_t 234500,并且 *shared_ring 是指向正在共享的数据结构 (donut_ring) 的指针。 int shmid, se
我需要一些有关此代码的帮助。 我在共享段中创建了一个struct msg_t,并在另一个段中创建了一个简单的字符串。在此方法中,首先附加两个段,然后要初始化字符串。然后我尝试将指向该字符串的指针(在第
我是一名优秀的程序员,十分优秀!