Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

An EXEC call (UNIX)

Posted on 1998-04-19
15
317 Views
Last Modified: 2008-02-01
Is there any way to know that exec failed.

Suppose i execute
        execvl("hi","hi","-la");
How do i know that this is invalid command, since it
does not return -1.

thanks      
0
Comment
Question by:danil082697
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 7

Expert Comment

by:busuka
ID: 1249852
danil. I found only execl, execv, execle, execve, execlp and execvp functions.
Are you sure that you posted right name ? BTW, I suggest to use
spawn family instead of exec. Because in case of exec:
"A successful call to exec() does not return because the new
program overwrites the calling program."

This is result, taken from "man exec" command made on HP-UX.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1249853
This is not a very meaningful answer.

That is, you are not supposed to get rid of exec*() calls, rather you should understand how they work.

If the exec*() call succedes, the new process image replaces the current process. I.e. successful calls to the exec functions do not return because the system overlays the calling process with the new process.

If the exec*() functions returns to the calling process image, an error has occurred; the return value is -1 (at least on my OS), and the function sets errno to indicate the error.

Have a look, on your OS, at exec(2) (man exec, or man 2 exec) for complete reference.

danil, if this is the correct answer, please reopen this question to me, thanks.
0
 
LVL 7

Expert Comment

by:busuka
ID: 1249854
julio, answer really answers ;) Take a look on original question:
"How do i know that this is invalid command, since it does not
return -1"
If it doesn't returns -1, that is standard for every OS I know
(maybe except VAX, where I have had troubles to find C compiler ;),
this means that danil actually ran some program "hi". We have
only one way to know that for sure: from danil's feedback.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 5

Expert Comment

by:julio011597
ID: 1249855
Yes, right... DANIL!

Joke apart, if you didn't think "execvl"  could be a typo (but how could the program compile at all then?), that's what i would have asked first, *before* answering.

My comment was mostly generated by your "I suggest to use spawn family instead of exec. Because...", which does not seem neither correct nor anyway an answer to me.

Yes, answer really answers ;-)
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1249856
Just an additional comment about real answers.

Sometimes users ask something which is simply meanigless. In those cases, IMO, you should tell them their very approach is wrong and give the correct one, instead of giving a *working* solution which keeps the wrong design.

But this is really meant as just my opinion.
0
 
LVL 1

Author Comment

by:danil082697
ID: 1249857
Actually I am wrigting a shell, and i am executing an
      status=execvp()
call, which really does not matter.
Anyway my status is different depending on command. For Ex:
"ls" -> status = 0;
"ls>out"status = 1350;/*some meanigless #*/
"hello" status = 256; /*hello does not exist, actually it                               always returns 256 if command is                               invalid*/
0
 
LVL 7

Expert Comment

by:busuka
ID: 1249858
How you defined status ? Integer, signed/unsigned ? You print "status" with printf, right ?
What % you use ? status should be signed int and printf('Status= %i",status);
0
 
LVL 84

Expert Comment

by:ozo
ID: 1249859
"ls>out" is unlikely to be a valid filename in your PATH
Did you check the value of errno after the execvp fails?
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 150 total points
ID: 1249860
as julio said, exec* spawns a new process replacing the existing
(with the exec() call) and never returning, if it returns exec
failed anyhow.
Error conditions are placed in the common error variable.
So just put a printf behind and you'll see it, like:
 printf("exec failed with error %d\n", error);

Don't forget to have anywhere
      extern int error;
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1249861
Is this C-shell??
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1249862
julio, I meant C, you not?
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1249863
Actually, danil does not in his Tuesday, April 21 1998 - 08:27AM PDT comment.
BTW, i've never seen a common "error" variable in C.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1249864
I thought it was called "errno"
But if danil is expecting something to return, maybe system or fork is what's wanted?

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1249865
it's errno
Why doing a fork? exec* failed if it returns.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1249866
With fork() you are actually spawning a child, and, in the parent, you not only know if the fork at all succeded, also will be able to get the child's exit value.

IMO, what ozo also points out is that danil should give some feedback.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question