Go Back   openSUSE Forums > Programming/Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read

Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 18-Jun-2009, 12:23
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
  #2 (permalink)  
Old 18-Jun-2009, 16:15
Wise Penguin
 
Join Date: Sep 2008
Location: Dubai
Posts: 1,534
syampillai is a reputation jewel in the roughsyampillai is a reputation jewel in the roughsyampillai is a reputation jewel in the roughsyampillai is a reputation jewel in the roughsyampillai is a reputation jewel in the rough
Default Re: Shared memory

You are not supposed to write anything beyond the allocated space anyway. If you do the result can be catastrophic.
__________________
openSUSE 11.2 (x86_64) with Kernel 2.6.31.12-0.1-desktop and KDE 4.3.5 (Release 3) on MacBook Pro
Latest MS Windows version used: Win95
Reply With Quote
  #3 (permalink)  
Old 18-Jun-2009, 16:23
Flux Capacitor Penguin
 
Join Date: Jun 2008
Location: GMT+10
Posts: 6,088
ken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputation
Default Re: Shared memory

This is what specification writers call an undefined situation. The shmem routines have satisfied the minimum specifications. If you go beyond the limits, it may blow up on you now, blow up on you later, or do nothing. But since you ask, probably something to do with the minimum page size and the amount of memory beyond that you can overwrite without noticing anything.
Reply With Quote
  #4 (permalink)  
Old 19-Jun-2009, 20:23
Student Penguin
 
Join Date: Apr 2009
Posts: 62
fbsduser hasn't been rated much yet
Default Re: Shared memory

./shm.o 20 9393 = success
./shm.o 20 9394 = segfault
Reply With Quote
  #5 (permalink)  
Old 20-Jun-2009, 08:32
Monex's Avatar
Busy Penguin
 
Join Date: Jun 2008
Location: Germany
Posts: 269
Monex hasn't been rated much yet
Default Re: Shared memory

Hi,

I've already written data with a size of some MBs in the past, and I know a lot of programs which writes a lot more, maybe you should have a look at Advanced Linux Programming chapter 5 which describes shared memory usage. Sometimes it is necessary to increase the maximum shared memory size (look at /proc/sys/kernel/shmmax).

Maybe you also want to use a more high level interface for your IPC task look at Chapter*9.*Boost.Interprocess or if you want to use Qt Qt 4.5: QSharedMemory Class Reference

Hope this helps
Reply With Quote
Reply

Bookmarks


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.2