gpt4 book ai didi

c++ - 如何使用 dup2 定期读取重定向到文件描述符的 stdout 上的内容

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:41:01 28 4
gpt4 key购买 nike

我正在努力连续读取重定向到文件的 stdout 的内容。请帮我解决问题。

我的详细问题是我在第一个线程中有两个线程,我必须执行一个 c 文件,该文件不断将输出写入标准输出(即在无限循环中写入 hello world),在第二个线程中我必须逐行检索定期从 stdout 输出并存储在缓冲区中。对于第二个线程实现,我创建了管道并将标准输出重定向到该管道的写入端,并尝试逐行连续读取该管道的内容,但我没有得到预期的结果。第二个线程的代码如下:

int redirectPipe[2];
//creation of pipe
pipe(redirectPipe);
//redirection of pipe
dup2(redirectPipe[1],STDOUT_FILENO);
//reading continuously line by line from pipe
// some code here

有人可以帮我编码如何从这个管道连续逐行读取吗?

下面是代码:请建议我,我的方法对吗?以及如何实现 entryOpThread() 方法以便它定期读取 stdout 的内容我是否应该使用 read() 系统调用或 getLine() 或任何其他方法从管道逐行读取(stdout 被重定向到此管道)。在此示例中,exec 线程正在执行 test.c,它在 stdout 上连续打印 hello world,因此根据我的要求,我必须在 Opthread 中从管道连续逐行读取此输出(“Hello world”)并将其存储在缓冲区

//*************************CODE***********************************************************
#include <iostream>
#include<pthread>
#include<unistd.h>
#include<sys/wait.h>
const int MAX_LENGTH=100;
using namespace std;

//! file descriptor to duplicate the system standard input
//!
int inputPIPE[2];


//! file descriptor to duplicate the system standard output
int OutputPipe[2];

//!Thread to retrieve output
//!
pthread_t OpThread;

//!Thread to execute script
//!
pthread_t ExecThread;


//! entry point of the exec thread
void *entryExecThread(void * obj)
{
//create a child process to launch the script
int lPid;
lPid=fork();
//connect read end of the pipe to the standard input
if(dup2(inputPIPE[0],STDIN_FILENO)==-1)
{
//throw exception
}

//connect write end of the pipe to the standard output
if(dup2(OutputPipe[1],STDOUT_FILENO)==-1)
{
//throw exception
}
if(lPid==0)
{



execvp("./test",NULL);
}

else
{
int mProcessId=lPid;
waitpid(mProcessId,NULL,0);
}
return NULL;

}

//! entry point of the output thread
void *entryOpThread(void * obj)
{
//read contents of stdout periodically
/* char *lMssg=new char[MAX_LENGTH];
read(OutputPipe[0],lMssg,MAX_LENGTH);

FILE *fd=fdopen(OutputPipe[0],"r");
int lBufferLength=MAX_LENGTH;
while(true)
{
getline(&lMssg,(size_t * )&lBufferLength,fd);
// push message to the output buffer
if (lMssg !=NULL)
{
pthread_mutex_lock( &mOutputBufferMutex);
mOutputBuffer.push(lMssg);
pthread_mutex_unlock( &mOutputBufferMutex);
}
}
*/

}
int main() {

//call to pipe system call
if(pipe(inputPIPE)==-1)
{
printf("ERROR IN OPENING FILE \n");
}
if(pipe(OutputPipe)==-1)
{
printf("ERROR IN OPENING FILE\n ");
}

//CREATE OUTPUT THREAD TO RETRIEVE OUTPUT

if (pthread_create(&OpThread,0,entryOpThread,NULL)!=0)
{

//throw exception
printf("Creation of Output thread failed for Script with ID\n");
}

//Create execution thread to launch script
pthread_t execThread;
if (pthread_create(&ExecThread,0,entryExecThread,NULL)!=0)
{

//Stop output thread
pthread_cancel(OpThread);
//throw exception
printf("Creation of Execution thread failed for Script with ID\n");
}
}
//**********************************************************************************888
test.c

#include <stdio.h>
#include "string.h"
//using namespace std;

int main() {
int i=0;
while(i<500)
{
printf("!!!Hello World!!! \n" ); // prints !!!Hello World!!!
//extracting header and data from message
sleep(5);
i++;
}
return 0;
}
//***********************************************************************************

最佳答案

这可能是缓冲问题 ( http://www.pixelbeat.org/programming/stdio_buffering/ )?

当 stdout 连接到终端时,它的输出将被行缓冲。如果不是这种情况,则输出是页面缓冲的。

要验证,您可以尝试从编写器线程中删除 sleep(),并检查消费者线程上的输出(每 5 秒打印少量字节需要一段时间才能填满一页)。从那里您可以考虑使用 fflush()setvbuf()

关于c++ - 如何使用 dup2 定期读取重定向到文件描述符的 stdout 上的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3433548/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com