-1

I want to execute a C program from another C program. Actually I need to use system() function for my functionality as the control returns to the calling program. As I was not able to get the result with system(), I tried using execv() which was not successful either.

Below are the sample codes which I was trying

int main(void) {
puts("executing this prog from another prog"); 
return EXIT_SUCCESS; 
}

The above one is called test1.c

int main(void) {
execv("./test1",NULL); //system("./test1");
puts("!!!Hello World!!!"); 
return EXIT_SUCCESS; 
}

This is test2.c

With system(), I am getting the sh: 1: ./test1: not found error and with execv() its just ignoring the statement and printing the !!!Hello World!!!

Note : Mainly I want to know the functioning of system() for the problem I want to solve.

karra
  • 45
  • 1
  • 10
  • 1
    The error you get from `system` should be a very good hint about the problem. – Some programmer dude Mar 30 '18 at 09:27
  • Did you compile `test1.c` into `test1`? – Barmar Mar 30 '18 at 09:28
  • 1
    Is the `test1` executable in your current working directory? – Barmar Mar 30 '18 at 09:28
  • `system()` is not a system call, I removed that tag. – Barmar Mar 30 '18 at 09:29
  • `NULL` is not a valid second argument to `execv()`. It requires a valid `argv` array there. – Barmar Mar 30 '18 at 09:30
  • @Barmar yes `test1` is executable and its in the current directory – karra Mar 30 '18 at 09:31
  • If you want to use `execv()` and also continue the original program, you need to use `fork()` to run it in a child process. – Barmar Mar 30 '18 at 09:31
  • @Barmar Instead of having both `execv()` and `fork()`, I can use `system()`. Right ? – karra Mar 30 '18 at 09:33
  • Yes, that's correct. And it should have worked if the path is correct. – Barmar Mar 30 '18 at 09:33
  • @Barmar I have tried even with the array argument. `char *arr[] = {"./test1",NULL}; execv(arr[0],arr);` – karra Mar 30 '18 at 09:34
  • if `execv()` returns it's because it got an error, you should use `perror()` to see the reason. – Barmar Mar 30 '18 at 09:35
  • Possible duplicate of [System Call fork() and execv function](https://stackoverflow.com/q/19147386/608639), [How to use execv system call in linux?](https://stackoverflow.com/q/32142164/608639), [What is the difference between the functions of the exec family of system calls like exec and execve?](https://stackoverflow.com/q/20823371/608639), [System call vs Function call](https://stackoverflow.com/q/2668747/608639), [Using system() function in c](https://stackoverflow.com/q/15079921/608639), etc – jww Mar 30 '18 at 09:37
  • @karra Try to use `access()` before `system()` to check if your file exists and if it has execute permission: https://linux.die.net/man/2/access – Fabio_MO Mar 30 '18 at 09:38
  • When I have used `echo $?` it returned number 127 and in `man` page it says that if `\bin\sh` could not be executed it returns the 127 number. – karra Mar 30 '18 at 09:39
  • I have provided execution permission for the file. I have gone through the previous questions posted in stack overflow. I don't know what I am missing. – karra Mar 30 '18 at 09:46
  • Can you change `system("./test1");` to `system("pwd; ls -ld ./test1");` and see if the output is what you expect for the current directory name and the file? – Mark Plotnick Mar 30 '18 at 16:46
  • If you are after the output written to `stdout` using `popen()` would be another option. – alk Mar 31 '18 at 10:53

1 Answers1

0

The second parameter of execv must be an array of pointers to null-terminated strings, change to:

#include <unistd.h>

int main(void)
{
    char *argv[] = {NULL};
    execv("./test1", argv);
    return 0;
}

If this fails you can check the cause of the error with perror:

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

int main(void)
{
    char *argv[] = {NULL};
    if (execv("./test1", argv) == -1) {
        perror("execv");
        exit(EXIT_FAILURE);
    }
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94