I have some code that works. However, when I try to make it not work (i.e. try to execute a non-existent program), I don't see any indication that it hasn't worked. Here's the code snippet.

pid_t pid;
//    char *const paramList[] =  {"/usr/threshold/bin/preconfig.sh", options[indexSelected].optionName, NULL};
    const char* paramList[] =  {"doesNotExist", "-r", NULL};

    printf ("Forking\n");
    if ((pid = fork()) == -1)
    {
        printf ("pid == -1\n");
        printf ("Error : Program did not fork correctly.");
    }
    else if (pid == 0) 
    {
        printf ("pid == 0\n");
        execvp(paramList[0], paramList);
        printf("Error : execvp did not execute successfully.\n");
    }

    printf ("Program done");
    return 0;

Line 2 is the real executable. Uncomment line 2 and comment line 3 and everything works great. The program in line 2 exists and I have seen by the files that it changed that it has executed successfully.

Comment out line 2 and uncomment line 3 and that changes it to make an execvp call to a non-existent program. I expected line 15 to display. However, it did not display.

Does anyone know why? I need some way of knowing whether my call to the program was successful. Thanks.

Recommended Answers

All 8 Replies

Here's what the manpages have to say...

RETURN VALUE
If any of the exec() functions returns, an error will have occurred.
The return value is -1, and errno will be set to indicate the error.

Here's what the manpages have to say...

Right. If there was an error, it should have returned and thus line 15 should have been printed. But it wasn't printed.

does it print "program done" and return 0?

does it print "program done" and return 0?

Which one..The program forks

point. so i mean, does it do it print that end message twice, from the parent and the child? or just once? (or none?)

i don't have the answer, i'm just trying to narrow down possibilities.

I wrote this simple program to demonstrate how the exec family of functions behave:
testp.c

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

int main(int argc, char**argv)
{
	execlp(argv[1], argv[1], NULL);
	fprintf(stdout, "failed\n");
	exit(EXIT_SUCCESS);
}

Usage: execute program with a valid shell executable say ps

./testp ps

Then execute with a bogus shell executable say pssssp

./testp pssssp

Yeah, that worked. So does this change to use execvp.

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

int main(int argc, char*argv[])
{
	char* args[3] = {argv[1], argv[2], NULL};
	execvp(args[0], args);
	fprintf(stdout, "failed\n");
	exit(EXIT_SUCCESS);
}

Interestingly, this works now (i.e. gives the error message):

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

int main(int argc, char*argv[])
{
    pid_t pid;
//    char *const paramList[] =  {"/usr/threshold/bin/preconfig.sh", options[indexSelected].optionName, NULL};
    char* paramList[] =  {"doesNotExist", "-r", NULL};

    printf ("Forking\n");
    if ((pid = fork()) == -1)
    {
        printf ("pid == -1\n");
        printf ("Error : Program did not fork correctly.");
    }
    else if (pid == 0) 
    {
        printf ("pid == 0\n");
        execvp(paramList[0], paramList);
        printf("Error : execvp did not execute successfully.\n");
    }

    printf ("Program done");
    return 0;
}

"execvp did not execute successfully" displays once, "Program done" displays twice. That seems correct to me.

It's not what was happening with the larger program and it didn't seem to be happening before when I was testing on some smaller programs. But I guess the above program is correct?

Not sure what I did before. I THINK this is all good now. I'm a little confused since it seems to be operating a little differently now. Will report back. Thanks.

Well, I can't recreate the earlier behavior and it works now, so I guess it's solved. Thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.