【发布时间】:2012-09-23 18:51:18
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "structs.h"
#include "server.h"
#include "client.h"
int main(void)
{
pid_t pid;
int mypipe[2];
int otherPipe[2];
if(pipe(mypipe))
{
printf("Error in making a pipe");
}
if(pipe(otherPipe))
{
printf("Error creating another pipe");
}
if((pid=fork()) == -1)
{
perror("fork");
exit(1);
}
printf("Child ID: %d" , pid);
if(pid == 0)
{
close(mypipe[1]);
close(otherPipe[0]);
client *c = new client(mypipe[0], otherPipe[1]);
wait(NULL);
//c->startProgram();
//return EXIT_SUCCESS;
}
else
{
printf("Yo");
close(mypipe[0]);
close(otherPipe[1]);
server s;
s.fdIn = otherPipe[0];
s.fdOut = mypipe[1];
s.startServer();
//wait(NULL);
//return EXIT_SUCCESS;
}
}
以上是我的代码。子进程运行良好(如果我明显删除了该评论),但是 else 永远不会被执行(或者我在终端中遗漏了什么?)。
这是输出的屏幕截图:http://i.imgur.com/dUWn8.png
关于为什么它不去 else/parent 的任何想法?
谢谢!
【问题讨论】:
-
您期待什么?
strace告诉你什么? -
你为什么要
waiting孩子?? -
我不知道 strace 是什么以及如何使用它。不过,当我有更多时间时,我会研究它。我实际上早先把父母和孩子换了,然后把它留在那里。认为它可能在孩子之前到达了父母。我的错。删除它并没有帮助。似乎 Michael K. 是正确的。感谢您的关注!
-
如果您希望输出出现在屏幕上,请使用换行符结束输出,或使用
fflush()强制输出。当您的消息不以换行符结尾时,它们将保持缓冲状态,直到缓冲区溢出或添加换行符。