freopen stderr进入文件,但仍然打印到控制台

时间:2017-11-02 13:52:37

标签: c pthreads popen freopen

这是我的代码架构(为了简化错误控制已删除)

主:

  freopen("error.txt","w",stderr);//redirecting stderr to error.txt
  FILE *fp=popen("./process", "w");//lunching a process
  pthread_t readerThread;

  /*reading output from process lunched in a thread otherwise it could block*/
  pthread_create(&readerThread, NULL,IsKhacToolKit::threadRead, solver);
  solver->generateDimacFile(fp);
  pclose(fp);
  pthread_exit(0);

对象解算器使用

fprintf(stderr,"Somes debugs Messages" --------%f seconds |%f seconds\n", (double)(start-tmp)/CLOCKS_PER_SEC, (double)start/CLOCKS_PER_SEC);
无处不在,可以追踪发生的事情。

我的问题是,当我在午餐时执行可执行程序时,我可以看到调试消息,我不明白为什么因为在做任何事情之前我将stderr重定向到error.txt。我在这里缺少什么?

我想要做的就是吃一个过程,给他输出威胁,然后我需要阅读它的outPut。但显然我不明白它是如何工作的,因为在这里我已经无法理解调试信息打印到控制台的原因了。

此外,我在线程函数中计算进程的输出后fclose(stderr)。

//编辑 例如:

_______@voisin:~/espaces/travail/calculabilite/version2$ ./dimac_generator_is_k_HAC 10K2 5
Initializing adjList --------0.003381 seconds |0.003381 seconds
Initialiszing translater --------0.000125 seconds |0.003506 seconds
Allocating solutionParser --------0.000012 seconds |0.003518 seconds
s UNSATISFIABLE
^C
_______@voisin:~/espaces/travail/calculabilite/version2$ cat error.txt 
Preparing buffer to receive dimacFile --------0.000627 seconds |0.004145 seconds
Tranlating contraint 1 --------0.000451 seconds |0.004596 seconds
Tranlating contraint 2 --------0.000045 seconds |0.004641 seconds
Tranlating contraint 3 --------0.000010 seconds |0.004651 seconds
Tranlating contraint 4 --------0.000037 seconds |0.004688 seconds
Sending dimacFile to glucose --------0.000029 seconds |0.004717 seconds
_______@voisin:~/espaces/travail/calculabilite/version2$ 

整件事尚未完成,所以它阻止了。我需要 Ctrl + C 但你可以看到 Ctrl + C 之前已经打印过调试消息。打印后添加fflush使得error.txt不再为空。

0 个答案:

没有答案