简体   繁体   English

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

[英]Read Output From Background Process in C++

I have a background-process that is started at the beginning of my program, which generates output data every 20ms. 我在程序的开头启动了一个后台进程,该进程每20毫秒生成一次输出数据。 In my program, I want to read this data, but would like to avoid constantly writing to/reading from a file as this can be very slow. 在我的程序中,我想读取此数据,但要避免不断地写入文件或从文件中读取数据,因为这可能非常慢。

The output from every iteration is a single string (max length ~100 characters), and I only need the most recent data at any given time. 每次迭代的输出都是一个字符串(最大长度为100个字符),在任何给定时间我只需要最新的数据。

Ideally, I could use some sort of in-memory buffer to get this data, but I am unsure how to do this. 理想情况下,我可以使用某种内存中的缓冲区来获取此数据,但是我不确定如何执行此操作。 I have looked into using popen (see also: c++: subprocess output to stdin ), however, this seems like it is used to execute a command, and wait for a single output. 我已经研究过使用popen(另请参见: c ++:subprocess输出到stdin ),但是,这似乎是用来执行命令并等待单个输出的。 My output will be more or less constantly changing. 我的输出或多或少会不断变化。 I am running this program on Raspbian . 我正在Raspbian上运行此程序。

I think you wanna redirect stdout to a stringstream. 我认为您想将标准输出重定向到字符串流。 it is described here . 在这里描述。

Combined with this one . 结合这一点

Now you have redirected stdout and will be able to send it to another process. 现在,您已经重定向了stdout,并且可以将其发送到另一个进程。

Use shared memory: Allocate memory for the data you want to share, and allocate a second segment for a mutex variable to handle locking. 使用共享内存:为要共享的数据分配内存,并为互斥量变量分配第二段以处理锁定。

Example code sharing a float[9]: 共享float的示例代码[9]:

server 服务器

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
}

client 客户

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM