Thread: Shared memory
View Single Post
  #1 (permalink)  
Old 18-Jun-2009, 12:23
-Tantalus- -Tantalus- is offline
Puzzled Penguin
 
Join Date: May 2009
Posts: 1
-Tantalus- hasn't been rated much yet
Default Shared memory

Hello,

I've got a question about shared memory under Linux. When I allocate e.g. 20 Bytes of shm with shmget()&shmat(), I'm able to write up to ~5200 Bytes of data until it crashes. (with symbol lookup error?!)
When I write ~5900 Byte, I get a segmentation fault.

Under Solaris 10 I'm able to write up to PAGE_SIZE until I get a segmentation fault. (and no symbol lookup errors)
`getconf PAGE_SIZE` results in 4k on my Linux machine.

Well, my question is: How many Bytes are allocated at once of the OS? 4k or 8k or something like that would IMHO make sense for me, but these ~5500 Bytes are quiet strange.
And why are there symbol lookup errors? (and no, this isn't my homework and yeah I'm aware that this is a silly question )

I tested this with the following program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/shm.h>

int main(int argc, char* argv[])
{
    int shmid;
    char* message;
    long limit = 0;
    long real = 0;

    /* Parameter handling */
    if (argc == 3) {
	real = atol(argv[1]);
	limit = atol(argv[2]);
    } else {
	printf("Usage: %s <size of shared memory> <nr of bytes to write>\n", argv[0]);
	exit(1);
    }

    if ((limit <= 0) || (real <= 0)) {
	printf("Please specify a number above 0\n");
	exit(1);
    }

    /* Allocate and attach "real"-Bytes of shared memory */
    shmid = shmget(42, (size_t) real, IPC_CREAT | 0640);
    if (shmid == -1) {
	perror("Couldn't create shared memory");
	exit(errno);
    }

    message = (char*) shmat(shmid, NULL, 0);
    if (message == NULL) {
	shmctl(shmid, IPC_RMID, 0);
	perror("Couldn't attach shared memory");
	exit(errno);
    }

    /* Write "limit"-Bytes to shared memory */
    memset(message, '\0', (size_t) limit);
    printf("%ld Bytes successfully written to shared memory\n", limit);

    /* Detach and free shared memory */
    if (shmdt((void*) message) == -1) {
	perror("Couldn't detach shared memory");
	exit(errno);
    }

    if (shmctl(shmid, IPC_RMID, 0) == -1) {
	perror("Couldn't remove shared memory");
	exit(errno);
    }

    return EXIT_SUCCESS;
}
With the following output:
Code:
> ./producer 20 4096
4096 Bytes successfully written to shared memory

> ./producer 20 5100
5100 Bytes successfully written to shared memory

> ./producer 20 5300
./producer: symbol lookup error: ./producer: undefined symbol: printf, version GLIBC_2.0

> ./producer 20 5900
Speicherzugriffsfehler
(segmentation fault)
Reply With Quote