Teodora kulikov Teodora kulikov - 3 months ago 8
C Question

Proper use of execl in linux

I want to run file test2.c from the file test1.c .But it is not happening so?Any mistake?

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(){
int j=execl("/home/ishan/os","test2.c",(char*)NULL);
}

Answer

You are passing in the arguments incorrectly.

Assuming that your executable image is called "test2", try this:

    int j=execl("/home/ishan/os/test2", "/home/ishan/os/test2", (char*)NULL);

Notes:

  • The first argument is the complete path to the target executable, including the filename.
  • The second argument is passed as argv[0] to the new process. Conventionally, this is also the complete path to the target executable.
  • The target executable is not the source code file ("test2.c"), but the result of compiling that source code (typically, "test2").

Reference: http://linux.die.net/man/3/execl