473,382 Members | 1,628 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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
Mar 7 '08 #1
16 3562
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
Mar 7 '08 #2
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?

Mar 7 '08 #3
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
Mar 7 '08 #4
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
Mar 7 '08 #5
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);
}
}
Mar 7 '08 #6
In article <fq**********@news.yaako.com>,
Anonymous User <no****@localhost.comwrote:
>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
Mar 7 '08 #7
Richard Tobin wrote:
In article <fq**********@news.yaako.com>,
Anonymous User <no****@localhost.comwrote:
>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
Mar 7 '08 #8
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.
Mar 7 '08 #9
In article <fq**********@online.de>,
Joachim Schmitz <jo**@schmitz-digital.dewrote:
>>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
Mar 7 '08 #10
Richard Tobin wrote:
In article <fq**********@online.de>,
Joachim Schmitz <jo**@schmitz-digital.dewrote:
>>>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
Mar 7 '08 #11
"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
Richard Tobin wrote:
>In article <fq**********@online.de>,
Joachim Schmitz <jo**@schmitz-digital.dewrote:
>>>>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
Mar 7 '08 #12
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.
Mar 7 '08 #13
Richard wrote:
"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
>Richard Tobin wrote:
>>In article <fq**********@online.de>,
Joachim Schmitz <jo**@schmitz-digital.dewrote:

>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

Mar 7 '08 #14
Joachim Schmitz wrote:
Richard wrote:
>"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
>>Richard Tobin wrote:
In article <fq**********@online.de>,
Joachim Schmitz <jo**@schmitz-digital.dewrote:

>>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

Mar 7 '08 #15
"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
Richard wrote:
>"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
>>Richard Tobin wrote:
In article <fq**********@online.de>,
Joachim Schmitz <jo**@schmitz-digital.dewrote:

>>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/~matlo....html#tth_sEc2

Mar 7 '08 #16
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:Th*************@gmail.com>

Mar 7 '08 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tamer Higazi | last post by:
Hi! I want to run an external Program in my C++ Program. I have a string of an existing program which has to be executed. Which command do i have to execute and in which library is it to find? ...
2
by: Xah Lee | last post by:
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the...
12
by: sunil | last post by:
Hi All, Please have a look the below program #include<stdlib.h> int i = system("pwd"); I compiled the above program in UNIX ,it got compiled and executed with out any errors.It prints the...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
3
by: extrnh | last post by:
i have a c++ program compiled in visual c++ that links to a c library. when a system function is called in the c library a blank command window is displayed while the c logic is executing. this...
9
by: Matt | last post by:
Hi friends, Okay so well, I have quite a problem right now with a file stream. What I am doing is to use the Cannon SDK dlls to get control over my old Cannon A60 Camera for some surveillance...
23
by: Maarten | last post by:
Howdy, Recently I switched from a Windows PC to Mac OS-X 10.5 (php v5.2.6) and I have a little problem with one function within my cd-management script. For extracting a bit of info from my...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.