2

I have a C program, xyz.c which does some computations on a certain variable, say transferme. I want to transfer this variable/ pass this variable to another C program, say jkl.c.

I have been trying to do the following:

Fork the xyz.c and use execvp() to pass this transferme to an executable of jkl.c [Turns out execvp will accept only paths to a binary executable or a shell script.]

What is happening is,

  1. execvp() does not transfer variables, it needs const char * to be the type of the argument.
  2. passing the path to the binary executable does not work. [I tried a simple forking and execvp, without trying to pass the variable.]

Could anyone please:

  1. Tell me how to use execvp() here in this case? where the binary executable is in the home folder, say the path is /home/user/jkl.o
  2. Tell me how to pass this variable to the other program?

I tried looking into pipe, but I got more confused.

EDIT: I meant jkl.out

Mekap
  • 2,065
  • 14
  • 26
complextea
  • 393
  • 1
  • 5
  • 16
  • 1
    `xyz.c` and `jkl.c` are not programs, they are _source files_ – Eregrith May 06 '15 at 08:30
  • 1
    Just to make sure, is `jkl.c` just another source code file, or an executable? – user1978011 May 06 '15 at 08:31
  • 1
    Do they each have a `main` function? – Eregrith May 06 '15 at 08:32
  • 1
    What type of variable is `transferme`? Most types can easily converted to a string and back. – JS1 May 06 '15 at 08:34
  • @Eregrith They both have `main` functions. – complextea May 06 '15 at 08:38
  • @JS1It needs to be an integer in actuality, But, I can work with strings. I just need to first be able to transfer them. Being a string does not help. It also has to be `const` right? If you are hinting at that. – complextea May 06 '15 at 08:39
  • @HobbitEesmereldatGoldworthy Then why do you try calling `jkl.o` instead of the executable you get from that file ? You said you tried without passing the variable – Eregrith May 06 '15 at 08:39
  • @user1978011 If you can help me with `jkl.c` being an executable, yay!, if you're able to help me with `jkl.c` being a source file, double yay! – complextea May 06 '15 at 08:40
  • @Eregrith I tried calling the source initially, but nothing worked. I then read somewhere that only paths to executables and shell scripts would work. Guess that's wrong? – complextea May 06 '15 at 08:43
  • You have to compile 2 program separatly, and generate two different executables. – Mekap May 06 '15 at 08:46
  • @HobbitEesmereldatGoldworthy You can't call the source indeed. But when you compile your source and link it (with `gcc -o jkl jkl.c` for instance) you get an executable. How would you use your first `xyz.c` in the first place if it weren't compiled into an executable? – Eregrith May 06 '15 at 08:47

1 Answers1

0

You have serveral ways to transfer data to another program. (pipin', files, and way more. I can edit my answer later and give a more standard guide about sending data between two C programs if it's necessary) Let's look execl() for your case.

We have to agree first that the program jkl.o will be started by your main program, and will receive as its start parameter a string containing the argument.

Let's look at the man of execl() :

The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

OK. So you first have to give execl() your path, and then, in each row an argument. You pointed out that you need to fork, it's true. So let's do that, we fork, and then we send a string containing your transferme. I'm going to assume that this variable is a regular string.

 child_pid = fork() 
/* fork() == 0 for child process */

   if(child_pid == 0)
   {  
      /*executing  jkl.o */
      execl("/home/user/a.out", "a.out" , transferme);  /* with a.out being your second program, you can change that to whatever you'd want*/
   }
/* parent stuff*/

This should do the trick.


[update from comment:]

I forgot that argv[0] (ie the second argument of execl must be the name of the program itself.), so try

execl("/home/user/d.out", "d.out", transferme, (char*) NULL);
alk
  • 69,737
  • 10
  • 105
  • 255
Mekap
  • 2,065
  • 14
  • 26
  • How is `execl` going to execute an object file that does not have a `__start()`? – Eregrith May 06 '15 at 08:41
  • 1
    @Eregrith Yeah, i just'd c/c without thinking, i edited my answer that to its second program right now, thanks for the heads up – Mekap May 06 '15 at 08:44
  • @Mekap Okay, so that works! Just one more question. Where do I receive the `transferme`? I print the argv[1] in the main in jkl.c, but only 'null' gets printed. Heres's my execl() call: `execl("/home/user/d.out", transferme, (char*)NULL);` – complextea May 06 '15 at 09:07
  • Because when I didn't pass the (char*)NULL, I got some `not enough variables to fit a sentinel` error. @Mekap – complextea May 06 '15 at 09:08
  • @HobbitEesmereldatGoldworthy yeah, reread my answer, i forgot that argv[0] (ie the second argument of execl must be the name of the program itself.), so try `execl("/home/user/d.out", "d.out", transferme, (char*)NULL);` – Mekap May 06 '15 at 09:19
  • 1
    @HobbitEesmereldatGoldworthy you could make execvp work, you just have to build "d.out", transferme into a char array. You can do it too if you want to. just have a `char tab[] = {"d.out", transferme }` and then call `execvp("/home/user/d.out", tab);` – Mekap May 06 '15 at 09:31
  • @Mekap: `tab` needs to be a pointer array and it misses the `NULL` termination. It should look like: `char * tab[] = {"d.out", transferme, NULL};` – alk May 06 '15 at 10:22
  • @alk yeah, sorry for that, i was just in the heat of the answer ;) – Mekap May 06 '15 at 10:25