Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > system function not executed correctly

Reply
Thread Tools

system function not executed correctly

 
 
Richard Tobin
Guest
Posts: n/a
 
      03-07-2008
In article <fqrpdo$uur$(E-Mail Removed)>,
Joachim Schmitz <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-07-2008
Richard Tobin wrote:
> In article <fqrpdo$uur$(E-Mail Removed)>,
> Joachim Schmitz <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      03-07-2008
"Joachim Schmitz" <(E-Mail Removed)> writes:

> Richard Tobin wrote:
>> In article <fqrpdo$uur$(E-Mail Removed)>,
>> Joachim Schmitz <(E-Mail Removed)> 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

 
Reply With Quote
 
Anonymous User
Guest
Posts: n/a
 
      03-07-2008
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.
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-07-2008
Richard wrote:
> "Joachim Schmitz" <(E-Mail Removed)> writes:
>
>> Richard Tobin wrote:
>>> In article <fqrpdo$uur$(E-Mail Removed)>,
>>> Joachim Schmitz <(E-Mail Removed)> 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



 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-07-2008
Joachim Schmitz wrote:
> Richard wrote:
>> "Joachim Schmitz" <(E-Mail Removed)> writes:
>>
>>> Richard Tobin wrote:
>>>> In article <fqrpdo$uur$(E-Mail Removed)>,
>>>> Joachim Schmitz <(E-Mail Removed)> 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



 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      03-07-2008
"Joachim Schmitz" <(E-Mail Removed)> writes:

> Richard wrote:
>> "Joachim Schmitz" <(E-Mail Removed)> writes:
>>
>>> Richard Tobin wrote:
>>>> In article <fqrpdo$uur$(E-Mail Removed)>,
>>>> Joachim Schmitz <(E-Mail Removed)> 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/~matlo....html#tth_sEc2

 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      03-07-2008
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: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why would a function be executed twice if only called onload()? DrKen Javascript 8 06-24-2011 10:27 PM
Function executed twice Bruno Alexandre ASP .Net 0 02-14-2007 09:12 PM
My web login screen does not function correctly Naccessible Java 2 12-07-2006 07:02 PM
Wait for a system command executed in the background Brice Python 2 11-05-2006 10:21 PM
ASPX not executed taja ASP .Net 6 10-22-2003 08:57 PM



Advertisments