我试图在fork()之后向孩子发送一些东西我已经设置了dup2来重定向孩子的输出。但看起来它没有得到流。 还有更多我如何得到孩子的外出。 该文件是一个外部可执行文件,只是打印文本,但它没有显示。 有什么帮助吗?
答案 0 :(得分:1)
您错误地使用pipe
,因为它只需要调用一次(RTM)。
您正在正确调用calloc
。使用calloc (100, sizeof (char))
不要将常量字符串分配给动态分配的内存部分。请改用char message[] = "sent from parent"
。这种方式也很有用,因为您可以在sizeof
而不是write
中使用100
。这也可能导致分段错误,因为"sent from parent"
所在的内存区域长度不是100
个字节。
使用execv
代替execlp
。您无需使用execlp
答案 1 :(得分:0)
一个问题是你曾两次拨打pipe
,你应该拨打一次,如:
if(pipe(tmpFd) == 0)
{
//now do fork
}
或者如果你想创建两个管道,你应该创建另一个变量,如tmpFd2
所以你可以这样做:
if(pipe(tmpFd) == 0 && pipe(tmpFd2) == 0)
{
/*
parent can write to tmpFd[1] and child can read from tmpFd[0]
child can write to tmpFd2[0] and parent can read from tmpFd2[1]
*/
}
现在你应该将两个文件描述符(tmpFd
和tmpFd2
)作为参数(argv
)传递给socond程序。
这样孩子就可以与父母沟通。
这是一个孩子和父母如何交流的例子:
int main()
{
char buffer[1024];
const char child_data[] = "child says hello";
const char parent_data[] = "parent says hello";
if(pipe(tmpFd) == 0 && pipe(tmpFd2) == 0)
{
if(fork() == (pid_t)0)
{
printf("child.\n");
write(tmpFd[1], child_data, strlen(child_data));
read(tmpFd2[0], buffer, 100);
printf("child read: %s\n" , buffer);
}
else
{
printf("parent.\n");
write(tmpFd2[1], parent_data, strlen(parent_data));
read(tmpFd[0], buffer, 100);
printf("parent read: %s\n" , buffer);
}
}
else
{
printf("pipe failed.\n");
}
return 0;
}
此示例不使用exec
,但它会为您提供想法。
答案 2 :(得分:0)
there are couple of problem in your code.. Basics of pipe, fork are missing here.
我建议先清除管道和叉子的知识。先把这个例子扔掉了 对于这个问题http://www.cim.mcgill.ca/~franco/OpSys-304-427/messages/node92.html
see below comments from #Comment# tag
void createChildren(void){
struct AgentInfo *child= head; //#Comment1# I am assuming you are getting correct child here
int tmpFd[2];
int parentWrite = tmpFd[OUT]; //#Comment2# you dont need to take this in another variable. Till now its garbage value as you have not passed it threw pipe function. It will initialise inside pipe function
int parentRead = tmpFd[IN];//#Comment3# same as above
while(child !=NULL){
pipe(tmpFd);
child->childRead = tmpFd[IN];
pipe(tmpFd); //#Comment4# prev pipe var has been overwriten. you just need one pipe to transfer data from parent to child or vice versa
//在管道中为父fd tmpFd [1]用于写入fd而tmpFd [0]用于读取 // for child fd tmpFd [0]用于写入,tmpFd [1]用于读取
child->childWrite = tmpFd[OUT];
child->agentPid = fork();
char* message = calloc(100,sizeof(char*)); //#Comment5# create this element before fork as it will create twice if you allocate it after fork
message = "sent from parent\n"; //#Comment6# This is highly misunderstanding of memory allocation if you want to use same memory as you have allocated in calloc than use strcpy function.
//constant string will allocate memory once again
if(child->agentPid<0){
exit(-1);
}
if(child->agentPid == 0){ //#Comment8# this means its parent code. For child it will be child->agentPid > 0 write you child code there. after this whoile code is totally wrong
printf("in the child\n");
dup2(child->childWrite,STDOUT_FILENO); //#Comment7# Why you are involving STDOUT/IN heare as you just want to transfer data bet parent child. you can use tmpFd for this directly
dup2(child ->childRead,STDIN_FILENO);
/*fwrite(message, 100, 1,stdin);*/
write(STDIN_FILENO, message,100);
close(child->childRead);
close(child->childWrite);
/*close(parentWrite);*/
/*close(parentRead);*/
execlp(child->fileName,child->fileName,NULL);
/*error handled here and exit status*/
}else{
/*write(child->childRead,message,100);*/
/*read(parentRead,message,100);*/
char* temp = calloc(100,sizeof(char*));
printf("in parent\n");
read(child->childWrite, temp,100);
printf("%s", temp);
child = child->next;
}
}
}