Thread: shmget function

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    40

    shmget function

    I am having problems with the shmget function. The code compiles correctly, but I get the error "shmget: Invalid argument" when I execute it. I am compiling on a Sparcstation 20 running solaris 2.6 and using the sun workshop c compiler v4.2.
    Does anyone have any idea why this is failing?

    I include the code for my small program below:

    /*
    * shm_server.c - Program to test passing information between
    * processes using shared memory
    *
    * Creates the string and shared memory portion.
    */


    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <stdio.h>

    /* #define SHMSZ 27 */

    main()
    {
    char c;
    int shmid;
    int msgflg = IPC_CREAT | 0666;
    key_t key;
    size_t size;
    char *shm;
    char *s;

    /*
    * We will name our shared memory segment
    * "5678".
    */
    key = 5678;
    size = 27;

    /*
    * Create the segment.
    */
    printf("shmget( %i, %i, %o)\n", key, size, msgflg);
    /* shmid = shmget(key, SHMSZ, msgflg); */
    shmid = shmget(key , size, IPC_CREAT | 0666);
    if ( shmid < 0 )
    {
    perror("shmget");
    exit(1);
    }

    /*
    * Now we attach the segment to our data space.
    */
    if ( (shm = shmat(shmid, NULL, 0)) == (char *) -1 )
    {
    perror("shmat");
    exit(1);
    }

    /*
    * Now we put some things into the memory for the
    * other process to read.
    */
    s = shm;

    for (c = 'a'; c <= 'z'; c++)
    *s++ = c;
    *s = NULL;

    /*
    * Finally, we wait until the other process
    * changes the first character of our memory
    * to '*', indicating that it has read what
    * we put there.
    */
    while (*shm != '*')
    sleep(1);

    exit(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    According to the manual page I'm reading

    EINVAL
    The size argument is less than the system-imposed minimum or greater than the system-imposed maximum.
    EINVAL
    A shared memory identifier exists for key but the size of the segment associated with it is less than size and size is not equal to 0.

    First guess - you're asking for too little - read the docs to find out what the minimum is.

    > while (*shm != '*')
    > sleep(1);
    You might want to make shm a volatile pointer, if you're expecting the outside world to change this value when you're not looking.

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Type ipcs on the shell prompt and check if another form of IPC (semaphore, message queue or shared memory) with the same key (5678) exists. If it exists, remove it and execute your program again.

    And your program works fine here, i'm using DIGITAL 4.0

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Can you create any shared memory using a private key?
    Code:
    shmid = shmget(IPC_PRIVATE , size, IPC_CREAT | 0660);

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    40
    Thank you all,
    Your replies were all helpful. Changing the size value from 27 to 512, which according to my research is the size of a block of shared memory on a sun box, was the winner. I may have a play with different sizings to see if it will go smaller. Salem also suggested making the pointer a volatile one, which when I found out what volatile meant, is a very good idea and the example I am following should have used it. In the final program it will not be necessary because the other processes will have read only access to the memory area.
    If I may I would like to also get your opinions on where I am going with this. I want to write a new process that will once a day read in a file consisting of two columns of numbers. The process will take the file and create an AVL tree in shared memory. This will be available 24x7. An existing process will then be amended to attach to this area on startup and do a lookup on the first number and pull back the second number.
    What do you think?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Sounds OK so far
    The only real trouble is the daily replace one tree with another, where you'll need to be extra careful you don't remove the old tree whilst another process is using it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM