shmctl() problem

hi

i've been toying around, started reading some books, and came upon a problem.
i'm trying to write a c program that will output some data about some shared memory segment, given that segment's numeric designation.
here's the code (without checkign and unneccesary parts...)

#include <stdio.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <ctype.h>

int main(){
    int segment_id = 456456;    /* let's asume i know the ID of the shared mem. segment    */
    struct shmid_ds buffer;

    /* dovuci podatke o segmentu*/
    if(shmctl(segment_id, IPC_STAT, &buffer) != 0){
        printf("shit\n");
        exit(-1);
    }

    printf("owner: %d\n", buffer.shm_perm.uid); /* ok*/
    printf("size: %d \n", buffer.shm_segsz); /* NOT OK */
    printf("# of attaches: %d \n", buffer.shm_nattch); /* NOT OK */

    return 0;
}

Now, 2 questions:

  1. why do i get segmentation fault every time i run the prog? it runs it course, but always ends up with a segmentation fault
  2. when i try to access sub-structure shm_perm, all data is ok, but when i try to access any members of the shmid_ds structure, i get out garbage... why?
    thank you
    Shoki

buffer.shm_segsz is of type size_t which may not be an int (%d) on your particular platform.

True, but I don't see how treating a long as an int in a printf() statement can cause a SEGV.

If the code posted does SEGV, something else is going on.

Also, to go along with your statement about shm_segsz, shm_nattach is a long int on my OpenSolaris box.

thanks for your input guys
it's not the format issue, printdf is smarter than that.

personally deep down i dislike linux for some reason, so i'll just get up from my lazy ass and install ether some kind of solaris or bsd.

thanks
shoki

I have tried your program on both AIX and HPUX and have no problems with your code...I only changed the shm_id to match what exists on those systems. It could just be the compiler you are using. What compiler type and version are you using. Get the latest version and try your program again and see what happens.