Shared Library creation tips

This is a discussion on Shared Library creation tips within the C Programming forums, part of the General Programming Boards category; I'm creating a shared library for the first time and would appreciate it if someone could provide a little guidance. ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    Shared Library creation tips

    I'm creating a shared library for the first time and would appreciate it if someone could provide a little guidance. Specifically, are there any special considerations I should be aware of? I'm compiling on Solaris 9 on a sparc station.

    One more... I have a timeout mechansim using signals:
    Code:
    void timeoutAlarm()
    {
       variable1 = value1; variable2 = value2...
       exit(1);
    }
    ...
    signal(SIGALRM, timeoutAlarm)
    where I want to return to the calling program when the timeout expires. My fear is that having an "exit(1)" in the timeoutAlarm() function will cause the calling program to exit instead of just returning to the calling function/program with the other stuff set in timeoutAlarm. Is this fear founded? If so, how would I accomplish this? Are there other alternatives?

    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    The exit() function will cause the program to terminate. If you are just trying to return to the calling function, then why not use the return statement? I might have misunderstood your question a little bit.

    Also, as a side note...
    The only values you should pass to exit() are EXIT_SUCCESS, or EXIT_FAILURE. You can also pass 0 which is the same as EXIT_SUCCESS.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Sorry if I was vague. Here is a more concrete explanation...

    I have a set of functions that I want to include in a shared library. I have a timeout mechanism for each of these functions. Given the following...

    Code:
    int timeoutAlarm(int n)
    {
       return;
    }
    
    int otherFunction()
    {
       signal(SIGALRM, timeoutAlarm);
       alarm(10);
       ...
    }
    Now, if the otherFunction() function is called by an external program will the return in timeoutAlarm() return to that calling program? Or just to the otherFunction()?

    Thanks for the clarification. (I know what I hope the answer is because it will make my life that much less complicated!)

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    It would return to whatever statement the signal interrupted. I'm not sure what otherFunction() does, but would it be possible for it to continuously check a variable? That way you could make timeoutAlarm() just set a global value like timedout to 1 or something. When otherFunction() sees that value then you can return back to the function that called otherFunction().
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    the problem I see with continually checking a global "timedout" variable is that if the alarm signal interrupts a function that is waiting on network i/o (for example) and simply returns it back to that interrupted function, then it may take several moments to complete the (network i/o) transaction (if it completes at all) before moving on to the next command - which would be my first opportunity to check the global variable. (or am I missing something regarding signal handling?)

    If the network i/o is a problem then I would like to simply cut my losses and return to the calling function instead of being dependent on that transaction completing.

    I'm guessing that this is not an uncommon scenario. I welcome any ideas and suggestions.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Most (I think, maybe all, not sure) I/O functions will fail if they're interrupted by a signal. So if you're waiting on a read() for instance, and the read() operation is interrupted by a signal, when the signal handler returns (after setting the global timedout variable for instance) then the program will continue after the read() call and that read() call will have returned -1 if no data had been read yet, or the number of bytes that had been read before the call was interrupted. This is listed in the ERRORS section of read()'s man page:
    Code:
           EINTR  The call was interrupted by  a  signal  before  any
                  data was read.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Cool. I'll run on the assumption that the signal will interrupt the current command/function and return to the calling function where I can then evaluate the global variable... unless I hear otherwise.

    I'll have to add a bit of overhead to my remaining function calls to perform this evaluation, but I think that it will give me the desired functionality. Thanks for the input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shared library for QNX
    By ReeV in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 10:58 AM
  2. Shared library design problem
    By p1r0 in forum C++ Programming
    Replies: 9
    Last Post: 03-23-2009, 12:36 PM
  3. Problems using/creating a shared library
    By benshi in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2008, 02:27 PM
  4. Global variable in shared library
    By krock923 in forum C Programming
    Replies: 5
    Last Post: 01-11-2008, 03:56 PM
  5. Problem calling external method from within a shared library
    By Major Tom in forum C++ Programming
    Replies: 0
    Last Post: 04-21-2007, 09:14 AM

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