如何解决在结构中写入管道失败
我有一个具有以下数据成员的结构
struct process{
char *arg1;
char *arg2;
int fd[2];
int shmID;
};
我使用arg1,arg2和shmID初始化名为process1的进程的结构,shmId是我使用shmget生成的共享内存进程的ID
然后我使用以下代码从存储在我的结构中的数组中创建一个管道
if(pipe(process1.fd) < 0){
printf("Pipe Failed\n");
exit(1);
}
然后我调用一个函数并传入刚创建并初始化的结构的地址,该结构的名称现在为“ unit”。我在函数中派生了该进程,如果进程的父进程关闭了读取端,则写入管道,然后关闭写入端,然后在子进程中,关闭写入端并从管道中读取。
但是什么也没写到管道
int val = 12;
pid_t pid = fork();
// If pid is less than 0 the fork failed
if(pid < 0){
printf("Fork Failed\n");
exit(-1);
}
// Parent Code
else if(pid > 0){
close(unit->fd[0]);
write(unit->fd[1],&val,sizeof(int));
close(unit->fd[1]);
....additional code (I can add if needed,it just waits for the return of the child)
}
// In Child
else if(pid == 0){
close(unit->fd[1]);
char str[8];
sprintf(str,"%d",unit->fd[0]);
execlp("./program2","programName",unit->arg1,unit->arg2,str,NULL);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。