如何在两个C程序之间发送变量

时间:2022-04-20 19:39:09

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.

我有一个C程序,xyz.c,它对某个变量进行一些计算,比如transferme。我想传递这个变量/将这个变量传递给另一个C程序,比如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.]

fork xyz.c并使用execvp()将此transferme传递给jkl.c的可执行文件[关闭execvp只接受二进制可执行文件或shell脚本的路径。]

What is happening is,

发生的事情是,

  1. execvp() does not transfer variables, it needs const char * to be the type of the argument.
  2. execvp()不传递变量,它需要const char *作为参数的类型。

  3. passing the path to the binary executable does not work. [I tried a simple forking and execvp, without trying to pass the variable.]
  4. 将路径传递给二进制可执行文件不起作用。 [我尝试了一个简单的forking和execvp,而没有尝试传递变量。]

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. 在这种情况下告诉我如何使用execvp()?二进制可执行文件位于主文件夹中的位置,例如路径为/home/user/jkl.o

  3. Tell me how to pass this variable to the other program?
  4. 告诉我如何将此变量传递给其他程序?

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

我试着看管道,但我更加困惑。

EDIT: I meant jkl.out

编辑:我的意思是jkl.out

1 个解决方案

#1


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.

您有多种方法可以将数据传输到另一个程序。 (pipin',文件,以及更多。我可以稍后编辑我的答案,并提供一个关于在两个C程序之间发送数据的更标准指南,如果有必要的话)让我们看看你的案例的execl()。

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.

我们必须首先同意程序jkl.o将由您的主程序启动,并将接收包含该参数的字符串作为其start参数。

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

让我们看看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.

execl(),execlp()和execle()函数中的const char * arg和后续省略号可以被认为是arg0,arg1,...,argn。它们一起描述了一个或多个指向以null结尾的字符串的指针的列表,这些字符串表示执行程序可用的参数列表。按照惯例,第一个参数应指向与正在执行的文件关联的文件名。参数列表必须由NULL指针终止,并且由于这些是可变参数函数,因此必须强制转换此指针(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.

好。所以你首先要给execl()你的路径,然后在每一行中给一个参数。你指出你需要分叉,这是真的。所以,让我们这样做,我们分叉,然后我们发送一个包含你的transferme的字符串。我将假设这个变量是一个常规字符串。

 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

我忘了argv [0](即execl的第二个参数必须是程序本身的名称。),所以试试

execl("/home/user/d.out", "d.out", transferme, (char*) NULL);

#1


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.

您有多种方法可以将数据传输到另一个程序。 (pipin',文件,以及更多。我可以稍后编辑我的答案,并提供一个关于在两个C程序之间发送数据的更标准指南,如果有必要的话)让我们看看你的案例的execl()。

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.

我们必须首先同意程序jkl.o将由您的主程序启动,并将接收包含该参数的字符串作为其start参数。

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

让我们看看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.

execl(),execlp()和execle()函数中的const char * arg和后续省略号可以被认为是arg0,arg1,...,argn。它们一起描述了一个或多个指向以null结尾的字符串的指针的列表,这些字符串表示执行程序可用的参数列表。按照惯例,第一个参数应指向与正在执行的文件关联的文件名。参数列表必须由NULL指针终止,并且由于这些是可变参数函数,因此必须强制转换此指针(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.

好。所以你首先要给execl()你的路径,然后在每一行中给一个参数。你指出你需要分叉,这是真的。所以,让我们这样做,我们分叉,然后我们发送一个包含你的transferme的字符串。我将假设这个变量是一个常规字符串。

 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

我忘了argv [0](即execl的第二个参数必须是程序本身的名称。),所以试试

execl("/home/user/d.out", "d.out", transferme, (char*) NULL);