Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   system function not executed correctly (http://www.velocityreviews.com/forums/t596810-system-function-not-executed-correctly.html)

Morris Dovey 03-07-2008 02:15 PM

Re: system function not executed correctly
 
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

Anonymous User 03-07-2008 03:08 PM

system function not executed correctly
 
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

santosh 03-07-2008 03:31 PM

Re: system function not executed correctly
 
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?


Anonymous User 03-07-2008 03:49 PM

Re: system function not executed correctly
 
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

Mark Bluemel 03-07-2008 04:04 PM

Re: system function not executed correctly
 
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);
}
}

Richard Heathfield 03-07-2008 04:05 PM

Re: system function not executed correctly
 
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 Tobin 03-07-2008 04:08 PM

Re: system function not executed correctly
 
In article <fqrlp8$671$1@news.yaako.com>,
Anonymous User <nobody@localhost.com> 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

Joachim Schmitz 03-07-2008 04:10 PM

Re: system function not executed correctly
 
Richard Tobin wrote:
> In article <fqrlp8$671$1@news.yaako.com>,
> Anonymous User <nobody@localhost.com> 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



Anonymous User 03-07-2008 04:10 PM

Re: system function not executed correctly
 
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.

Richard Tobin 03-07-2008 04:14 PM

Re: system function not executed correctly
 
In article <fqro61$996$1@news.yaako.com>,
Anonymous User <nobody@localhost.com> 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


All times are GMT. The time now is 04:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.