not able to read key using shmctl function
i want to access the key value in struct ipc_perm using the shmctl function.
shmctl_return =shmctl(segment_id,IPC_STAT,&buf)
when i try to print contents of buf.shm_perm.key i get a message saying key is not defined in ipc_perm structure. can anybody help?
Code:
#include<stdlib.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<sys/ipc.h>
int main()
{
char * shared_memory;
int segment_id;
struct shmid_ds buf;
struct ipc_perm shm_perm;
int shmctl_return;
int size = 2048;
key_t key;
key = ftok("/home2/hrodriques/prog.c", 'R');
printf("key=%d\n",key);
segment_id= shmget(key,size,IPC_CREAT|S_IRUSR|S_IWUSR); //allocate a //sharedmemorysegment
printf("SEGMENT ID = %d\n",segment_id);
//Attach the shared-memory segment to its address //space.
shared_memory= (char *)shmat(segment_id,NULL,0);//atttach a shared //memorysegment
if((shmctl_return =shmctl(segment_id,IPC_STAT,&buf))==-1)
printf("error");
else{
printf("success");
//printf("Size=%d\n",buf.shm_segsz);
printf("Key=%d\n", buf.shm_perm.key);
} //Detach and remove the shared-memory //segment
shmdt(shared_memory);
shmctl(segment_id,IPC_RMID,NULL);
return 0;
}
|