Thread: Sharing a C struct

  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,895
    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,895
    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, 07: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, 03:25 AM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM