Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过fork和exec执行程序_C_Linux_Exec - Fatal编程技术网

如何通过fork和exec执行程序

如何通过fork和exec执行程序,c,linux,exec,C,Linux,Exec,我有一个二进制文件,其中包含一个程序,其中的函数用C编写,如下所示: int main() { int a, b; foo(a,b); return 0; } 现在我想在另一个名为“solver”的程序中使用fork()和execve()来执行这个程序 这样好吗?因为它是编译的,但我不确定“worker”程序中函数的参数是否接收到通过命令行传递给“solver”程序的变量 #include <stdio.h> #include <errno.h>

我有一个二进制文件,其中包含一个程序,其中的函数用C编写,如下所示:

int main()
{
    int a, b;
    foo(a,b);
    return 0;
}
现在我想在另一个名为“solver”的程序中使用fork()和execve()来执行这个程序


这样好吗?因为它是编译的,但我不确定“worker”程序中函数的参数是否接收到通过命令行传递给“solver”程序的变量

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>

static char *sub_process_name = "./work";

int main(int argc, char *argv[])
{
    pid_t process;

    process = fork();

    if (process < 0)
    {
        // fork() failed.
        perror("fork");
        return 2;
    }

    if (process == 0)
    {
        // sub-process
        argv[0] = sub_process_name; // Just need to change where argv[0] points to.
        execv(argv[0], argv);
        perror("execv"); // Ne need to check execv() return value. If it returns, you know it failed.
        return 2;
    }

    int status;
    pid_t wait_result;

    while ((wait_result = wait(&status)) != -1)
    {
        printf("Process %lu returned result: %d\n", (unsigned long) wait_result, status);
    }

    printf("All children have finished.\n");

    return 0;
}
#包括
#包括
#包括
#包括
静态字符*子进程名称=“/work”;
int main(int argc,char*argv[])
{
pid_t过程;
进程=fork();
如果(进程<0)
{
//fork()失败。
佩罗尔(“福克”);
返回2;
}
如果(进程==0)
{
//子过程
argv[0]=sub_process_name;//只需更改argv[0]指向的位置即可。
execv(argv[0],argv);
perror(“execv”);//我们需要检查execv()返回值。如果它返回,您就知道它失败了。
返回2;
}
智力状态;
等待结果;
while((等待结果=等待(&状态))!=-1)
{
printf(“进程%lu返回结果:%d\n”,(无符号长时间)等待结果,状态);
}
printf(“所有子项都已完成。\n”);
返回0;
}

./work将使用与原始程序相同的参数启动。

您可以阅读以了解其工作方式以及必须传递给它的参数。您可能希望阅读
main()
的参数
argv[0]
提供的内容。“这是一种好方法吗?”否;-)您想使用与第一个程序完全相同的参数启动“工作”吗?确实如此。我想启动它并调用里面的函数foo(a,b),并给主程序提供参数,这正是我想要做的。谢谢你的帮助!
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>

static char *sub_process_name = "./work";

int main(int argc, char *argv[])
{
    pid_t process;

    process = fork();

    if (process < 0)
    {
        // fork() failed.
        perror("fork");
        return 2;
    }

    if (process == 0)
    {
        // sub-process
        argv[0] = sub_process_name; // Just need to change where argv[0] points to.
        execv(argv[0], argv);
        perror("execv"); // Ne need to check execv() return value. If it returns, you know it failed.
        return 2;
    }

    int status;
    pid_t wait_result;

    while ((wait_result = wait(&status)) != -1)
    {
        printf("Process %lu returned result: %d\n", (unsigned long) wait_result, status);
    }

    printf("All children have finished.\n");

    return 0;
}