简体   繁体   English

C / C ++-运行system(“ process&”),然后写入其标准输入

[英]C/C++ - Run system(“process &”) and then write to its stdin

I am working on Linux and C/C++. 我正在Linux和C / C ++上工作。 I wrote a program with some threads (#include pthread.h) and I run it with sudo. 我编写了一个带有一些线程(#include pthread.h)的程序,并使用sudo运行它。

One thread runs a process (mplayer) and leaves it running by adding " &", so that system() can return quickly. 一个线程运行一个进程(mplayer),并通过添加“&”使其保持运行状态,以便system()可以快速返回。

system("mplayer -loop 0 /mnt/usb/* &");

The mplayer process runs normally and plays music as expected. mplayer进程正常运行,并按预期播放音乐。

After that, I get its process ID by running pidof. 之后,我通过运行pidof获得其进程ID。 Let's say that it returns 2449. A posix mutex is used to write/read that process ID on this thread and on the second thread. 假设它返回2449。posix互斥锁用于在该线程和第二个线程上写入/读取该进程ID。

On the second thread I try to write data to mplayer by using the /proc/2449/fd/0 pipe (is it called a pipe or stream?): 在第二个线程上,我尝试使用/ proc / 2449 / fd / 0管道(将其称为管道还是流?)将数据写入mplayer:

system("echo \">\" > /proc/2499/fd/0");

system() returns 0, but the mplayer process does not get anything. system()返回0,但是mplayer进程什么也没得到。 The ">" command should play the next track. “>”命令应播放下一首曲目。

Is the stdin stream being inherited by some other process? stdin流是否被其他进程继承?

There are several fd's listed under the 2449 process, is one of them (besides 0) the stdin stream? 在2449进程下列出了多个fd,stdin流中是否是其中之一(除0外)?

root@pisanlink:/proc# cd 2499
root@pisanlink:/proc/2499# cd fd
root@pisanlink:/proc/2499/fd# ls
0  1  2  3  4  5  7
root@pisanlink:/proc/2499/fd# 

I also tried another approach... I used popen() with write permissions. 我还尝试了另一种方法...我使用具有写权限的popen()。 I tried sending the command with fprintf, but mplayer didn't seem to receive anything as well. 我尝试使用fprintf发送命令,但是mplayer似乎也没有收到任何东西。

If any more code is needed, please let me know. 如果需要更多代码,请告诉我。

Any hints will be appreciated. 任何提示将不胜感激。 Thanks. 谢谢。

Use popen (not system) to open the process. 使用popen (不是系统)打开进程。 It will create the process with a pipe that you can either read from or write to (but not both). 它将使用您可以读取或写入(但不能同时读取和写入)的管道创建流程。 In your case, you'd open it with "w" for writing. 对于您的情况,您可以使用“ w”打开它进行编写。 From there you can simply use fwrite to send data to the process' stdin. 从那里,您可以简单地使用fwrite将数据发送到进程的stdin。

Pseudo-code Example: 伪代码示例:

FILE * pFile = popen("mplayer -loop 0 /mnt/usb/*", "w");

if(pFile == NULL)
   // Handle error

// Send ">" to process' stdin
const char * psData = ">";
size_t nNumWritten = fwrite(psData, 1, strlen(psData), pFile);

if(nNumWritten != 1)
   // Handl error

...

pclose(pFile);
pFile = NULL;

I used the mplayer slave option and the input as a fifo file. 我使用mplayer slave选项和输入作为fifo文件。 It is working correctly. 它工作正常。

Create the Linux fifo file with mkfifo: 使用mkfifo创建Linux fifo文件:

system("mkfifo /tmp/slpiplay_fifo");

Open mplayer with: 使用以下命令打开mplayer:

system("mplayer -slave -idle -really-quiet -input file=/tmp/slpiplay_fifo /mnt/usb_slpiplay/* &");

Pass a "next" command to mplayer by using the fifo: 使用fifo将“ next”命令传递给mplayer:

system("echo \"pt_step 1\" >> /tmp/slpiplay_fifo");

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

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