You attach to the same memid
twice:
if ((memid=shmget(0x1234L,sizeof(mezua),0600|IPC_CREAT))<0){
perror("shmget error");
exit(-1);
}
if((nptr2=(char*)shmat(memid,0,0))==(char*)-1){
perror("shmat error");
exit(-1);
}
//Estatistiken memoria sortu eta atzitu
if ((memid2=shmget(0x12345L,sizeof(stats),0600|IPC_CREAT))<0){
perror("shmget error");
exit(-1);
}
printf("%d\n",memid);
if((nptr=(struct estadistikak*)shmat(memid,0,0))==(struct estadistikak*)-1){
perror("shmat error");
exit(-1);
}
The second should be:
if((nptr=(struct estadistikak*)shmat(memid2,0,0))==(struct estadistikak*)-1){
perror("shmat error");
exit(-1);
}
Having said that, it seems cock-eyed to have nptr2
attached to memid
and nptr
attached to memid2
. I'd go with consistent suffixes:
nptr1 memid1
nptr2 memid2
Hence:
if ((memid1 = shmget(0x1234L, sizeof(mezua), 0600|IPC_CREAT)) < 0)
err_exit("shmget 1");
if ((nptr1 = (char*)shmat(memid1, 0, 0)) == (char*)-1)
err_exit("shmat 1");
if ((memid2 = shmget(0x12345L, sizeof(stats), 0600|IPC_CREAT)) < 0)
err_exit("shmget 2");
if ((nptr2 = (struct estadistikak*)shmat(memid2, 0, 0)) == (struct estadistikak*)-1)
err_exit("shmat 2");
And err_exit()
might be as simple as:
void err_exit(const char *msg)
{
perror(msg);
exit(-1);
}
Though I usually use a more complex variable-arguments, printf
-like function.