|
|

楼主 |
发表于 2005-6-14 14:53:53
|
显示全部楼层
谢谢你的回复,问题确实是在这里。不过这个还不是我想实现的功能。在我的想实现的功能中可能还不希望主程序被阻塞。
我想实现的功能是,在程序test中输入命令,将命令重定向到程序engine, 然后
将engine的返回结果在当前进程中打印出来。
下面是我新编的代码,跟前面略有不同,
它包括两个程序代码,附一是test.cpp,附二是engine.cpp
在我这里如果编译后直接运行./test,则无法返回engine程序的字串
但是我在用gdb调试的时候就可以按照理想的顺序返回。
哪位大虾能帮忙看看问题出在哪里?非常感谢。
附一:test.cpp
- #include <iostream>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <signal.h>
- #define BUF_SIZE 1024
- using namespace std;
- /* engine related elements */
- int to_eng[2]; /* pipe used to send commands to the engine */
- int from_eng[2]; /* pipe used to recieve engine response */
- void start_engine(); /* start engine and set the pipes */
- void send_to_eng(char *msg);
- int recv_from_eng(char *msg);
- void start_engine() {
- char *argv[64], *p;
- char buf[BUF_SIZE];
- int pid,k;
- /* create the pipes */
- if(signal(SIGPIPE, SIG_IGN)==SIG_ERR)
- {
- perror("signal error");
- exit(EXIT_FAILURE);
- }
- if (pipe(to_eng)<0 || pipe(from_eng)<0)
- {
- perror("creating pipe error\n");
- exit(EXIT_FAILURE);
- }
-
- /* start the engine */
- if ( (pid = fork()) < 0 )
- {
- //perror("pid<0");
- perror("fork");
- exit(EXIT_FAILURE);
- }
- else if(pid==0)
- {
- /* the child */
- dup2(to_eng[0], 0);
- dup2(from_eng[1], 1);
- close(to_eng[1]);
- close(from_eng[0]);
- if(dup2(to_eng[0], STDIN_FILENO)!=STDIN_FILENO) /* redirect stdout to the pipe */
- perror("dup2 error to stdin\n");
- close(to_eng[0]);
- if(dup2(from_eng[1], STDERR_FILENO)!=STDERR_FILENO) /* redirect stdout to the pipe */
- perror("dup2 error to stdout\n");
- close(from_eng[1]);
- if((execl("./engine", "engine", (char *)0)) <0)
- {
- perror("error");
- }
- perror(argv[0]);
- exit( EXIT_FAILURE );
- }
- else {
- /* the parent */
- close ( to_eng[0]);
- close ( from_eng[1]);
- /* set nonblocking read */
- fcntl( from_eng[0], F_SETFL, O_NONBLOCK);
- }
- }
- void send_to_eng(char *msg) {
- write ( to_eng[1], msg, strlen(msg));
- }
- int recv_from_eng(char *msg){
- memset( msg, '\0', BUF_SIZE );
- int nread = read( from_eng[0], msg, BUF_SIZE );
- cout << "nread=" << nread << endl;
- return strlen(msg);
- }
- int main()
- {
- char msg[BUF_SIZE];
- start_engine();
-
- int ret = 0;
- while((ret = recv_from_eng(msg))>0)
- {
- cout << "ret=" << ret << ":" << msg;
- }
- send_to_eng("change\n");
- while((ret = recv_from_eng(msg))>0)
- {
- cout << "ret=" << ret << ":" << msg;
- }
- send_to_eng("stop\n");
- while((ret = recv_from_eng(msg))>0)
- {
- cout << "ret=" << ret << ":" << msg;
- }
- exit(0);
- }
复制代码
附二:engine.cpp
- #include <cstdio>
- #include <cstdlib>
- #include <iostream>
- using namespace std;
- int main()
- {
- cout << "loop begin" << endl;
- int n, int1, int2;
- char msg[1024];
- while(1)
- {
- memset( msg, '\0', 1024 );
- int n = read(STDIN_FILENO, msg, 1024);
-
- if(strstr(msg, "stop"))
- {
- write(STDOUT_FILENO, "stopped\n", 9);
- break;
- }
- if(strstr(msg, "change"))
- write(STDOUT_FILENO, "Changed\n", 9);
- else if(strlen(msg)>0)
- write(STDOUT_FILENO, "Hello,world\n", 13);
- }
- return 0;
- }
复制代码 |
|