如何在带有shmget的叉子上使用共享内存?
我正在研究C上的共享内存,但是当我想更改子过程的价值时,我不明白如何使用共享记忆。我遇到了这个简单的代码,但是它不起作用,我会错过一些东西吗?
代码:
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <stdbool.h>
int create_shared_memory(){
return shmget(IPC_PRIVATE,sizeof(int),0644 | IPC_CREAT);
}
int get_shared_memory(int key){
return shmget(key,sizeof(int),0644 | IPC_CREAT);
}
int* attachd_shared_memory(int key){
return shmat(key,NULL,0);
}
int main(int argc, char const *argv[])
{
printf("\nSono il PID: %d di PID: %d\n",getpid(),getppid());
int sh_id = create_shared_memory();
int *result = attachd_shared_memory(sh_id);
int x = 5;
result = &x;
printf("Puntatore: %i Numero(x): %d\n",*result,x);
*result = 9;
printf("Puntatore: %i Numero(x): %d\n",*result,x);
if (!fork())
{
int sh2_id = get_shared_memory(sh_id);
int* result2 = attachd_shared_memory(sh2_id);
int y = 4;
result2 = &y;
printf("Sono il PID: %d di PID: %d \n",getpid(),getppid());
printf("ID: %d %d\n",sh_id,sh2_id);
printf("Puntatore: %i Numero(x): %d\n",*result2,x);
exit(0);
}
for (int i = 0; i < 2; i++)
{
printf("\nHo finito di aspettare:%d sono il padre: %d\n",wait(NULL),getppid());
}
shmctl(sh_id, IPC_RMID, NULL);
printf("Puntatore: %i Numero(x): %d \n",*result,x);
return 0;
}
感谢您的建议,我必须更好地阅读C的基本C,但这满足了我的要求。 解决方案:
int create_shared_memory(){
return shmget(IPC_PRIVATE,sizeof(int),0644 | IPC_CREAT);
}
int get_shared_memory(int key){
return shmget(key,sizeof(int),0644);
}
int* attachd_shared_memory(int key){
return shmat(key,NULL,0);
}
int main(int argc, char const *argv[])
{
printf("\nSono il PID: %d di PID: %d\n",getpid(),getppid());
int sh_id = create_shared_memory();
int *result = attachd_shared_memory(sh_id);
int x = 5;
*result = x;
printf("Puntatore: %i Numero(x): %d\n",*result,x);
*result = 9;
printf("Puntatore: %i Numero(x): %d\n",*result,x);
if (!fork())
{
*result = 4;
printf("Sono il PID: %d di PID: %d \n",getpid(),getppid());
exit(0);
}
for (int i = 0; i < 2; i++)
{
printf("\nHo finito di aspettare:%d sono il padre: %d\n",wait(NULL),getppid());
}
shmctl(sh_id, IPC_RMID, NULL);
printf("Puntatore: %i Numero(x): %d \n",*result,x);
return 0;
}
I'm study the shared memory on C but i don't understand how to use the share memory when i want to change the value on the child process. I worte this simple code but it doesn't work as i expexted, did i miss something?
CODE:
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <stdbool.h>
int create_shared_memory(){
return shmget(IPC_PRIVATE,sizeof(int),0644 | IPC_CREAT);
}
int get_shared_memory(int key){
return shmget(key,sizeof(int),0644 | IPC_CREAT);
}
int* attachd_shared_memory(int key){
return shmat(key,NULL,0);
}
int main(int argc, char const *argv[])
{
printf("\nSono il PID: %d di PID: %d\n",getpid(),getppid());
int sh_id = create_shared_memory();
int *result = attachd_shared_memory(sh_id);
int x = 5;
result = &x;
printf("Puntatore: %i Numero(x): %d\n",*result,x);
*result = 9;
printf("Puntatore: %i Numero(x): %d\n",*result,x);
if (!fork())
{
int sh2_id = get_shared_memory(sh_id);
int* result2 = attachd_shared_memory(sh2_id);
int y = 4;
result2 = &y;
printf("Sono il PID: %d di PID: %d \n",getpid(),getppid());
printf("ID: %d %d\n",sh_id,sh2_id);
printf("Puntatore: %i Numero(x): %d\n",*result2,x);
exit(0);
}
for (int i = 0; i < 2; i++)
{
printf("\nHo finito di aspettare:%d sono il padre: %d\n",wait(NULL),getppid());
}
shmctl(sh_id, IPC_RMID, NULL);
printf("Puntatore: %i Numero(x): %d \n",*result,x);
return 0;
}
Thank for the advice, i have to read better the basic of c but this satisfy my request.
SOLUTION:
int create_shared_memory(){
return shmget(IPC_PRIVATE,sizeof(int),0644 | IPC_CREAT);
}
int get_shared_memory(int key){
return shmget(key,sizeof(int),0644);
}
int* attachd_shared_memory(int key){
return shmat(key,NULL,0);
}
int main(int argc, char const *argv[])
{
printf("\nSono il PID: %d di PID: %d\n",getpid(),getppid());
int sh_id = create_shared_memory();
int *result = attachd_shared_memory(sh_id);
int x = 5;
*result = x;
printf("Puntatore: %i Numero(x): %d\n",*result,x);
*result = 9;
printf("Puntatore: %i Numero(x): %d\n",*result,x);
if (!fork())
{
*result = 4;
printf("Sono il PID: %d di PID: %d \n",getpid(),getppid());
exit(0);
}
for (int i = 0; i < 2; i++)
{
printf("\nHo finito di aspettare:%d sono il padre: %d\n",wait(NULL),getppid());
}
shmctl(sh_id, IPC_RMID, NULL);
printf("Puntatore: %i Numero(x): %d \n",*result,x);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将覆盖指向共享内存的指针
当您现在进行
结果
指向x
的私有内存,而不是共享内存段时,。当孩子这样做时,孩子就会有相同的问题,
您无法通过将其地址分配给
result
将私有内存转变为共享内存。所以不要做这些任务。只需使用
*结果
作为共享内存。您在一个过程中分配的任何内容都将在另一个过程中可见。因此,
用于将
X
的值存储在共享内存中,并将其检索。
You're overwriting the pointer to the shared memory when you do
Now
result
points to the private memory ofx
instead of the shared memory segment.The child has the same problem when it does
You can't turn private memory into shared memory by assigning its address to
result
. So don't do those assignments.Just use
*result
as the shared memory. Anything you assign to that in one process will be visible in the other.So use
to store the value of
x
in the shared memory, andto retrieve it.