Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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
在execl调用的子进程中写入管道_C_Exec_File Descriptor - Fatal编程技术网

在execl调用的子进程中写入管道

在execl调用的子进程中写入管道,c,exec,file-descriptor,C,Exec,File Descriptor,我不明白如何在子进程sp.c中使用从mp.c创建的管道。当使用外部进程的execl时,我(认为我)似乎无法访问正确的文件描述符 /***************mp.c*****************/ #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[])

我不明白如何在
子进程
sp.c中使用从mp.c创建的
管道。当使用外部进程的
execl
时,我(认为我)似乎无法访问正确的
文件描述符

   /***************mp.c*****************/ 

#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h>  
#include <string.h> 

int main(int argc, char *argv[]) {
char  *procpath = "/mypath/sp";
char  *procname = "sp"; 
pid_t pid;
int fd[2];
int ret;
char buf[20];
memset(&buf[0], 0, sizeof(buf));
ret = pipe(fd);
if(ret == -1){
perror("pipe");
exit(1);
}
pid = fork();
printf("%d\n",pid); 
    if (pid == 0){
//dup2(mypipefd[1],STDOUT_FILENO);
    ret = execl(procpath, procpath, "1","2",NULL);
    perror("execl failed to run slave program");
    exit(1);
    } 
    else if (pid > 0){
    /* Parent process*/
    printf("execl ret val = %d",ret);
    printf("Parent process \n");
    close(fd[1]);
    read(fd[0],buf,15);
//  close(fd[1]);
    close(fd[0]);
    printf("buf: %s TEST\n", buf);
    printf("buf: %s TEST\n", buf);
    }
    else{
    printf("call to fork failed, no child\n");
    exit(-1);
    }
exit(0); 
}
/*********************mp.c******************/
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
char*procpath=“/mypath/sp”;
char*procname=“sp”;
pid_t pid;
int-fd[2];
int ret;
char-buf[20];
memset(&buf[0],0,sizeof(buf));
ret=管道(fd);
如果(ret==-1){
佩罗(“管道”);
出口(1);
}
pid=fork();
printf(“%d\n”,pid);
如果(pid==0){
//dup2(mypipefd[1],标准文件号);
ret=execl(procpath,procpath,“1”,“2”,NULL);
perror(“execl无法运行从属程序”);
出口(1);
} 
否则,如果(pid>0){
/*父进程*/
printf(“execl ret val=%d”,ret);
printf(“父进程”);
关闭(fd[1]);
读取(fd[0],buf,15);
//关闭(fd[1]);
关闭(fd[0]);
printf(“buf:%s测试\n”,buf);
printf(“buf:%s测试\n”,buf);
}
否则{
printf(“对fork的调用失败,没有子\n”);
出口(-1);
}
出口(0);
}
而创建的过程

/***************sp.c*****************/

#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h>  
#include <string.h> 
#include <errno.h>

int main(int argc, char *argv[]){
int ret;
//printf("Child process \n");
int fd[2];
pipe(fd);
//dup2(fd[1],1);
//int out;
/*ret = dup2(fd[1],1);
    if (ret = -1){
    printf("%s\n", strerror(errno));
    };*/
//sprintf()
//printf("%d\n", ret);
//mypipefd = argv[1];
printf("Child process \n");
//close(fd[0]);
write(fd[1], "Hello there!",12);
close(fd[1]);
exit(0);
}
/*************sp.c*****************/
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
int ret;
//printf(“子进程”);
int-fd[2];
管道(fd);
//dup2(fd[1],1);
//指出;
/*ret=dup2(fd[1],1);
如果(ret=-1){
printf(“%s\n”,strerror(errno));
};*/
//sprintf()
//printf(“%d\n”,ret);
//mypipefd=argv[1];
printf(“子进程”);
//关闭(fd[0]);
写(fd[1],“你好!”12);
关闭(fd[1]);
出口(0);
}

问题在于您在每个应用程序中创建的管道不同。为了通过管道正确通信,两个程序应该共享同一个管道(管道函数创建的文件描述符之一)

基本上要解决这个问题,您必须在一个应用程序中创建管道,并将文件描述符发送到另一个程序,而无需再次调用系统调用管道。可以使用socket unix域将文件描述符发送到另一个进程。看看这篇文章