Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
Multithreading 我的要求是从单个程序运行多个可执行文件_Multithreading_Exec_Multiprocess - Fatal编程技术网

Multithreading 我的要求是从单个程序运行多个可执行文件

Multithreading 我的要求是从单个程序运行多个可执行文件,multithreading,exec,multiprocess,Multithreading,Exec,Multiprocess,我的要求是在Linux平台下使用C/C++从单个程序运行多个可执行文件 我已经用fork+exec编写了运行单个可执行文件的代码,我能够成功地运行它 由于exec函数接受该文件作为第一个参数,因此我不确定如何重定向输出、读取输入并为可执行文件传递类似cmd的参数。尽管我将cmd行作为传递参数传递,但没有发现任何更改 注意:我需要运行的可执行文件有多个线程在运行 int status; pid_t p_pid, c_pid; const char *path=/home/user/foo" p_

我的要求是在Linux平台下使用C/C++从单个程序运行多个可执行文件

我已经用fork+exec编写了运行单个可执行文件的代码,我能够成功地运行它

由于exec函数接受该文件作为第一个参数,因此我不确定如何重定向输出、读取输入并为可执行文件传递类似cmd的参数。尽管我将cmd行作为传递参数传递,但没有发现任何更改

注意:我需要运行的可执行文件有多个线程在运行

int status;
pid_t p_pid, c_pid;
const char *path=/home/user/foo"

p_pid = getpid();
c_pid = fork();

switch(c_pid)
{
    case -1:
            perror(" fork failed");
            exit(EXIT_FAILURE);
            break;

    case 0:
            execv(path, NULL);
            //execv(Pis1_binPath, NULL);
            printf("==%s",strerror(errno));
           // execl(Pis1_binPath, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
            break;
    default:
            break;
}

printf("==Parent PID:%d\n",p_pid);
printf("==Child PID:%d\n",c_pid);
在fork()之后,两个进程的文件描述符是相同的副本。在子对象中,可以通过以下方式重定向stdin:

if ((fd = open(“myfile”, O_RDONLY)) > 0) {
    dup2(fd, 0);
    close(fd);
}
显然,您希望正确地处理错误;以上所述只是避免了显而易见的问题

同样,您可以在fork之前创建一个管道(),然后重新安排fd,以便可以控制stdin或捕获stdout、stderr等

execv()的第二个参数是字符串数组,它将被复制到新图像中的“argv”中