C++ 在C+中读取后台进程的输出+;

C++ 在C+中读取后台进程的输出+;,c++,background-process,output,C++,Background Process,Output,我有一个后台进程,它在程序开始时启动,每20毫秒生成一次输出数据。在我的程序中,我希望读取这些数据,但希望避免不断地写入/读取文件,因为这可能非常缓慢 每次迭代的输出都是一个字符串(最大长度约100个字符),我只需要在任何给定时间获取最新的数据 理想情况下,我可以使用某种内存缓冲区来获取这些数据,但我不确定如何做到这一点。我已经研究过如何使用popen(另请参见:),但是,这似乎是用来执行命令并等待单个输出的。我的输出或多或少会不断变化。 我正在运行此程序。我想您应该将stdout重定向到str

我有一个后台进程,它在程序开始时启动,每20毫秒生成一次输出数据。在我的程序中,我希望读取这些数据,但希望避免不断地写入/读取文件,因为这可能非常缓慢

每次迭代的输出都是一个字符串(最大长度约100个字符),我只需要在任何给定时间获取最新的数据

理想情况下,我可以使用某种内存缓冲区来获取这些数据,但我不确定如何做到这一点。我已经研究过如何使用popen(另请参见:),但是,这似乎是用来执行命令并等待单个输出的。我的输出或多或少会不断变化。
我正在运行此程序。

我想您应该将stdout重定向到stringstream。对其进行了描述

结合

现在您已经重定向了标准输出,可以将其发送到另一个进程。

使用共享内存: 为要共享的数据分配内存,并为互斥变量分配第二段以处理锁定

共享浮点数的示例代码[9]:

服务器

int shmid, shmid_mutex;
key_t key = 1337, key_mutex = 1338;
float *shm;
pthread_mutex_t *shm_mutex;
int SHMSZ = sizeof(float[9]), SHMSZ_mutex = sizeof(pthread_mutex_t);

//Mutex shared memory
shmid_mutex = shmget(key_mutex, SHMSZ_mutex, IPC_CREAT | 0660); //Create the segment
shm_mutex = (pthread_mutex_t *)shmat(shmid_mutex, NULL, 0); //Attach the segment to data space
shmctl(shmid_mutex, SHM_LOCK, (struct shmid_ds *) NULL); //Lock this segment from being swapped

//Setup mutex attributes
pthread_mutexattr_t psharedm;
pthread_mutexattr_init(&psharedm);
pthread_mutexattr_setpshared(&psharedm, PTHREAD_PROCESS_SHARED);
//Initialize mutex
pthread_mutex_init(shm_mutex, &psharedm);

//Float array shared memory
shmid = shmget(key, SHMSZ, IPC_CREAT | 0660); //Create the segment
shm = (float *)shmat(shmid, NULL, 0); //Attach the segment to data space
shmctl(shmid, SHM_LOCK, (struct shmid_ds *) NULL); //Lock this segment from being swapped

float x = 0.1;
while(true)
{
    pthread_mutex_lock(shm_mutex); //start critical section
    //Write to shared memory
    for (int i = 0; i < 9; i++)
        shm[i] = x * i;
    x += 0.1;
    printf("W ");
    for (int i = 0; i < 9; i++)
        printf("%f ", shm[i]);
    printf("\n");
    usleep(100000); //slow down output to make it readable
    pthread_mutex_unlock(shm_mutex);  //end critical section
    usleep(10000); //wait for 10ms to simulate other calculations
}
int-shmid,shmid\u互斥体;
key\u t key=1337,key\u mutex=1338;
浮动*shm;
pthread_mutex_t*shm_mutex;
int SHMSZ=sizeof(float[9]),SHMSZ\u mutex=sizeof(pthread\u mutex\t);
//互斥共享内存
shmid_mutex=shmget(key_mutex,SHMSZ_mutex,IPC_CREAT | 0660)//创建段
shm_mutex=(pthread_mutex_t*)shmat(shmid_mutex,NULL,0)//将段附加到数据空间
shmctl(shmid_互斥体,SHM_锁,(struct shmid_ds*)NULL)//锁定正在交换的此段
//设置互斥体属性
pthread_mutextatr_t psharedm;
pthread_mutexattr_init(&psharedm);
pthread_mutexattr_setpshared(&psharedm,pthread_PROCESS_SHARED);
//初始化互斥
pthread_mutex_init(shm_mutex和psharedm);
//浮点阵列共享内存
shmid=shmget(键,SHMSZ,IPC|U创建| 0660)//创建段
shm=(float*)shmat(shmid,NULL,0)//将段附加到数据空间
shmctl(shmid,SHM_LOCK,(struct shmid_ds*)NULL)//锁定正在交换的此段
浮动x=0.1;
while(true)
{
pthread_mutex_lock(shm_mutex);//启动临界段
//写入共享内存
对于(int i=0;i<9;i++)
shm[i]=x*i;
x+=0.1;
printf(“W”);
对于(int i=0;i<9;i++)
printf(“%f”,shm[i]);
printf(“\n”);
usleep(100000);//减慢输出速度以使其可读
pthread_mutex_unlock(shm_mutex);//结束临界段
usleep(10000);//等待10毫秒以模拟其他计算
}
客户

int shmid, shmid_mutex;
key_t key = 1337, key_mutex = 1338;
float *shm;
pthread_mutex_t *shm_mutex;
int SHMSZ = sizeof(float[9]), SHMSZ_mutex = sizeof(pthread_mutex_t);

//Mutex shared memory
shmid_mutex = shmget(key_mutex, SHMSZ_mutex, IPC_CREAT | 0660); //Create the segment
shm_mutex = (pthread_mutex_t *)shmat(shmid_mutex, NULL, 0); //Attach the segment to data space
shmctl(shmid_mutex, SHM_LOCK, (struct shmid_ds *) NULL); //Lock this segment from being swapped

//Float array shared memory
shmid = shmget(key, SHMSZ, 0660); //Locate the segment
shm = (float *)shmat(shmid, NULL, 0); //Attach the segment to data space
shmctl(shmid, SHM_LOCK, (struct shmid_ds *) NULL); //Lock this segment from being swapped

while(true)
{
    pthread_mutex_lock(shm_mutex); //start critical section
    //Read from shared memory
    printf("R ");
    for (int i = 0; i < 9; i++)
        printf("%f ", shm[i]);
    printf("\n");
    usleep(100000); //slow down output to make it readable
    pthread_mutex_unlock(shm_mutex); //end critical section
    usleep(10000); //wait for 10ms to simulate other calculations
}
int-shmid,shmid\u互斥体;
key\u t key=1337,key\u mutex=1338;
浮动*shm;
pthread_mutex_t*shm_mutex;
int SHMSZ=sizeof(float[9]),SHMSZ\u mutex=sizeof(pthread\u mutex\t);
//互斥共享内存
shmid_mutex=shmget(key_mutex,SHMSZ_mutex,IPC_CREAT | 0660)//创建段
shm_mutex=(pthread_mutex_t*)shmat(shmid_mutex,NULL,0)//将段附加到数据空间
shmctl(shmid_互斥体,SHM_锁,(struct shmid_ds*)NULL)//锁定正在交换的此段
//浮点阵列共享内存
shmid=shmget(键,SHMSZ,0660)//定位该段
shm=(float*)shmat(shmid,NULL,0)//将段附加到数据空间
shmctl(shmid,SHM_LOCK,(struct shmid_ds*)NULL)//锁定正在交换的此段
while(true)
{
pthread_mutex_lock(shm_mutex);//启动临界段
//从共享内存读取
printf(“R”);
对于(int i=0;i<9;i++)
printf(“%f”,shm[i]);
printf(“\n”);
usleep(100000);//减慢输出速度以使其可读
pthread_mutex_unlock(shm_mutex);//结束临界段
usleep(10000);//等待10毫秒以模拟其他计算
}

只需谷歌搜索进程间通信的各种方法,这与我可以使用fprintf()执行此操作大致相同?fprintf打印到stdout,重定向stdout重定向fprintf