tags:

views:

76

answers:

1
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main (int argc, const char * argv[])
{
    printf("start\n");
    char *const parmList[] = {"/bin/ls", "-l", NULL};
    execv("/bin/ls", parmList);
    return 0;
}

I compiled with GCC4.2 Any ideas why this might crash? I'm not getting any error messages in xcode.

EDIT: user error. "crash" meant xcode froze when it ran the program. Pressing continue works fine.

+2  A: 

That code runs and compiles fine in my environment, gcc 4.4.3 under Ubuntu 10. That leads me to believe that you have a different problem from the one you think you have :-)


pax@pax-desktop:~$ ./testprog
start
total 2152
drwxr-xr-x 11 pax pax    4096 2010-10-02 08:23 Pax
: :
----r-S---  1 pax pax       0 2010-08-23 18:58 xyz

pax@pax-desktop:~$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Try the following code to see if the actual exec is failing. If it is, it should tell you why. If it isn't, then you won't see the rc output at all.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main (int argc, const char * argv[])
{
    int rc;
    printf("start\n");
    char *const parmList[] = {"/bin/ls", "-l", NULL};
    rc = execv("/bin/ls", parmList);
    printf ("rc = %d, errno = %d\n", rc, errno);
    return 0;
}

Also check to make sure the /bin/ls is what you expect it to be (an executable, not a script, for example).

And it's worth clarifying what you mean by "crash". Is it just not producing any output? It it dumping a core file? Is it bringing your entire OS to its knees, causing a reboot?

paxdiablo
+1, I tried on my Mac and it works fine.
Carl Norum
funny. When in xcode, it freeze like I have an error. If I press continue, it works like it should.
joels
Interesting. If "continue" is a button, you may have a breakpoint set that it's stopping on. That's one possibility. Another is that the xcode IDE may not be used to having the entire process swapped out underneath it (which `exec` does). Both of those are suppositions but possible nonetheless.
paxdiablo