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

时间:2013-03-07 04:49:03

标签: c++ background-process output

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

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

理想情况下,我可以使用某种内存缓冲区来获取此数据,但我不确定如何执行此操作。我已经研究过使用popen(参见:c++: subprocess output to stdin),但是,这似乎用于执行命令,并等待单个输出。我的输出或多或少会不断变化。 我正在Raspbian上运行此程序。

2 个答案:

答案 0 :(得分:0)

我认为您想将stdout重定向到字符串流。它被描述为here

结合this one

现在您已重定向stdout,并且可以将其发送到另一个进程。

答案 1 :(得分:0)

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

共享浮点数的示例代码[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_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
}