如何在带有shmget的叉子上使用共享内存?

发布于 2025-01-30 18:24:54 字数 2818 浏览 1 评论 0原文

我正在研究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 技术交流群。

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

发布评论

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

评论(1

已下线请稍等 2025-02-06 18:24:54

您将覆盖指向共享内存的指针

result = &x;

当您现在进行结果指向x的私有内存,而不是共享内存段时,

。当孩子这样做时,孩子就会有相同的问题,

result = &y;

您无法通过将其地址分配给result将私有内存转变为共享内存。所以不要做这些任务。

只需使用*结果作为共享内存。您在一个过程中分配的任何内容都将在另一个过程中可见。

因此,

*result = x;

用于将X的值存储在共享内存中,并将

y = *result;

其检索。

You're overwriting the pointer to the shared memory when you do

result = &x;

Now result points to the private memory of x instead of the shared memory segment.

The child has the same problem when it does

result = &y;

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

*result = x;

to store the value of x in the shared memory, and

y = *result;

to retrieve it.

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