Thread: How to LIMIT share memory in C ???

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    8

    Post How to LIMIT share memory in C ???

    With 2 c files, they use 1 memory area. How to set limit memory for them ???
    I use library shm.h and its func (shmget, shmat, shmctl ... ). When i share memory for another, i don't know how to limit the memory ???

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    8
    Size of segment :
    Code:
    #define SHMSZ 27
    Code:
       if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) 
       {
            perror("shmget");
            exit(1);
        }
    When SHMSZ > 27.
    Code:
    shmget: Invalid argument
    Why ?

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's implementation-specific. Have you looked at the man page?

    On my system it says:
    Code:
           SHMMAX     Maximum size in bytes for a shared memory  seg&#173;
                      ment:  implementation dependent (currently 4M).
    
           SHMMIN     Minimum size in bytes for a shared memory  seg&#173;
                      ment:  implementation  dependent  (currently  1
                      byte, though PAGE_SIZE is the effective minimum
                      size).
    Is this your first shmget() call with that key?
    Code:
           EINVAL      if  a new segment was to be created and size <
                       SHMMIN or size > SHMMAX, or no new segment was
                       to  be  created,  a  segment  with  given  key
                       existed, but size is greater than the size  of
                       that segment.
    Last edited by itsme86; 09-06-2006 at 10:25 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    8
    I use FreeBSD 6.0
    The man page do not show that.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Maybe not, but it does say this:
    When creating a new shared memory segment, size indicates the desired
    size of the new segment in bytes. The size of the segment may be rounded
    up to a multiple convenient to the kernel (i.e., the page size).
    And this:
    ERRORS
    The shmget() system call will fail if:

    [EINVAL]
    Size specified is greater than the size of the previ-
    ously existing segment. Size specified is less than
    the system imposed minimum, or greater than the system
    imposed maximum.
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    8
    Thanks so much.
    Because I use it but not remove it after run. So I change the seg size and compile, it occurs error.
    I have solved it.

    Can I write one struct to share memory ??
    Code:
    struct test
    {
        int a;
        int b;
    }
    I insert it 10 times in loop. And then insert 1 time with this memory. It replace the first record in share memory ??? How can I add it in the end of the share memory ???
    Code:
    MEM Before :  {0,0} {1,1} {2,2} {3,3} {4,4} {5,5} {6,6} {7,7} {8,8} {9,9} 
    After insert {10, 10}
    MEM after   :  {10,10} {1,1} {2,2} {3,3} {4,4} {5,5} {6,6} {7,7} {8,8} {9,9} 
    
    Can add the end of shared memory to have this result : {0,0} {1,1} {2,2} {3,3} {4,4} {5,5} {6,6} {7,7} {8,8} {9,9} {10,10}  ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM