LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   not able to read key using shmctl function (https://www.linuxquestions.org/questions/linux-newbie-8/not-able-to-read-key-using-shmctl-function-762919/)

hjc 10-19-2009 05:26 AM

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;

}


neonsignal 10-19-2009 10:21 PM

You need to use buf.shm_perm.__key. The ipc_perm you were using has been obsoleted, the new one is in 'include/bits/ipc.h'.

hjc 10-20-2009 01:16 PM

thanks it works..
Quote:

Originally Posted by neonsignal (Post 3725554)
You need to use buf.shm_perm.__key. The ipc_perm you were using has been obsoleted, the new one is in 'include/bits/ipc.h'.



All times are GMT -5. The time now is 11:06 AM.