访问共享内存段的进程返回不同的值

时间:2017-11-15 13:28:03

标签: c ipc shared-memory ipcs

我是IPCS概念的新手,我希望实现一个进程创建并初始化共享内存,然后调用附加到同一共享内存段的另一个进程并在共享内存中打印数据。 但是我无法实现它:

AlgoCommon.h

struct sub_algo_shm
{
int time;
int pno;
};
struct AlgoShm
{
int head;
int tail;
char fFlag;
struct sub_algo_shm algoshm;
};

AlgoThrottle.c

#define key (key_t)111119
#define SHM_SIZE 1024

    int main()
        {
                int shmid ;
                struct timeval CurTime;
                struct timespec nTime;
                struct AlgoShm *shmaddr,*ptr;

                ptr = (struct AlgoShm *)malloc(10*sizeof(struct AlgoShm));
                //Creating Shared Memory
                if((shmid = shmget(key,SHM_SIZE,0644 |IPC_CREAT)) < 0)
                {
                        perror("shmget:error");
                }
                else
                {
                        printf("Shm created key=[%d] \n Segment Id [%d] ",key,shmid);
                        //Attaching to Shared Memory
                        shmaddr = (struct AlgoShm *)shmat(shmid,0,0);
                        if(shmaddr < 0)
                        {
                                perror(" shmat :error");
                        }
                        else
                        {
                                printf(" SHM Segment attached at [%x] ",shmaddr);
                        }
                        //Loading/Initializing Data Blocks in SHM
                        clock_gettime(CLOCK_REALTIME,&nTime);
                        printf(" Time in N secs[%ld] \n ",nTime.tv_nsec);
                        ptr->head = 5;
                        ptr->tail = 3;
                        ptr->fFlag = '0';
                        ptr->algoshm.time = 0;
                        ptr->algoshm.pno = 1;
                        //memcpy(shmaddr,&ptr,sizeof(ptr));
                        printf(" AlgoThrottle|ptr->head [%d] ",ptr->head);
                //      sleep(1);
                        system("./AlgoRead"); 

                }

        return 0;
        }

AlgoRead.c

#define key (key_t)111119
#define SHM_SIZE 1024

    int main()
    {

    int shmid ;
    struct AlgoShm *shmaddr,*ptr1 ;
    ptr1 = (struct AlgoShm *)malloc(10*sizeof(struct AlgoShm));

    if((shmid = shmget(key,SHM_SIZE,0)) < 0)
    {

    perror(" AlgoRead|shmget:error ");
    }
    else
    {
            printf(" AlgoRead|SHM Created shmid [%d] ",shmid);

            if((shmaddr = (struct AlgoShm *)shmat(shmid,0,0)) < 0)
            {
                    perror("AlgoRead|shmat error");
            }
            else
            {
                    //memcpy(ptr1,shmaddr,sizeof(struct AlgoShm));
                    printf(" AlgoRead|Attached to Segment at Address[%x] \n ",shmaddr);
                    printf(" AlgoRead|ptr1->head [%d] ptr1->tail [%d] ptr1->fFlag[%c] \n ",ptr1->head,ptr1->tail,ptr1->fFlag);
            }
    }

    return 0;
    }

这是我的输出:

Shm created key=[111119]
 Segment Id [1179615678]  SHM Segment attached at [4a6b4000]  Time in N secs[114594083]
 AlgoRead|SHM Created shmid [1179615678]  AlgoRead|Attached to Segment at Address[624cb000]
  AlgoRead|ptr1->head [36810768] ptr1->tail [0] ptr1->fFlag[▒]
   AlgoThrottle|ptr->head [5]

此外,系统调用的输出首先显示printf语句,即使我在调用system()之前使用了sleep(1)

2 个答案:

答案 0 :(得分:1)

首先,如果您使用的是shared memory,那么为什么malloc()?共享内存本身会将shmid附加到shared memory,您不必显式地执行malloc()。

第二件事,您将shmaddr指针与shmat()返回的内存相关联,但将数据放入ptr ptr 是附加堆内存而不是共享内存。因此要么将数据放入shmaddr指针,要么将其替换为

shmaddr = (struct AlgoShm *)shmat(shmid,0,0);

ptr = (struct AlgoShm *)shmat(shmid,0,0);

并且不使用mallocc()分配内存。然后执行

                ptr->head = 5;
                ptr->tail = 3;
                ptr->fFlag = '0';
                ptr->algoshm.time = 0;
                ptr->algoshm.pno = 1;

在这两个过程中进行所有这些修改。最后你应该shmdt()取消附加共享内存,否则它将是memory leakage

我将您的代码修改为 one.c

int main()
{
        int shmid ;
        struct timeval CurTime;
        struct timespec nTime;
        struct AlgoShm *shmaddr,*ptr;
        if((shmid = shmget(key,SHM_SIZE,0644 |IPC_CREAT)) < 0)
        {
                perror("shmget:error");
        }
        else
        {
                printf("Shm created key=[%d] \n Segment Id [%d] ",key,shmid);
                ptr = (struct AlgoShm *)shmat(shmid,0,0);
                if(shmaddr < 0)
                {
                        perror(" shmat :error");
                }
                else
                {
                        printf(" SHM Segment attached at [%x] ",ptr);
                }
                clock_gettime(CLOCK_REALTIME,&nTime);
                printf(" Time in N secs[%ld] \n ",nTime.tv_nsec);
                ptr->head = 5;
                ptr->tail = 3;
                ptr->fFlag = 'a';
                ptr->algoshm.time = 10;
                ptr->algoshm.pno = 11;
                printf(" AlgoThrottle|ptr->head [%x] ",ptr->head);

        }
        system("./AlgoRead");
        return 0;
}

two.c

int main()
{
        int shmid ;
        struct AlgoShm *shmaddr,*ptr1 ;
        if((shmid = shmget(key,SHM_SIZE,0)) < 0)
        {

                perror(" AlgoRead|shmget:error ");
        }
        else
        {
                printf(" AlgoRead|SHM Created shmid [%d] ",shmid);

                if((ptr1 = (struct AlgoShm *)shmat(shmid,0,0)) < 0)
                {
                        perror("AlgoRead|shmat error");
                }
                else
                        printf(" AlgoRead|Attached to Segment at Address[%x] \n ",ptr1);
                printf(" AlgoRead|ptr1->head [%d] ptr1->tail [%d] ptr1->fFlag[%c] \n ",ptr1->head,ptr1->tail,ptr1->fFlag);
        }
        shmdt(ptr1);
        return 0;
}

我希望它可以帮助你,我的建议是通过shmget(),shmat()&amp;的手册页。 shmdt()正确。

答案 1 :(得分:1)

请在下方查看您的错误 为什么要分配10倍的结构?

正确的行

 ptr = (struct AlgoShm *)malloc(10*sizeof(struct AlgoShm));

于: -

ptr = (struct AlgoShm *)malloc(sizeof(struct AlgoShm));

在两个c文件上执行上述更改

现在在memcpy为什么要提供像ptr这样的指针&ptr的地址?只需删除&即可。这不是一个对象,因此您只需要提供指针ptr。纠正它如下

memcpy(shmaddr,ptr,sizeof(ptr));

而不是memcpy更好地使用,如下所示。

shmaddr->head = ptr->head;
shmaddr->tail = ptr->tail;
shmaddr->fFlag=ptr->fFlag;

我在下面重新编写了代码。试试吧。根据我的建议,更好地纠正自己的代码。

  

AlgoThrottle.c

int main()
{
    int shmid ;
    struct timeval CurTime;
    struct timespec nTime;
    struct AlgoShm *shmaddr,*ptr;

    ptr = (struct AlgoShm *)malloc(sizeof(struct AlgoShm));
    //Creating Shared Memory
    if((shmid = shmget(key,SHM_SIZE,0644 |IPC_CREAT)) < 0)
    {
        perror("shmget:error");
    }
    else
    {
        printf("Shm created key=[%d] \n Segment Id [%d] ",key,shmid);
        //Attaching to Shared Memory
        shmaddr = (struct AlgoShm *)shmat(shmid,0,0);
        if(shmaddr < 0)
        {
                perror(" shmat :error");
        }
        else
        {
                printf(" SHM Segment attached at [%x] ",shmaddr);
        }
        //Loading/Initializing Data Blocks in SHM
        clock_gettime(CLOCK_REALTIME,&nTime);
        printf(" Time in N secs[%ld] \n ",nTime.tv_nsec);
        ptr->head = 5;
        ptr->tail = 3;
        ptr->fFlag = '0';
        ptr->algoshm.time = 0;
        ptr->algoshm.pno = 1;
        //memcpy(shmaddr,ptr,sizeof(ptr));
        shmaddr->head = ptr->head;
        shmaddr->tail = ptr->tail;
        shmaddr->fFlag=ptr->fFlag;
        printf(" AlgoThrottle|ptr->head [%d] \n",ptr->head);
        //sleep(1);
        system("./AlgoRead"); 

    }

    return 0;
}
  

AlgoRead.c

int main()     {

    int shmid ;
    struct AlgoShm *shmaddr,*ptr1 ;
    ptr1 = (struct AlgoShm *)malloc(sizeof(struct AlgoShm));

    if((shmid = shmget(key,SHM_SIZE,0)) < 0)
    {

    perror(" AlgoRead|shmget:error ");
    }
    else
    {
            printf(" AlgoRead|SHM Created shmid [%d] ",shmid);

            if((shmaddr = (struct AlgoShm *)shmat(shmid,0,0)) < 0)
            {
                    perror("AlgoRead|shmat error");
            }
            else
            {
                    memcpy(ptr1,shmaddr,sizeof(struct AlgoShm));
                    printf(" AlgoRead|Attached to Segment at Address[%x] \n ",shmaddr);
                    printf(" AlgoRead|ptr1->head [%d] ptr1->tail [%d] ptr1->fFlag[%c] \n ",ptr1->head,ptr1->tail,ptr1->fFlag);
            }
    }

return 0;
}