Sharing a C struct

This is a discussion on Sharing a C struct within the Linux Programming forums, part of the Platform Specific Boards category; Hi All, I've got a problem. I have wrapped a C linux library using perl-xs. The library provides - an ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    Sharing a C struct

    Hi All,

    I've got a problem.

    I have wrapped a C linux library using perl-xs. The library provides

    - an init function (where a C struct is initialised)
    - a do function (where actions are performed using the C struct)
    - a free function (where the C struct is freed).

    I have 30 perl slaves (processes) that each use their own instance of the C struct in memory.

    The problem is that each struct requires around 30 MB (30 *30 = a lot).

    Now I would normally initialise the struct in the parent perl controlling thread and pass it too each slave, however I do not have control of the parent thread (at least not withour hacking someone else's source code).

    So, the question is, how can I share the initialised C struct between all threads? I've had a couple of ideas:

    - write a server that maintains a pointer to the struct.
    - keep the struct in a file :/ ?!
    - somehow cache the struct between processes within the C side of the wrapper - how could I do this?

    Any ideas?

    Thanks for any help on this,

    rotis23

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I think the third approach is not feasible. The second is slow, depending on the way you use the file. If you can memory map a file on a RAM disk, it would be quite ok, but a hard disk is slow. The first sounds the best, but it's hard work, you need to implement the server and provide some protocol for communication, locking etc. The advantage is that you can make it a distributed app very easily
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Thanks for your reply CornedBee.

    I've found a chapter in my Linux programming book that describe 'Shared Memory'.

    Can I use this to solve my problem?

    I assume this is going to raise all sorts of locking issues.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Should be possible. But write sync is gonna be a nightmare

    There should be cross-process mutexes too, though. The front page of the Linux forum even contains a thread that mentions mutex, but I didn't look at it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 08:13 AM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  4. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  5. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21