本文介绍了C/C ++-运行system("process&"),然后写入其标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Linux和C/C ++上工作.我编写了一个带有一些线程(#include pthread.h)的程序,并使用sudo运行了该程序.

I am working on Linux and C/C++. I wrote a program with some threads (#include pthread.h) and I run it with sudo.

一个线程运行一个进程(mplayer),并通过添加&"使其保持运行状态,以便system()可以快速返回.

One thread runs a process (mplayer) and leaves it running by adding " &", so that system() can return quickly.

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

mplayer进程正常运行,并按预期播放音乐.

The mplayer process runs normally and plays music as expected.

在那之后,我通过运行pidof获得了它的进程ID.假设它返回2449.posix互斥锁用于在该线程和第二个线程上写入/读取该进程ID.

After that, I get its process ID by running pidof. 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.

在第二个线程上,我尝试使用/proc/2449/fd/0管道(称为管道或流?)将数据写入mplayer:

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?):

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

system()返回0,但是mplayer进程什么也没得到.>"命令应播放下一首曲目.

system() returns 0, but the mplayer process does not get anything. The ">" command should play the next track.

stdin 流是否被其他进程继承?

Is the stdin stream being inherited by some other process?

2449进程中列出了几份fd,stdin流中是否有其中之一(0以外)?

There are several fd's listed under the 2449 process, is one of them (besides 0) the stdin stream?

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#

我还尝试了另一种方法...我使用具有写权限的popen().我尝试使用fprintf发送命令,但mplayer似乎也没有收到任何东西.

I also tried another approach... I used popen() with write permissions. I tried sending the command with fprintf, but mplayer didn't seem to receive anything as well.

如果需要更多代码,请告诉我.

If any more code is needed, please let me know.

任何提示将不胜感激.谢谢.

Any hints will be appreciated. Thanks.

推荐答案

我使用mplayer slave选项和输入作为fifo文件.它工作正常.

I used the mplayer slave option and the input as a fifo file. It is working correctly.

使用mkfifo创建Linux fifo文件:

Create the Linux fifo file with mkfifo:

system("mkfifo /tmp/slpiplay_fifo");

使用以下命令打开mplayer:

Open mplayer with:

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

使用fifo将下一步"命令传递给mplayer:

Pass a "next" command to mplayer by using the fifo:

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

这篇关于C/C ++-运行system("process&"),然后写入其标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 14:35