问题标签 [shared-memory]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
2174 浏览

c - How do I write to a different thread's stack in C/C++?

I know this is a bad idea! Certainly for safe programming the stack for a given thread should be considered private to that thread. But POSIX at least guarantees that all of a thread's memory is shared and writeable by other threads, which means the stack of one thread can (in theory) be written to by another thread. So I'm curious how one could do that.

The only way I can think of would be to pass the address of a local variable in thread B to thread A and have A start writing to that general area. But this doesn't fully emulate function calls in C. In particular I'm curious if it's possible to say, have thread A sleep while its program counter is set to the first line of some function that takes parameters, and then on thread B actually push those parameters onto the stack, then resume thread A and have it execute as if the function were originally called in thread A with those parameters. I think this requires thread B to be able to access thread A's registers under at least x86 calling conventions, and I'm not sure that's possible.

0 投票
2 回答
5509 浏览

c - 在 C 中创建访问共享内存

所以我有一个问题,我真的不知道该怎么做。我希望也许你能让我知道如何处理它。

我需要在共享内存中分配 N 个缓冲区。每个缓冲区应初始化为 0。然后我必须分叉 N/2 个子进程。

然后每个孩子(i)将值(i)写入缓冲区(i),休眠一秒钟。然后读取当前缓冲区的值,如果值同时更改,则显示一条消息。然后孩子移动 i 位置 N/2 次。


有什么想法应该怎么做?

  • 我可以创建一个包含 N 个整数的数组吗?由于所有孩子都将访问相同的元素,这会是一个问题吗?
0 投票
4 回答
19727 浏览

c - 如何在 C 中的共享内存中保留一个 int 和一个数组?

我正在尝试编写一个程序,其中子进程在 Linux 上相互通信。

这些进程都是从同一个程序创建的,因此它们共享代码。

我需要他们访问两个整数变量以及一个整数数组。

我不知道共享内存是如何工作的,我搜索过的每一个资源都没有做任何事情,只会让我感到困惑。

任何帮助将不胜感激!

编辑:这是我迄今为止编写的一些代码示例,只是为了共享一个 int,但它可能是错误的。

0 投票
4 回答
47033 浏览

python - 在 Python 多处理中将 Pool.map 与共享内存数组结合起来

我有一个非常大的(只读)数据数组,希望由多个进程并行处理。

我喜欢这个Pool.map函数,并想用它来并行计算该数据上的函数。

我看到可以使用ValueorArray类在进程之间使用共享内存数据。但是当我尝试使用它时,我会RuntimeError: 'SynchronizedString objects should only be shared between processes through inheritance在使用 Pool.map 函数时得到一个:

这是我正在尝试做的一个简化示例:

谁能告诉我我在这里做错了什么?

所以我想做的是在进程池中创建新创建的共享内存分配数组后将它们传递给进程。

0 投票
2 回答
4669 浏览

c - 消息队列。msgsend msgrcv。C (Linux) 中的 System V IPC 系统调用

嗨,我正在使用一些共享内存,其中不同的进程读取和写入数据。我正在使用消息队列来存储数据在读取和写入操作之间发生更改时的消息。

因此,在读写和检查更改时,进程以下列方式存储消息

这工作正常。哦,顺便说一下,这是我创建消息队列的方式。

所以我的问题是我不太确定如何从队列中检索消息并将它们显示在屏幕上。我一直在阅读msgrcv()系统调用,但对我来说不是很清楚。

rc是一个,int因为msgrcv()返回一个int。我如何将int其指向实际消息?如何从消息中读取内容以便显示它们?我假设这应该在某种循环中完成。

0 投票
1 回答
2007 浏览

c - IPC 消息队列。msgrcv 系统调用。系统五、如何跳出循环

我有一个消息队列,我从中循环获取消息。问题是我不知道如何退出循环。msgrcv 返回类型 size_t 所以它继续。我应该比较返回值什么值才能退出循环?

0 投票
4 回答
1578 浏览

php - PHP 从共享内存中检索时是否复制变量?

如果我运行 shm_get_var(),它会返回一个“引用”,将数据保存在共享内存中吗?

我想在共享内存中保留一个大小约为 50MB 的数组,以便多个进程可以使用它,而不必保留这个 50MB 数组的多个副本。如果共享内存不是答案,还有人有其他想法吗?

0 投票
4 回答
519 浏览

linux - 有没有办法确定 free() 是否会失败?

有没有办法确定如果free()在某个内存块指针上调用是否会失败?

我有以下情况:有权访问共享资源的线程失败,而可能处于释放所述资源的状态。现在我需要设计一种安全的方法来清理这个共享资源。

当然,我已经为正常情况分配了资源的所有权,但是上述限制情况呢?

更新:如果我使用额外的同步机制,它只会做更多的清理工作,并且可能涉及额外的限制条件。如果可能,我想限制/避免这些。

解决方案:我最终决定执行重构。感谢所有贡献者。你们真棒!

0 投票
5 回答
11303 浏览

c++ - 一个用户崩溃时共享内存中的互斥锁?

假设一个进程正在共享内存中创建一个互斥锁并锁定它并在互斥锁被锁定时转储核心。

现在在另一个进程中,我如何检测互斥锁已被锁定但不属于任何进程?

0 投票
2 回答
245 浏览

shared-memory - 将数据从数据库表加载到共享内存

关于将数据从数据库加载到共享内存的任何想法,这个想法是为了加快从常用表中检索数据的速度?