Thread: Use system() in Solaris

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    Use system() in Solaris

    Hello,

    I am trying to get result status from system(my_exe) on Solaris. I want to detect if `my_exe` gives exception/core dump...So I created dummy exe which has zero division operation.

    And then I run it with system().
    The problem is that I can't detect the exception. My code is like:

    int status = system("./my_exe");
    printf("WIFEXITED=%d\n", WIFEXITED(status) );

    the macro WIFEXITED always gives 1 !

    On Linux it becomes 0, and I can WTERMSIG() then. But I cant achieve this on Solaris.
    I tried different ways to kill/stop the executed process - WIFEXITED is always 1.

    thanks for any help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The return result of system() is implementation-defined for anything other than system(NULL).
    Which in short means you can't use the result to tell you anything useful.

    For example, what I suspect you're getting is the exit status of the shell, not the exit status of your process. Depending (or perhaps not) on the complexity of the string you pass, you may find that your "cmd" is translated into "/bin/sh -c cmd" instead. Result, you see what sh exited with.

    If you want that much control over the child process, and to be able to reliably get the exit status, then you must use fork() and exec() to run the child process.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The return value of system() is not the same as what wait() or waitpid() returns. And even if it were, you would be getting the status of the shell process that executed the command.

    You'll have to fork(), exec() the command, and call waitpid() yourself if you want to use WIFEXITED and related macros.

    gg

    [edit]regurgitation of Salem's post[/edit]

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    Thanks a lot guys. Very helpful answers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 01:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07:56 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  4. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM
  5. Problem Reporting System. Need Advide!
    By brunomiranda in forum Tech Board
    Replies: 9
    Last Post: 09-25-2003, 09:21 PM