problem with shared memory

problem with shared memory

Post by Davide Celott » Fri, 30 Apr 1999 04:00:00



Hi to all,

I'm now  facing with use of shared memory.
I have a segmentation fault when I try to execute my program
This is the piece of code:

 typedef struct
       {
           char name_doc[30];
           char temp_doc[30];
           char  type_doc[10];
            int  cod_doc;
            int  byte_doc;
           }doc_access;

typedef struct
      {
      doc_access WWWdoc[500];
      }WWWDOC;

WWWDOC  *wwwdoc;

if ((shmid_2 = shmget(SHMID_SUMMARY_2,sizeof(wwwdoc) ,IPC_CREAT | 0644))
== -1)

        {
        perror("shmget");
   exit(1);
        }

     wwwdoc = (WWWDOC *)shmat(shmid_2, NULL, 0);

The problem is when I initialize the structures:

         strcpy(wwwdoc->WWWdoc[i].name_doc,name_doc);

         strcpy(wwwdoc->WWWdoc[i].temp_doc,temp_doc);

         strcpy(wwwdoc->WWWdoc[i].type_doc,type_doc);

          wwwdoc->WWWdoc[i].cod_doc=cod_doc;

          wwwdoc->WWWdoc[i].byte_doc=byte_doc;
          i++;

When  i becomes equal to 51, the program crash with the message SEGMENTATION
FAULT
Is the problem the size of the shared memory or something else?

Any help would be appreciated.
Tanks to all in advance.
Davide

______________________________________________

Davide Celotto
CEFRIEL
via Fucini 2
20133 Milano
Phone: 02/23954.248

______________________________________________

 
 
 

problem with shared memory

Post by Pierre-Michel Anse » Fri, 30 Apr 1999 04:00:00



> Hi to all,

> I'm now  facing with use of shared memory.
> I have a segmentation fault when I try to execute my program
> This is the piece of code:

>  typedef struct
>        {
>            char name_doc[30];
>            char temp_doc[30];
>            char  type_doc[10];
>             int  cod_doc;
>             int  byte_doc;
>            }doc_access;

> typedef struct
>       {
>       doc_access WWWdoc[500];
>       }WWWDOC;

> WWWDOC  *wwwdoc;

> if ((shmid_2 = shmget(SHMID_SUMMARY_2,sizeof(wwwdoc) ,IPC_CREAT | 0644))
> == -1)

wwwdoc is a pointer, so sizeof(wwwdoc) will probably return 4 (bytes) which
is the size of an int (it depends of the system and I don't know yours).
In fact, You have to put the size of the structure :

if ((shmid_2 = shmget(SHMID_SUMMARY_2,sizeof(WWWDOC) ,IPC_CREAT | 0644))
== -1)

and it should work better.

- Show quoted text -

Quote:

>         {
>         perror("shmget");
>    exit(1);
>         }

>      wwwdoc = (WWWDOC *)shmat(shmid_2, NULL, 0);

> The problem is when I initialize the structures:

>          strcpy(wwwdoc->WWWdoc[i].name_doc,name_doc);

>          strcpy(wwwdoc->WWWdoc[i].temp_doc,temp_doc);

>          strcpy(wwwdoc->WWWdoc[i].type_doc,type_doc);

>           wwwdoc->WWWdoc[i].cod_doc=cod_doc;

>           wwwdoc->WWWdoc[i].byte_doc=byte_doc;
>           i++;

> When  i becomes equal to 51, the program crash with the message SEGMENTATION
> FAULT
> Is the problem the size of the shared memory or something else?

Yes, it is a memory allocation bug.

> Any help would be appreciated.
> Tanks to all in advance.
> Davide

> ______________________________________________

> Davide Celotto
> CEFRIEL
> via Fucini 2
> 20133 Milano
> Phone: 02/23954.248

> ______________________________________________

Pierre-Michel Ansel

 
 
 

problem with shared memory

Post by John Simpso » Fri, 30 Apr 1999 04:00:00


 >
 > Hi to all,
 >
 > I'm now  facing with use of shared memory.
 > I have a segmentation fault when I try to execute my program
 > This is the piece of code:
 >
 >  typedef struct
 >        {
 >            char name_doc[30];
 >            char temp_doc[30];
 >            char  type_doc[10];
 >             int  cod_doc;
 >             int  byte_doc;
 >            }doc_access;
 >
 > typedef struct
 >       {
 >       doc_access WWWdoc[500];
 >       }WWWDOC;
 >
 > WWWDOC  *wwwdoc;
 >
 > if ((shmid_2 = shmget(SHMID_SUMMARY_2,sizeof(wwwdoc) ,IPC_CREAT |
0644))
 > == -1)
 >
 >         {
 >         perror("shmget");
 >    exit(1);
 >         }
 >
 >      wwwdoc = (WWWDOC *)shmat(shmid_2, NULL, 0);
 >
 > The problem is when I initialize the structures:
 >
 >          strcpy(wwwdoc->WWWdoc[i].name_doc,name_doc);
 >
 >          strcpy(wwwdoc->WWWdoc[i].temp_doc,temp_doc);
 >
 >          strcpy(wwwdoc->WWWdoc[i].type_doc,type_doc);
 >
 >           wwwdoc->WWWdoc[i].cod_doc=cod_doc;
 >
 >           wwwdoc->WWWdoc[i].byte_doc=byte_doc;
 >           i++;
 >
 > When  i becomes equal to 51, the program crash with the message
SEGMENTATION
 > FAULT
 > Is the problem the size of the shared memory or something else?
 >
 > Any help would be appreciated.
 > Tanks to all in advance.
 > Davide
 >
 > ______________________________________________
 >
 > Davide Celotto
 > CEFRIEL
 > via Fucini 2
 > 20133 Milano
 > Phone: 02/23954.248

 > ______________________________________________

It is the size of the shared memory.  You are asking for a segment the
size of the pointer, sizeof(wwwdoc).  You need your shmget to reference
sizeof(*wwwdoc).

--
John H. Simpson Phone: 503-450-2667  FAX: 503-450-3629
CNF AdTech Center. (CNF Transportation, Inc.)
1717 NW 21st St.
Portland, OREGON 97209 USA
for e-mail, remove _not_oj_ from address

 
 
 

problem with shared memory

Post by Larry Blanchar » Fri, 30 Apr 1999 04:00:00



> Hi to all,

> I'm now  facing with use of shared memory.
> I have a segmentation fault when I try to execute my program
> This is the piece of code:

>  typedef struct
>        {
>            char name_doc[30];
>            char temp_doc[30];
>            char  type_doc[10];
>             int  cod_doc;
>             int  byte_doc;
>            }doc_access;

> typedef struct
>       {
>       doc_access WWWdoc[500];
>       }WWWDOC;

Others have already found the bug for you (sizeof in the shmget call).
I'm just curious why you found it desirable to encapsulate the array
WWWdoc inside the structure WWWDOC.  Wouldn't it be simpler just to use
the array?
--
Larry Blanchard - Old roses, old motorcycles, and old trains
Homo Sapiens is a goal, not a description.
 
 
 

problem with shared memory

Post by e » Mon, 03 May 1999 04:00:00


have u checked the SHMMAX parameter that limits the maximum shared memory
blocks that a process can hold ? this ( name may change ) is configured to
ur kernel!
if u check the configuration parameters ull find it somewhere,

or u should check ur programs' memory leaks!
good chance
Tansel OKAY


> Hi to all,

> I'm now  facing with use of shared memory.
> I have a segmentation fault when I try to execute my program
> This is the piece of code:

>  typedef struct
>        {
>            char name_doc[30];
>            char temp_doc[30];
>            char  type_doc[10];
>             int  cod_doc;
>             int  byte_doc;
>            }doc_access;

> typedef struct
>       {
>       doc_access WWWdoc[500];
>       }WWWDOC;

> WWWDOC  *wwwdoc;

> if ((shmid_2 = shmget(SHMID_SUMMARY_2,sizeof(wwwdoc) ,IPC_CREAT | 0644))
> == -1)

>         {
>         perror("shmget");
>    exit(1);
>         }

>      wwwdoc = (WWWDOC *)shmat(shmid_2, NULL, 0);

> The problem is when I initialize the structures:

>          strcpy(wwwdoc->WWWdoc[i].name_doc,name_doc);

>          strcpy(wwwdoc->WWWdoc[i].temp_doc,temp_doc);

>          strcpy(wwwdoc->WWWdoc[i].type_doc,type_doc);

>           wwwdoc->WWWdoc[i].cod_doc=cod_doc;

>           wwwdoc->WWWdoc[i].byte_doc=byte_doc;
>           i++;

> When  i becomes equal to 51, the program crash with the message
SEGMENTATION
> FAULT
> Is the problem the size of the shared memory or something else?

> Any help would be appreciated.
> Tanks to all in advance.
> Davide

> ______________________________________________

> Davide Celotto
> CEFRIEL
> via Fucini 2
> 20133 Milano
> Phone: 02/23954.248

> ______________________________________________

 
 
 

problem with shared memory

Post by Anders Melchiorse » Mon, 03 May 1999 04:00:00



> or u should check ur programs' memory leaks!

Memory leaks will never give segmentation faults.

A memory leak is simply memory that is left unused instead of being
freed so it could be reused for different data.
--
Regards, Anders                         If a Microsoft product
(address is valid)                      fails, who do you sue?