0

This is the struct:

typedef struct Queue {
  int data[MAX_SIZE];
  long front;
  long back;
  int size;
  int mysize;
} Queue;

and this is the code:

if((queueShmid = shmget(IPC_PRIVATE, sizeof(Queue), IPC_CREAT | IPC_EXCL ))==-1) {
  printf("Shared memory segment exists - opening as client\n");
  /* Segment probably already exists - try as a client*/
  if((queueShmid = shmget(IPC_PRIVATE, 2*sizeof(Queue), 0)) == -1)
  { perror(" bad shmget"); exit(1);}
}
Queue *queue = (Queue*) shmat(queueShmid, NULL, 0);
if(queue=NULL) {
  perror("bad shmat"); exit(1);
}

if ((int) queue < 0) {
  printf("shmat() failed %d \n",(int)queue); exit(1);
}
printf ("shared memory attached at address %p\n", queue);

its return: shared memory attached at address (nil),
any other shared memory work well, any idea?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
daniel
  • 3
  • 1
  • 1
    `if ((int) queue < 0)`, `%d` with `(int)queue`? isn't that smell foul? why not use simple `%p`? – Sourav Ghosh Dec 31 '14 at 19:10
  • 1
    I assume `if(queue=NULL)` should be `if(queue==NULL)`? – wolfPack88 Dec 31 '14 at 19:16
  • 1
    Don't cast the result of functions that return `void*`. http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Barmar Dec 31 '14 at 19:19
  • Is `if(queue=NULL)` in the real code or a copying error? If it's the real code, this question should be closed as due to a typo. – Barmar Dec 31 '14 at 19:20
  • if(queue=NULL) was mistake and i fixed it, however now i get:shmat() failed -1 – daniel Dec 31 '14 at 20:02
  • @daniel so it fails on `if ((int) queue < 0)`. My PC has 4gb memory so a 32 bit pointer won't necessarily be positive when cast to `int`. Although you haven't posted what `shmat()` does. – Weather Vane Dec 31 '14 at 20:20
  • shmat() attches to the proscess memory the shared memory created by OS,the action return pointer to the shared memory part,the address that always i get with the Queue is 0xFFFFFFFFFFFFFFFF – daniel Dec 31 '14 at 20:38
  • regarding this line: 'if ((int) queue < 0) {' Since 'queue' is a pointer, it could be pointing to an area in the upper half of memory. In which case casting it to 'int' will result in a number that is < 0. – user3629249 Dec 31 '14 at 20:51
  • even when i ingore: if ((int) queue < 0) { printf("shmat() failed %d \n",(int)queue); exit(1); } and then do: queue->front=0;or any other operation with queue i get segmention fault – daniel Dec 31 '14 at 21:00
  • @daniel said "even when i ingore *[queue == -1]* and then do: queue->front=0; or any other operation with queue i get segmention fault". Yes of course you do. What is a returned pointer of `-1` trying to tell you? – Weather Vane Dec 31 '14 at 21:08

1 Answers1

0
my comments are prefixed with '// <--'

suggest OP read the documentation on system calls, especially the returned values

 typedef struct Queue
 {
    int data[MAX_SIZE];
    long front;
    long back;
    int size;
    int mysize;
} Queue;



if((queueShmid = shmget(IPC_PRIVATE, sizeof(Queue), IPC_CREAT | IPC_EXCL ))==-1)
{
    printf("Shared memory segment exists - opening as client\n");

    // <-- should verify errno is EEXIST (Segment exists, cannot create)
    // <-- rather than making any assumptions

    /* Segment probably already exists - try as a client*/

    // <-- it was a single queue instance above so why is it 2 times that size below???
    if((queueShmid = shmget(IPC_PRIVATE, 2*sizeof(Queue), 0)) == -1)
    {
        //perror(" bad shmget");
        perror( "shmget failed for 2 times size of queue" );
        exit(1);
    } // end if
} // end if

Queue *queue=(Queue*)shmat(queueShmid, NULL, 0);

//if(queue=NULL) // <-- placing the literal on the left would have enabled the compiler to catch this error
if(queue==NULL)  // <-- on error, shmat returns (void*)-1 (not NULL) and sets errno
{
    perror( "shmat for ptr to instance of queue failed" );
    //perror("bad shmat");
    exit(1);
}

if ((int) queue < 0)
{
    printf("shmat() failed %d \n",(int)queue); exit(1);
}

printf ("shared memory attached at address %p\n", queue);
user3629249
  • 16,402
  • 1
  • 16
  • 17