Thread: Passing function pointer using message queues

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    Passing function pointer using message queues

    Hi

    A general question first: is there a way to pass a function pointer between two user
    space applications in Linux, using a message queue (msgsnd, msgget pair)?

    Assuming that this can be done, here is what I'm trying to do:
    ================================
    In a header file I've defined:
    ================================
    Code:
    typedef struct msgbuf{
       int mtype;
       int (*ptr)();
       } message_buf;
    
    int myfunc(int);
    ================================
    In the sending application:
    =================================
    Code:
    int myfunc(int mdata){
       /*Do something */
       return 0;   
    };
    .
    .
    .
       struct msgbuf* m_msg; /* Structure used for received messages. */
       int queue_id;         /* ID of the created queue. */
       int i;
    
       /* Access the public message queue. */
       queue_id = msgget(QUEUE_ID, 0);
       if (queue_id == -1) {
          perror("msgget");
          exit(1);
       }
    
       /* Allocate space for message */
       m_msg = (struct msgbuf*)malloc(sizeof(struct msgbuf)+MAX_MSG_SIZE);
    
       /* Set valid message type. */
       m_msg->mtype = VALID_TYPE;
    
       /* Set function. *** HOPING THIS IS RIGHT ***/
       m_msg->ptr = myfunc;
    
       /* Send message */
       i = msgsnd(queue_id, m_msg, sizeof(struct msgbuf)+1, 0);
       if (rc == -1) {
          perror("msgsnd");
          exit(1);
       } 
    
       /* Free allocated memory. */
       free(m_msg);
    .
    .
    .
    ================================
    In the receiving application:
    =================================
    Code:
      struct msgbuf * m_msg; /* Structure used for messages. */
      int i;
      int (*func)(int);
    .
    .
    
       while (1) {
          i = msgrcv(m_qid, m_msg, MAX_MSG_SIZE+1, VALID_TYPE, 0);
          if (rc == -1) {
             perror("msgrcv");
             exit(1);
          }
          func = m_msg->cb_ptr;
          /*** HERE IS THE PROBLEM. SEGMENTATION FAULT WHEN REACHED ***/
          i = func(100);
       }
    .
    .
    Any help, even more a sample code or link, is more than welcome.

    LxRz

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    In general a pointer from one process is meaningless in another. There is a chance, if both processes are running from the same binary, that a function pointer might work in both processes. But instead of counting on that, just pass a numeric code which indicates the function to be called, and dispatch the call with a switch statement or some other kind of lookup.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    3
    brewbuck thanx for the tip.

    The method you described was my initial choice but what I'm really
    after is passing a function pointer to realise a callback mechanism,
    not in a local (same proccess) manner but between two applications
    like this:
    the first process delivers a message (including a callback pointer) to the
    second, and when the second is done (asyhcronously) it replies using
    the callback pointer.

    Do you think this is possible? Thanx again.

    LxRz

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, that's absolutely impossible. The second process will have to send a message back.
    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

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    3
    CornedBee thanx for that ...
    I'll implement a message queue to the reverse path as well, and let
    the first process deal with the callback.

    LxRz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Replies: 9
    Last Post: 12-25-2007, 05:01 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM