system function not executed correctly

Discussion in 'C Programming' started by Morris Dovey, Mar 7, 2008.

  1. Morris Dovey

    Morris Dovey Guest

    Li Zhou wrote:
    >
    > When i use system() it always get return code 11 and the command is not
    > executed regardless of the command used. Does anyone know what is with
    > it? Environment is Linux, glibc and gcc.


    Can you post a minimal program that exhibits the problem
    behavior, please?

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto
     
    Morris Dovey, Mar 7, 2008
    #1
    1. Advertising

  2. When i use system() it always get return code 11 and the command is not
    executed regardless of the command used. Does anyone know what is with
    it? Environment is Linux, glibc and gcc.

    Regards,
    Li Zhou
     
    Anonymous User, Mar 7, 2008
    #2
    1. Advertising

  3. Morris Dovey

    santosh Guest

    Anonymous User wrote:

    > When i use system() it always get return code 11 and the command is
    > not executed regardless of the command used. Does anyone know what is
    > with it? Environment is Linux, glibc and gcc.


    Can you show us the code?
     
    santosh, Mar 7, 2008
    #3
  4. santosh wrote:
    > Anonymous User wrote:
    >
    >> When i use system() it always get return code 11 and the command is
    >> not executed regardless of the command used. Does anyone know what is
    >> with it? Environment is Linux, glibc and gcc.

    >
    > Can you show us the code?
    >


    if ( system() ) /* see if a shell exists */
    {
    int return_code = system("echo hello");
    std::cout << "return code " << return_code << std::endl;
    }

    result is:

    return code 11

    and repeated lines of:

    semop lock failure invalid argument
    semop unlock failure invalid argument
     
    Anonymous User, Mar 7, 2008
    #4
  5. Morris Dovey

    Mark Bluemel Guest

    Anonymous User wrote:
    > santosh wrote:
    >> Anonymous User wrote:
    >>
    >>> When i use system() it always get return code 11 and the command is
    >>> not executed regardless of the command used. Does anyone know what is
    >>> with it? Environment is Linux, glibc and gcc.


    But not C - the code you've shown us is C++

    >>
    >> Can you show us the code?
    >>

    >
    > if ( system() ) /* see if a shell exists */


    That should be "system(NULL)" according to my man pages and compiler

    > {
    > int return_code = system("echo hello");
    > std::cout << "return code " << return_code << std::endl;
    > }
    >
    > result is:
    >
    > return code 11
    >
    > and repeated lines of:
    >
    > semop lock failure invalid argument
    > semop unlock failure invalid argument


    I think you'd do better a) in a C++ group or b) in a forum relating to
    the GNU tools you're using. The semop warning messages suggest there's
    something awry in your installation, I'd guess.

    The code below (which is in C, as far as I can tell :) works for me
    and returns 0 from the second call to system().

    #include <stdlib.h>
    #include <stdio.h>
    int main(void) {
    if (system(NULL)) {
    int result = system("echo hello world");
    printf("Got %d\n",result);
    }
    }
     
    Mark Bluemel, Mar 7, 2008
    #5
  6. Anonymous User said:

    > santosh wrote:
    >> Anonymous User wrote:
    >>
    >>> When i use system() it always get return code 11 and the command is
    >>> not executed regardless of the command used. Does anyone know what is
    >>> with it? Environment is Linux, glibc and gcc.

    >>
    >> Can you show us the code?
    >>

    >
    > if ( system() ) /* see if a shell exists */


    That should be:

    if ( system(NULL) ) /* see if a shell exists */

    > {
    > int return_code = system("echo hello");


    That's fine, but there are one and a half reasons why it isn't doing what
    you expect. Firstly, the command (*almost* certainly) is being executed,
    but a shell is being created for the purpose, the echo is happening within
    that shell, and the shell is then terminating - all too fast for you to
    even notice (and there's no requirement on the shell, as far as I know, to
    provide a visible terminal session, although on some systems it actually
    will do that).

    One-and-a-halfthly, and this is more of a heads-up in your case than an
    actual problem, the return value is not necessarily the one that the
    command returns (alas!), but an implementation-defined value. On my
    system, however, the docs say that "The value returned is -1 on error
    (e.g. fork failed), and the return status of the command otherwise." ISO C
    doesn't guarantee this, but perhaps your implementation does.

    > std::cout << "return code " << return_code << std::endl;


    This, however, will not compile. It's stuffed full with errors.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Mar 7, 2008
    #6
  7. In article <fqrlp8$671$>,
    Anonymous User <> wrote:
    >When i use system() it always get return code 11 and the command is not
    >executed regardless of the command used. Does anyone know what is with
    >it? Environment is Linux, glibc and gcc.


    That usually indicates a segmentation fault in the called command.
    It seems unlikely that you would get this for every command, so
    you'll have to show us your exact program.

    -- Richard
    --
    :wq
     
    Richard Tobin, Mar 7, 2008
    #7
  8. Richard Tobin wrote:
    > In article <fqrlp8$671$>,
    > Anonymous User <> wrote:
    >> When i use system() it always get return code 11 and the command is
    >> not executed regardless of the command used. Does anyone know what
    >> is with it? Environment is Linux, glibc and gcc.

    >
    > That usually indicates a segmentation fault in the called command.

    You may be confusing signal 11, SISSEGV, with exit(11)

    > It seems unlikely that you would get this for every command, so
    > you'll have to show us your exact program.
    >
    > -- Richard
    >> wq


    Bye, Jojo
     
    Joachim Schmitz, Mar 7, 2008
    #8
  9. Richard Heathfield wrote:
    > Anonymous User said:
    >
    >> santosh wrote:
    >>> Anonymous User wrote:
    >>>
    >>>> When i use system() it always get return code 11 and the command is
    >>>> not executed regardless of the command used. Does anyone know what is
    >>>> with it? Environment is Linux, glibc and gcc.
    >>> Can you show us the code?
    >>>

    >> if ( system() ) /* see if a shell exists */

    >
    > That should be:
    >
    > if ( system(NULL) ) /* see if a shell exists */
    >


    yeah, otherwise it won't compile.

    >> {
    >> int return_code = system("echo hello");

    >
    > That's fine, but there are one and a half reasons why it isn't doing what
    > you expect. Firstly, the command (*almost* certainly) is being executed,
    > but a shell is being created for the purpose, the echo is happening within
    > that shell, and the shell is then terminating - all too fast for you to
    > even notice (and there's no requirement on the shell, as far as I know, to
    > provide a visible terminal session, although on some systems it actually
    > will do that).


    Hmmm, that echo command was just an example. The real command is to
    print something on the printer. When i issue the printing command from
    the shell everything works as expected but when used inside system()
    nothing happened.

    >
    > One-and-a-halfthly, and this is more of a heads-up in your case than an
    > actual problem, the return value is not necessarily the one that the
    > command returns (alas!), but an implementation-defined value. On my
    > system, however, the docs say that "The value returned is -1 on error
    > (e.g. fork failed), and the return status of the command otherwise." ISO C
    > doesn't guarantee this, but perhaps your implementation does.
    >
    >> std::cout << "return code " << return_code << std::endl;

    >
    > This, however, will not compile. It's stuffed full with errors.
    >


    This is some c++ code for debugging.
     
    Anonymous User, Mar 7, 2008
    #9
  10. In article <fqro61$996$>,
    Anonymous User <> wrote:

    >> Can you show us the code?


    We really need the whole code, not just the bit where you think
    the error is.

    >if ( system() ) /* see if a shell exists */


    This is wrong, and shouldn't compile. Have you forgotten to
    include stdlib.h?

    You should be passing a null argument to it.

    >{
    > int return_code = system("echo hello");
    > std::cout << "return code " << return_code << std::endl;


    Um, that looks like C++, not C.

    -- Richard
    --
    :wq
     
    Richard Tobin, Mar 7, 2008
    #10
  11. In article <fqrpdo$uur$>,
    Joachim Schmitz <> wrote:

    >>> When i use system() it always get return code 11 and the command is
    >>> not executed regardless of the command used. Does anyone know what
    >>> is with it? Environment is Linux, glibc and gcc.


    >> That usually indicates a segmentation fault in the called command.


    >You may be confusing signal 11, SISSEGV, with exit(11)


    No. Under Linux, the return code from system() is the signal number
    if the program dies from a signal. If the command exited with status
    11, system() would return 11*256.

    -- Richard
    --
    :wq
     
    Richard Tobin, Mar 7, 2008
    #11
  12. Richard Tobin wrote:
    > In article <fqrpdo$uur$>,
    > Joachim Schmitz <> wrote:
    >
    >>>> When i use system() it always get return code 11 and the command is
    >>>> not executed regardless of the command used. Does anyone know what
    >>>> is with it? Environment is Linux, glibc and gcc.

    >
    >>> That usually indicates a segmentation fault in the called command.

    >
    >> You may be confusing signal 11, SISSEGV, with exit(11)

    >
    > No. Under Linux, the return code from system() is the signal number
    > if the program dies from a signal. If the command exited with status
    > 11, system() would return 11*256.

    Right, sorry, didn't read the man-page properly

    int status = system("whatever")
    if ( status = -1)
    perror("system()");
    else if WISIGNALED(status)
    fprintf(stderr, "killed by signal %d\n", WTERMSIG(status));
    else if WIFEXITED(status)
    if (WEXITSTATUS(status))
    fprintf(stderr, "exited with\n", WEXITSTATUS(status));
    else
    printf("all is well!\n");
    ....

    Bye, Jojo
     
    Joachim Schmitz, Mar 7, 2008
    #12
  13. Morris Dovey

    Richard Guest

    "Joachim Schmitz" <> writes:

    > Richard Tobin wrote:
    >> In article <fqrpdo$uur$>,
    >> Joachim Schmitz <> wrote:
    >>
    >>>>> When i use system() it always get return code 11 and the command is
    >>>>> not executed regardless of the command used. Does anyone know what
    >>>>> is with it? Environment is Linux, glibc and gcc.

    >>
    >>>> That usually indicates a segmentation fault in the called command.

    >>
    >>> You may be confusing signal 11, SISSEGV, with exit(11)

    >>
    >> No. Under Linux, the return code from system() is the signal number
    >> if the program dies from a signal. If the command exited with status
    >> 11, system() would return 11*256.

    > Right, sorry, didn't read the man-page properly
    >
    > int status = system("whatever")
    > if ( status = -1)
    > perror("system()");


    You might want to use a debugger on your code and examine the condition
    above very closely. Hint : keep an eye on "status".

    > else if WISIGNALED(status)
    > fprintf(stderr, "killed by signal %d\n", WTERMSIG(status));
    > else if WIFEXITED(status)
    > if (WEXITSTATUS(status))
    > fprintf(stderr, "exited with\n", WEXITSTATUS(status));
    > else
    > printf("all is well!\n");
    > ...
    >
    > Bye, Jojo
     
    Richard, Mar 7, 2008
    #13
  14. Mark Bluemel wrote:
    > Anonymous User wrote:
    >> santosh wrote:
    >>> Anonymous User wrote:
    >>>
    >>>> When i use system() it always get return code 11 and the command is
    >>>> not executed regardless of the command used. Does anyone know what is
    >>>> with it? Environment is Linux, glibc and gcc.

    >
    > But not C - the code you've shown us is C++
    >
    >>>
    >>> Can you show us the code?
    >>>

    >>
    >> if ( system() ) /* see if a shell exists */

    >
    > That should be "system(NULL)" according to my man pages and compiler
    >
    >> {
    >> int return_code = system("echo hello");
    >> std::cout << "return code " << return_code << std::endl;
    >> }
    >>
    >> result is:
    >>
    >> return code 11
    >>
    >> and repeated lines of:
    >>
    >> semop lock failure invalid argument
    >> semop unlock failure invalid argument

    >
    > I think you'd do better a) in a C++ group or b) in a forum relating to
    > the GNU tools you're using. The semop warning messages suggest there's
    > something awry in your installation, I'd guess.
    >
    > The code below (which is in C, as far as I can tell :) works for me
    > and returns 0 from the second call to system().
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    > int main(void) {
    > if (system(NULL)) {
    > int result = system("echo hello world");
    > printf("Got %d\n",result);
    > }
    > }


    Yeap, your code works even on Cygwin.
     
    Anonymous User, Mar 7, 2008
    #14
  15. Richard wrote:
    > "Joachim Schmitz" <> writes:
    >
    >> Richard Tobin wrote:
    >>> In article <fqrpdo$uur$>,
    >>> Joachim Schmitz <> wrote:
    >>>
    >>>>>> When i use system() it always get return code 11 and the command
    >>>>>> is not executed regardless of the command used. Does anyone know
    >>>>>> what is with it? Environment is Linux, glibc and gcc.
    >>>
    >>>>> That usually indicates a segmentation fault in the called command.
    >>>
    >>>> You may be confusing signal 11, SISSEGV, with exit(11)
    >>>
    >>> No. Under Linux, the return code from system() is the signal number
    >>> if the program dies from a signal. If the command exited with
    >>> status 11, system() would return 11*256.

    >> Right, sorry, didn't read the man-page properly
    >>
    >> int status = system("whatever")
    >> if ( status = -1)
    >> perror("system()");

    >
    > You might want to use a debugger on your code and examine the
    > condition above very closely. Hint : keep an eye on "status".

    No debugger needed, it should be status == -1.

    >> else if WISIGNALED(status)
    >> fprintf(stderr, "killed by signal %d\n", WTERMSIG(status));
    >> else if WIFEXITED(status)
    >> if (WEXITSTATUS(status))
    >> fprintf(stderr, "exited with\n", WEXITSTATUS(status));

    and here's a %s missing...

    >> else
    >> printf("all is well!\n");
    >> ...
    >>
    >> Bye, Jojo
     
    Joachim Schmitz, Mar 7, 2008
    #15
  16. Joachim Schmitz wrote:
    > Richard wrote:
    >> "Joachim Schmitz" <> writes:
    >>
    >>> Richard Tobin wrote:
    >>>> In article <fqrpdo$uur$>,
    >>>> Joachim Schmitz <> wrote:
    >>>>
    >>>>>>> When i use system() it always get return code 11 and the command
    >>>>>>> is not executed regardless of the command used. Does anyone know
    >>>>>>> what is with it? Environment is Linux, glibc and gcc.
    >>>>
    >>>>>> That usually indicates a segmentation fault in the called
    >>>>>> command.
    >>>>
    >>>>> You may be confusing signal 11, SISSEGV, with exit(11)
    >>>>
    >>>> No. Under Linux, the return code from system() is the signal
    >>>> number if the program dies from a signal. If the command exited
    >>>> with status 11, system() would return 11*256.
    >>> Right, sorry, didn't read the man-page properly
    >>>
    >>> int status = system("whatever")
    >>> if ( status = -1)
    >>> perror("system()");

    >>
    >> You might want to use a debugger on your code and examine the
    >> condition above very closely. Hint : keep an eye on "status".

    > No debugger needed, it should be status == -1.
    >
    >>> else if WISIGNALED(status)
    >>> fprintf(stderr, "killed by signal %d\n", WTERMSIG(status));
    >>> else if WIFEXITED(status)
    >>> if (WEXITSTATUS(status))
    >>> fprintf(stderr, "exited with\n", WEXITSTATUS(status));

    > and here's a %s missing...

    Rubbish, %d. It's been a long day...

    >
    >>> else
    >>> printf("all is well!\n");
    >>> ...
    >>>
    >>> Bye, Jojo
     
    Joachim Schmitz, Mar 7, 2008
    #16
  17. Morris Dovey

    Richard Guest

    "Joachim Schmitz" <> writes:

    > Richard wrote:
    >> "Joachim Schmitz" <> writes:
    >>
    >>> Richard Tobin wrote:
    >>>> In article <fqrpdo$uur$>,
    >>>> Joachim Schmitz <> wrote:
    >>>>
    >>>>>>> When i use system() it always get return code 11 and the command
    >>>>>>> is not executed regardless of the command used. Does anyone know
    >>>>>>> what is with it? Environment is Linux, glibc and gcc.
    >>>>
    >>>>>> That usually indicates a segmentation fault in the called command.
    >>>>
    >>>>> You may be confusing signal 11, SISSEGV, with exit(11)
    >>>>
    >>>> No. Under Linux, the return code from system() is the signal number
    >>>> if the program dies from a signal. If the command exited with
    >>>> status 11, system() would return 11*256.
    >>> Right, sorry, didn't read the man-page properly
    >>>
    >>> int status = system("whatever")
    >>> if ( status = -1)
    >>> perror("system()");

    >>
    >> You might want to use a debugger on your code and examine the
    >> condition above very closely. Hint : keep an eye on "status".


    > No debugger needed, it should be status == -1.


    There are many people in CLC who think debuggers are evil. Having worked
    on numerous huge multi team projects I think their view is distorted to
    say the least. We are not all Linus Torwalds.

    I run ALL my code through a debugger watching the locals and parameter
    panels as I step through for any unexpected blinks to occur.

    This would have immediately alerted you to the change in status.

    So my point was more "use a debugger" than "in this case". Well, it was
    intended to be.

    Littering code with printfs in amateurish at best unless there is no
    good HW debugger for your platform.

    Read this and good luck!

    http://heather.cs.ucdavis.edu/~matloff/UnixAndC/CLanguage/Debug.html#tth_sEc2
     
    Richard, Mar 7, 2008
    #17
  18. Anonymous User wrote:
    [...]
    > >> int return_code = system("echo hello");

    > >
    > > That's fine, but there are one and a half reasons why it isn't doing what
    > > you expect. Firstly, the command (*almost* certainly) is being executed,
    > > but a shell is being created for the purpose, the echo is happening within
    > > that shell, and the shell is then terminating - all too fast for you to
    > > even notice (and there's no requirement on the shell, as far as I know, to
    > > provide a visible terminal session, although on some systems it actually
    > > will do that).

    >
    > Hmmm, that echo command was just an example. The real command is to
    > print something on the printer. When i issue the printing command from
    > the shell everything works as expected but when used inside system()
    > nothing happened.

    [...]

    Does the system("echo hello") also return 11? (If not, then why
    post code that doesn't fail?)

    See Richard Tobin's reply elsethread regarding SEGV crashes.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Mar 7, 2008
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruno Alexandre

    Function executed twice

    Bruno Alexandre, Feb 14, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    Bruno Alexandre
    Feb 14, 2007
  2. Naccessible
    Replies:
    2
    Views:
    400
  3. Brice
    Replies:
    2
    Views:
    340
    Brice
    Nov 5, 2006
  4. Jim Keenan
    Replies:
    8
    Views:
    164
    A. Sinan Unur
    Jul 16, 2005
  5. DrKen
    Replies:
    8
    Views:
    1,911
    Denis McMahon
    Jun 24, 2011
Loading...

Share This Page