473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Question about struct in shared memory (C on linux)

Hi!

I have two struts like that:

struct {
int num;
int num2;
struct b arrayOfB[SIZE];
} a;

struct {
int num3;
int num4
} b;

I put struct a into Shared Memory:
shmid = shmget(IPC_PRIVATE, sizeof(struct b), 0666)

and atach it in two process:
ptr = shmat(shmid, 0, 0);

This works fine, but now i must delete the constant SIZE:

I will obtain the SIZE in runtime, before execute the shmget function.

the new struct a:

struct {
int num;
int num2;
struct b *arrayOfB;
} a;

now:

I put struct a into Shared Memory:
shmid = shmget(IPC_PRIVATE, sizeof(struct a) + sizeof(struct b) *
size, 0666);

and atach it in two process:
ptr = shmat(shmid, 0, 0);

my question is: how can tell arrayOfB to point over the shared memory?

i thought:

struct a *ptr,p2*;
p2 = ptr; //ptr is the shmat return
p2++;
ptr->arrayOfB

but doesn't work. in another way i read about the shmat parametrs, but
don't understand how (if its possible) use to solve my trouble.

Thanks.
Jul 3 '08 #1
4 9767
On Jul 3, 12:29*pm, "hugo.arre...@gmail.com" <hugo.arre...@gmail.com>
wrote:
Hi!

I have two struts like that:

struct {
int num;
int num2;
struct b arrayOfB[SIZE];

} a;

struct {
int num3;
int num4

} b;

I put struct a into Shared Memory:
shmid = shmget(IPC_PRIVATE, sizeof(struct b), 0666)

and atach it in two process:
ptr = shmat(shmid, 0, 0);

This works fine, but now i must delete the constant SIZE:

I will obtain the SIZE in runtime, before execute the shmget function.

the new struct a:

struct {
int num;
int num2;
struct b *arrayOfB;

} a;

now:

I put struct a into Shared Memory:
shmid = shmget(IPC_PRIVATE, sizeof(struct a) + sizeof(struct b) *
size, 0666);

and atach it in two process:
ptr = shmat(shmid, 0, 0);

my question is: how can tell arrayOfB to point over the shared memory?

i thought:

struct a *ptr,p2*;
p2 = ptr; //ptr is the shmat return
p2++;
ptr->arrayOfB

but doesn't work. in another way i read about the shmat parametrs, but
don't understand how (if its possible) use to solve my trouble.

Thanks.
Forgetting the details of shared memory (which you would get better
advice in comp.unix.programmer, btw, as it is not standard C), you
could consider using the struct hack to solve your problem. See, e.g.
http://c-faq.com/struct/structhack.html

-David
Jul 3 '08 #2
Forgetting the details of shared memory (which you would get better
advice in comp.unix.programmer, btw, as it is not standard C), you
could consider using the struct hack to solve your problem. See, e.g.http://c-faq.com/struct/structhack.html

-David
Thanks you David, but my problem is how to do that in shared memory, i
will try in comp.unix.programmer.

Thanks again.
Jul 3 '08 #3
On Jul 3, 2:52*pm, "hugo.arre...@gmail.com" <hugo.arre...@gmail.com>
wrote:
Forgetting the details of shared memory (which you would get better
advice in comp.unix.programmer, btw, as it is not standard C), you
could consider using the struct hack to solve your problem. *See, e.g..http://c-faq.com/struct/structhack.html
-David

Thanks you David, but my problem is how to do that in shared memory, i
will try in comp.unix.programmer.

Thanks again.
The first option in the faq site should work as well with shared
memory as elsewhere as far as I know. i.e. put in your structure
this:

struct b arrayOfB[1];

But allocate enough space for the structure AND the desired number of
"b" elements, then use it. This avoids the problem of having a
pointer to another block of memory in the struct...

-David
Jul 3 '08 #4
On Thu, 3 Jul 2008 09:29:37 -0700 (PDT), "hu**********@gmail.com"
<hu**********@gmail.comwrote:
struct {
int num;
int num2;
struct b arrayOfB[SIZE];
} a;

struct {
int num3;
int num4
} b;
<snip shared-memory>
Aside: you used sizeof(struct b) where you needed (struct a).

The shared-memory part is offtopic as it is not standard in C; but
issues of laying-out and accessing data in arbitrary memory, whether
it happened to come from shm* or say malloc (a_zillion), is ontopic.
At least if the memory is suitably aligned in the first place, and
both <offtopicshmat and <ontopicmalloc are (for all types).
[now] I will obtain the SIZE in runtime, before ... shmget ...
struct {
int num;
int num2;
struct b *arrayOfB;
} a;
<snip>
my question is: how can tell arrayOfB to point over the shared memory?
As already answered, you don't need this change; you can use the
'struct hack' in C89, or the equivalent but standard 'flexible array
member' in C99, to get the same 'in-place' layout as above.

But if you WANT to change to a pointer:
struct a *ptr,p2*;
Syntax error; should be *ptr, *p2;
p2 = ptr; //ptr is the shmat return
p2++;
ptr->arrayOfB
You're close. With the corrected declaration above, this gives you p2
pointing just after the struct-a. IF that location is suitably aligned
for a struct-b, you can just do ptr->arrayOfB = (struct b*) p2;
In fact you don't need a separate variable, just
ptr->arrayOfB = (struct b*) (ptr+1);
/* or equivalently */ = (struct b*) &ptr[1];

Unfortunately there is no standard way to determine what alignment is
needed for a struct-b, or provided by a struct-a (the size of any type
in C must be a multiple of its required alignment, to make arrays
work), so this can be unsafe. For the particular structs you showed,
it is extremely likely that any requirement for struct-a will (also)
satisfy struct-b, but it isn't absolutely 100% portably guaranteed.

If you want to use a pointer and handle the possible alignment issue,
simplest to use a union which is guaranteed to align for either:

struct a * aptr = shmat (...);
union x { struct a a; struct b b; } * xptr = (union x *) aptr;
aptr->arrayOfB = (struct b*) (xptr + 1);
/* this is guaranteed located after the struct-a,
AND aligned for a struct-b. It may _possibly_ be
offset more than actually needed and waste some space,
but on most modern systems this isn't worth worrying about,
unless your (individual) structures are truly huge,
in which case post a more specific example
and we can get into the more arcane methods. */

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Google Mike | last post by:
I'm using PHP 4.2.2 on RH9 Linux (with Progeny.com updates, in case you're wondering). I was using shared memory (shm* API) a great deal in my web applications because I have 1GB of RAM on this...
5
by: Claudio Grondi | last post by:
Background information: --------------------------------- in order to monitor mainboard sensory data as fan speeds, temperatures, applications like SpeedFan http://www.almico.com/speedfan.php or...
3
by: alanrn | last post by:
I would like to start a dialog on how to implement the equivalent functionality of UNIX shared memory in .NET. I work with a factory automation system. The bulk of the system is written in C/C++....
5
by: Jim | last post by:
Hello, I have a broken server that we are going to be moving off to a new server with a new version of DB2 but here is what I have right now: RedHat 7.0 (2.2.24smp) DB2 v6.1.0.40 I am...
1
by: zeny | last post by:
Hey everyone! I´ve been tying to create a shared memory segment with the size of a structure, as follows: typedef struct{ int id; char message; }data; In the line where i create the shared...
1
by: Ron Eggler | last post by:
Hi, I'm running an application on an embedded PC 104 platform on a Linux OS. I need some shared memory and I'm trying to create it with: int prg_shm = ::shmget(0x1999,sizeof(struct...
1
by: landacorp | last post by:
Hi, everyone! I am having trouble with values stored into the shared memory. Either i create "shmop_write" the value into the segment by cron and i being able to get it from the memory from...
3
by: Blubaugh, David A. | last post by:
To All, I was wondering if it was possible to utilize python to share a memory resource between a linux and windows system?? It should be stated that both the Linux (CENTOS 5) and windows...
0
by: Blubaugh, David A. | last post by:
Diez, What you have said is extremely concerning. I am now using VMware. With Linux as the Master and windows as the guest operating system. I was wondering if you have ever had to develop...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.