[英]Processes accessing a Shared Memory segment returning different values
我是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;
}
算法阅读器
#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]
此外,即使在调用system()之前我使用sleep(1)之后,系统调用的输出也将首先显示,然后显示printf语句
第一件事,如果您使用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()和shmdt()的手册页。
请在下面查看您的错误,为什么要分配该结构的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;
}
算法阅读器
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.